Respect all voices but only be yourself first praise again see form a habit first time to summarize the way to write an article wrong place please point out thank youCopy the code
start
The characteristics of redis
Let’s start with the characteristics of Redis
- Redis processes requests in a single-threaded mode. This is done for two reasons: one is because of non-blocking asynchronous event handling; The other is that the cache data is memory operation IO time is not too long, single thread can avoid the cost of thread context switch.
- Redis supports persistence, so Redis can be used not only as a cache, but also as a NoSQL database.
- Redis also has the advantage of supporting multiple data formats in addition to K-V, such as list, set, sorted set, hash, etc.
- Redis provides the master/slave synchronization mechanism and Cluster deployment capability to provide high availability services.
I believe you are familiar with these definitions. When the interviewer asks why your company chooses Redis, you can start from the characteristics. In addition, you can also learn about the characteristics of Memcache Memcached took their pros and cons into account and chose Redis.
Redis data type
What are the data types of Redis?
String Hash List Set SortedSet
(These five data structures are known to work best.)
If you want to stand out in an interview, you can mention redis’s special data structures: HyperLogLog, Geo, Pub/Sub. If you want to add bonus points, you can also play with Redis Module, such as BloomFilter, RedisSearch, Redis-ML
How does Redis implement message queuing
There are many ways to implement message queue, such as ActiveMQ,RabbitMQ,Kafka, etc., but it can also be implemented based on Redis, which can reduce the maintenance cost and implementation complexity of the system
List lpush-brPOP (rpush-blPOP)
Generally, we use the list structure as a queue, we use Rpush as a producer, and we use LPOP as a consumer. When there is no message, THE LPOP will poll empty and consume resources. At this point, the list has a BLPOP that goes to sleep when there is no data in the queue until the data comes in. Message latency is almost zero and negligible.
Note: There is also a problem with idle connections. If the thread is blocked, the connection to the Redis client becomes idle. If the connection is idle for too long, the server will automatically disconnect from the connection to reduce idle resources. If an exception is caught, there is a retry.
2. PUB/SUB, subscribe/publish mode
Implement a 1:N message queue using PUB/SUB
3. Implementation based on Sorted-Set
With sortedSet, time stamp is used as score, message content is used as key to call Zadd to produce messages, and consumers use ZrangeByScore command to obtain data polling N seconds ago for processing.
Here’s why a lot of people say don’t use Redis for message queues. I often hear a lot of discussion about the appropriateness of using Redis as a queue. Some people agree, arguing that Redis is lightweight and convenient to use as a queue. Others object, arguing that Redis will lose data and is better off using professional queue middleware. What I want to say is that if your business scenario is simple enough, not sensitive to data loss, and the probability of message backlogs is low, it is perfectly fine to use Redis as a queue. Redis is also lighter to deploy and operate than Kafka and RabbitMQ. If your business scenario is very sensitive to data loss and writes are very heavy and messages backlogs consume a lot of machine resources, THEN I recommend using professional message queue middleware.
Redis Persistence (high frequency)
If you are asked about redis persistence in an interview, you can say that RDB and AOF should be grasped when considering their advantages and disadvantages
RDB
So let’s talk about this RDB
advantages
It will generate multiple data files, and each data file represents the data in Redis at a certain time. RDB has very little impact on Redis performance, because it only forks a sub-process to do persistence when synchronizing data, and it is faster than AOF in data recovery.
disadvantages
The DB is a snapshot file and is generated every five minutes or more by default, which means that all data in the five minutes between one synchronization and the next is likely to be lost. AOF can lose one second of data at most. If the file is too large, the client may pause for a few milliseconds or even a few seconds. When your company is making a splashkill, it forks a child process to create a large snapshot. This is inevitable. If the hot products such as huawei mate40, iphone12 that the consequences for the company is believed to be very serious
Let’s talk about AOF
advantages
As mentioned, RDB takes snapshots every five minutes, but AOF takes snapshots every second through a background thread called fsync, which loses at most one second of data. AOF writes to log files in appends-only mode. It only writes to data in append mode, which naturally reduces the overhead of disk addressing. Write performance is fast, and files are not easy to break. (Add that aOF actually loses data for up to 2 seconds due to the rewriting of AOF)
AOF logs are recorded in a way called very readable, which makes them suitable for emergency recovery in case of catastrophic data deletion. For example, the company’s interns flushes all data in flushall, so you can make a copy of the AOF log file as soon as the backend rewrite doesn’t happen. Delete the last flushall command and fire the intern.
disadvantages
The same data, AOF file is larger than RDB. When AOF is enabled, it will need to refresh the log asynchronously every second. Therefore, redis will support lower QPS than RDB
If at this time after you talk to the interviewer about RDB and AOF the interviewer asks you how do you choose between the two
If you use RDB alone, you will lose a lot of data. If you use AOF alone, you will lose a lot of data. If you use AOF alone, your data recovery is not as fast as RDB. RDB/AOF together, is the Internet era of a high robust system king.
RDB does mirrored full persistence and AOF does incremental persistence. Because RDB takes a long time and is not real-time enough, a large amount of data will be lost during downtime, so AOF is needed to cooperate with RDB. When the Redis instance is restarted, the memory is rebuilt using the RDB persistence file, and the AOF is used to replay the recent operation instructions to achieve a full restoration of the pre-restart state.
RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB However, the mechanism of Redis itself is that when AOF persistence is enabled and AOF files exist, AOF files are loaded first. When AOF is closed or the AOF file does not exist, load the RDB file. After loading AOF/RDB file city, Redis starts successfully. Redis fails to start and prints error messages when there are errors in the AOF/RDB file
Redis master-slave Synchronization (high frequency)
How to synchronize data between master and slave
This is closely related to the RDB and AOF of data persistence that I mentioned earlier. Let me first say why we use master-slave architecture mode. As mentioned above, QPS on a single machine has a ceiling, and Redis is a feature that must support high read concurrency, so you can read and write on a single machine, who can stand this, not a person! But wouldn’t it be much better if you let the master do the writing, synchronize the data to the other slaves, and they all read it, distributing a lot of requests, and you can easily scale it horizontally when you expand it.
Anyway, how do they synchronize their data?
When you start a slave, it sends a psync command to the master. If the slave connects to the master for the first time, it triggers a full copy. The master will start a thread, generate a snapshot of the RDB, and cache all the new write requests in memory. When the RDB file is generated, the master will send the RDB file to the slave. The first thing the slave does when it gets the RDB file is write it to its local disk and load it into memory. The master then sends the new names cached in memory to the slave.
Redis elimination strategy
Periodically delete
Regular good understanding, default 100ms randomly selected some set expiration time key, to check whether expired, expired delete.
Why not scan all keys with expiration dates?
If all keys in Redis have an expiration date, scan them. That’s horrible, and we almost always set an expiration date online. Full table scan is the same as when you go to check the database without where condition not walk index full table scan, 100ms once, Redis tired to death.
Lazy to delete
Inert delete, see the name know meaning, inert, I do not take the initiative to delete, I am lazy, I wait for you to query I see you expired, expired on the deletion do not return to you, did not expire how how.
Finally is if of if, regular did not delete, I also did not query, that can do?
Memory flushing mechanism
The official website to the memory elimination mechanism is the following:
- Memory limit is reached and the client tries to execute commands that will allow more memory to be used (most write instructions, but DEL and a few exceptions)
- Allkeys-lru: Attempts to reclaim the least-used key (LRU) to make room for newly added data.
- Volatile – LRU: Attempts to reclaim the least-used key (LRU), but only the key in the expired set, so that newly added data has space to store.
- Allkeys-random: Retrieves random keys to make room for newly added data.
- Volatile -random: Retrievals random keys to make room for newly added data, but only for keys in expired collections.
- Volatile – TTL: Retrievals the keys in the expired set, and prioritized the keys with a shorter TTL to make room for newly added data.
- The policies volatile- LRU, volatile- Random, and volatile- TTL are designed to be like noeviction if no key satisfies reclamation prerequisites.
As for LRU words in the real interview will ask you to write the LUR algorithm, you can not do the original one, that really TM, write not finish we are familiar with LinkedHashMap also implemented LRU algorithm to find a data structure to implement the Java version of LRU is relatively easy, know what principle is good. And here’s LinkedHashMap for example
The last
This handsome first time to write an article to record about today to say that the above write is this handsome saw countless documents through countless articles finally summed up the problem let me benefit from a writer [Ao Bing] if interested in children’s shoes can pay attention to go a waveJuejin. Cn/user / 440649…We all work together to improve another code word is really difficult
In the end of the last we have where do not understand can be in the following comments to see the reply I said is not correct or not comprehensive hope that we point out common progress