1. Differences between Redis and Memcache

  • Redis supports complex data structures
  • Redis supports native clustering
  • Redis only uses a single core, while Memcache can use multiple cores. Therefore, Redis is more efficient than Memcache in storing small data per core on average. Memcache is more efficient than Redis with data storage above 100K.

2. Why is Redis single threaded so fast

  • Memory only operation
  • The core is a mechanism based on I/O multiplexing
  • C language implementation, closer to the underlying operating system
  • The single thread avoids the frequent context switching of multiple threads and avoids the contention problem that may arise from multiple threads

3. Two methods of Redis persistence

Advantages and disadvantages of RDB:
  • RDB generates multiple data files, each of which represents the data in Redis at a moment;
  • RDB has little impact on the read and write services provided by Redis, because the main Redis process forks a child process to perform disk I/O operations for RDB persistence.
  • If you want to lose as little data as possible in the event of a Redis failure, AOF is better than RDB. Since RDB data snapshots are typically generated every 5 minutes or longer, if Redis fails during this time, the 5 minutes of data is lost.
  • If the data file is too large during the fork process, it may cause the service provided by the client to pause for milliseconds or even seconds.
Advantages and disadvantages of AOF:
  • AOF guarantees less data loss because it executes fsync through a thread in the background once a second to write the buffer data to disk.
  • The AOF log file is written in APPEND-ONLY mode, that is, the end of the file is appended to write the way, so there is a lot less disk addressing overhead, writing efficiency is very high, even if the end of the file is damaged, it is also easy to repair;
  • The AOF log file records the readability of each executed statement, a feature suitable for emergency recovery from catastrophic deletions. For example, after accidentally executing flushall, it empties all the data, copies the AOF file, and deletes the command that last executed flushall. After putting back the AOF file, it can restore all the data through the automatic recovery mechanism.
  • AOF log files are typically larger than RDB data snapshot files for the same piece of data;
  • When AOF is turned on, fsync is performed every second, so Redis writes to a lower QPS than RDB writes.