Pros and cons of Redis

1. Why use Redis?

 

Fast speed, completely based on memory, implemented in C language, network layer uses epoll to solve high concurrency problems, single-threaded model avoids unnecessary context switching and competition conditions;

Note: Single thread only means that one thread is used to process the client's request on the network request module. If it is persisted, a thread/process will be reopened for processing.

Rich data types. Redis has 8 data types. Of course, the five types commonly used are String, Hash, List, Set, and SortSet. They all organize data based on key values. Each data type provides a very rich operation commands, which can meet most of the needs. If you have special needs, you can create new commands yourself through the lua script (atomic);

Disadvantage

  • Since Redis is an in-memory database, a large amount of data increase in a short period of time may cause insufficient memory.
  • Redis is single-threaded, a single server cannot make full use of the CPU of a multi-core server

 

3. Why is Redis fast?

1. Based entirely on memory, most of the requests are pure memory operations, very fast.

2. The data structure is simple, and the data operation is also simple. The data structure in Redis is specially designed;

3. Single thread is used to avoid unnecessary context switching and competition conditions, and there is no switching caused by multi-process or multi-thread to consume CPU;

4. Use multi-channel I/O multiplexing model, non-blocking IO;

5. The underlying model is different, the underlying implementation between them and the application protocol for communication with the client are different. Redis directly builds the VM mechanism by itself, because the general system calls system functions, it will waste a certain amount of time to move And request;
4. Data types supported by Redis:

String ; Hash ; List ; Set ; SortedSet .

5. Redis expiration strategy and memory elimination mechanism?

1. Periodic deletion + lazy deletion

Periodic deletion: It means that by default, Redis randomly extracts some keys with an expiration time set every 100ms, checks whether they expire, and deletes them if they expire.

Lazy deletion: When you get a key, redis will check whether the key has expired if the expiration time is set. If it expires, it will be deleted at this time, and nothing will be returned to you.

2. What should I do if a large number of expired keys are accumulated in the memory, causing the redis memory block to be exhausted?

Memory elimination mechanism:

Configuration in redis.conf:

1# maxmemory-policy noeviction  

noeviction: When memory usage reaches the threshold, all commands that cause memory application will report errors.

allkeys-lru: In the primary key space, preferentially remove keys that have not been used recently.

Volatile-lru: In the key space with the expiration time set, the key that has not been used recently is first removed.

allkeys-random: Randomly remove a key in the primary key space.

Volatile-random: Randomly remove a key in the key space with expiration time set.

volatile-ttl: In the key space with an expiration time set, the key with an earlier expiration time is removed first.

Guess you like

Origin blog.csdn.net/qq_27828675/article/details/109348232