Redis 5 design and source code analysis

Chapter 1 Introduction

1 The main reason why Redis has high performance

  • Redis is a memory-based storage database, most of the commands are memory operations
  • Redis is a single-process threaded service (in fact, a running Redis Server must have more than one thread, but only one thread to handle network requests)
  • Redis uses multiple I/O multiplexing models (select, poll, epoll), which can efficiently handle a large number of concurrent connections
  • Redis's data structure is specially designed, and operations such as adding, deleting, modifying, and checking are relatively simple

2 The main advantages of Redis

  • Redis is an in-memory database, and Redis key-value pairs are stored in memory
  • The working mode of Redis is single-threaded and does not require synchronization between threads. Redis uses single thread mainly because its bottleneck is in memory and bandwidth, rather than CPU .
  • The value of key-value in Redis has multiple data types, such as string, linked list, collection, etc.
  • Redis supports data persistence and can use RDB, AOF, RDB & AOF to recover data when the computer restarts
  • Redis supports a master-slave structure and can use slave instances for data backup

3 Redis's main source code file

The Redis source code is mainly stored in the src folder, server.c is the server program, and redis-cli.c is the client program.

The core parts of Redis are as follows:

  • Basic data structure : dynamic string (sds.c); integer set (intset.c); compressed list (ziplist.c); quick linked list (quicklist.c); dictionary (dict.c); the underlying implementation structure of Streams ( listpack.c and rax.c)
  • The underlying implementation of Redis data types : Redis object (object.c); string (t_string.c); list (t_list.c); dictionary (t_hash.c); set and ordered set (t_set.c and t_zset.c) ); data stream (t_stream.c)
  • Redis database implementation : the underlying implementation of the database (db.c); persistence (rdb.c and aof.c)
  • Redis server and client implementation : event-driven (ae.c and ae_epoll.c); network connection (anet.c and networking.c); server program (server.c); client program (redis-cli.c) )
  • Others : master-slave replication (replication.c); sentinel (sentinel.c); cluster (cluster.c); other data structures (such as hyperloglog.c, geo.c, etc.); pub/sub, Lua script

4 Executable files in Redis

  • redis-benchmark: The official Redis performance test tool that comes with it
  • redis-check-aof and redis-check-rdb: When there is a syntax error in AOF file or RDB file, you can use these two to repair
  • redis-cli: client command line tool, you can connect to the specified Redis server through the command redis-cli -h {host} -p {port}
  • redis-sentinel: Redis sentinel startup program
  • redis-server: Redis server startup program