This is the 16th day of my participation in Gwen Challenge

What is Redis?

A fast, concurrent, non-relational database is an open source key-value pair storage database written in ANSI C that supports multiple data structures and optional persistence.

Features:

  • C/S communication model
  • Single process single thread model
  • Rich data types
  • The operation is atomic
  • persistence
  • High concurrent read and write
  • Support for Lua scripts

What scenarios apply to Redis?

Hot data stores (read more than write), counters, message queue systems, temporary data tokens.

The main data types?

String, hash, List, Set, and sequential collection types.

Why redis?

Improve access efficiency. For some data that do not change often, or use for a very short time, you can add it to the cache. When you get it, you can judge whether there is data in the cache first. If there is data, you can get it directly from the cache.

Understand redis penetration, breakdown and avalanche?
Redis penetration

This can be viewed as a performance problem, if it doesn’t happen, it crashes.

It means that the client keeps making requests to the server for data that does not exist on the server. The customer first queries in Redis, and then queries in the database.

For example, if a hacker attacks Redis and sends 1000 requests, but the key value of these 1000 requests does not exist, the query will be put into the database, which will occur penetration.

How to solve it?

Redis is a single thread, even in the high concurrency is also a a solution, when the first request came in, found that the key does not exist, this time with multithreading, checked a database, using distributed setnx key lock, and set the expiration date of the lock, another thread to monitor the lock, if the lock failure time, haven’t get the data, Then delay the expiration of the lock. Then, if the data is found, it is inserted into Redis, and when the lock expires, it is entered in a request.

Key words: distributed lock, expiration time, multithreading.

Redis breakdown

Breakdown is like the balance between penetration and avalanche. It means that there is a hot key that is very popular, and everyone likes to visit it. When the concurrency is high, a large number of users request this key, and the key fails, so a large number of requests will fall on the database, and the database will be broken down.

How to solve this situation, of course, want the hot key to be permanent best

Key words: Bloom filter.

Redis avalanche

A large number of redis data failed at the same time, resulting in a large number of access to the DB.

How to solve it?

To avoid cache set failures, set different timeout periods for different keys.

Rely heavily on penetration solutions, add mutex, and control database requests.

Key words: different timeout, mutex