Why is Redis so fast?
1. Pure memory operation 2. Single thread operation, avoiding frequent context switching 3. Non-blocking I/O multiplexing mechanism
Second, what are the problems in the use of Redis?
1. Cache and database double write consistency problem 2. Cache avalanche problem 3
Solution to REDis problem:
1. Solution to cache and database double-write consistency problem
The premise is that the data has strong consistency requirements, can not slow down the storage; Can only reduce the probability of the occurrence of inconsistency, can not completely avoid; Only final consistency is guaranteed. 1) Adopt the correct update policy, update the database first, then delete the cache. 2) If the problem of deleting the cache may exist, provide a compensation measure, such as using message queues.
2. Cache Avalanche solution
(large concurrent project, traffic in millions) – Use mutex: it will reduce throughput – add expiration time to cache: random value to avoid collective invalidation – double cache
Cache penetration solution
(Large concurrent projects, traffic in millions) – use mutex – use asynchronous update strategy, return Key directly regardless of whether the value is taken – provide an interception mechanism to quickly determine whether the request is valid or not (Bloom filter)
4. Cache concurrency competition solution
– When no order is required, prepare a distributed lock, grab the lock at the same time, and then in the set operation. – When ordering is required, a timestamp needs to be saved when data is written to the database. – Use queues to make set operations serial access.
Redis delete mechanism + memory elimination mechanism:
Adopt periodic deletion + lazy deletion strategy; If a Key is not deleted during a regular delete, and a Key is not requested in a timely manner (lazy delete does not take effect), redis memory will become higher and higher. A memory flush mechanism should be used at this point.
Redis offers six data elimination strategies: Noeviction Allkeys – LRU: When memory is insufficient to accommodate newly written data, Removes the least recently used Key from the Key space.(recommended) allkeys-random volatile-lru volatile-random volatile- TTL
Reference materials: 1, Redis getting started guide 2, Redis design and implementation 3, Redis field case directory
— — the End — —