Redis and Memcache

Time:2021-4-21

1. The difference between redis and Memcache

  • Redis supports complex data structures
  • Redis supports native cluster mode
  • Redis only uses a single core, while Memcache can use multiple cores. So on average, the efficiency of storing small data on each core is higher than that of Memcache. For data storage above 100k, Memcache is more efficient than redis.

2. Why is redis single threaded so fast

  • Pure memory operation
  • The core is the mechanism based on I / O multiplexing
  • C language, closer to the underlying operating system
  • Single thread avoids the frequent context switching of multithreading and the possible competition problem of multithreading

3. Two methods of redis persistence

Advantages and disadvantages of RDB:
  • RDB generates multiple data files, and each data file represents the data in redis at a time;
  • RDB has little impact on the external read-write services provided by redis, because the main process fork out a subprocess, which allows the subprocess to perform disk I / O operations for RDB persistence;
  • If you want to lose as little data as possible in case of redis failure, AOF will be better than RDB. Because RDB data snapshot is usually generated once every five minutes or more, if redis fails during this period, the five minutes of data will be lost.
  • When RDB generates a snapshot of the fork subprocess, if the data file is very large, it may cause the service provided by the client to be suspended for several milliseconds or even seconds.
Advantages and disadvantages of AOF:
  • Aof can guarantee less data loss, because AOF mode will execute fsync once per second by a thread in the background, and write the data in the buffer to the hard disk;
  • Aof log files are written in append only mode, that is, the way of appending to the end of the file, so the cost of disk addressing is reduced, and the writing efficiency is very high. Even if the end of the file is damaged, it is easy to repair;
  • Aof log file records every executed readability statement, which is suitable for emergency recovery of catastrophic deletion. For example: if you accidentally execute flushall, clear all the data, copy the AOF file, delete the last command of flushall, and put back the AOF file, you can recover all the data through the automatic recovery mechanism;
  • For the same data, AOF log file is usually larger than RDB data snapshot file;
  • After AOF is turned on, fsync will be executed once a second, so the QPS written by redis is lower than RDB;