In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail what are the interview questions about distributed cache in Redis. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Interview questions
What's the difference between redis and memcached? What is the threading model of redis? Why does redis single thread support high concurrency?
Psychological analysis of interviewer
This is the most basic question when asking redis. One of the most basic internal principles and characteristics of redis is that redis is actually a single-threaded working model. If you don't know this, don't you know anything when something goes wrong with redis?
It's also possible that the interviewer will ask you the difference between redis and memcached, but memcached is a common caching scheme used by Internet companies in the early years, but now it's mostly redis, and few companies use memcached anymore.
Analysis of interview questions
What's the difference between redis and memcached?
Redis supports complex data structures
Compared with memcached, redis has more data structures and can support richer data operations. If you need caching to support more complex structures and operations, redis would be a good choice.
Redis natively supports cluster mode
In the redis3.x version, cluster mode is supported, while memcached does not have the native cluster mode, so you need to rely on the client to write data to the cluster.
Performance comparison
Because redis uses only a single core, while memcached can use multiple cores, on average, redis performs better than memcached in storing small data on each core. In the data above 100k, the performance of memcached is higher than that of redis. Although redis has recently optimized the performance of storing big data, it is still slightly inferior to memcached.
The threading model of redis
Redis internally uses the file event handler file event handler, which is single-threaded, so redis is called a single-threaded model. It uses IO multiplexing mechanism to monitor multiple socket at the same time, presses the event-generating socket into the memory queue, and the event dispatcher selects the corresponding event processor to process according to the event type on the socket.
The structure of the file event handler consists of four parts:
Multiple socket
IO multiplexing program
File event dispatcher
Event handler (connection reply processor, command request processor, command reply processor)
Multiple socket may produce different operations concurrently, and each operation corresponds to a different file event, but the IO multiplexer listens to multiple socket and queues the socket that generated the event. The event dispatcher takes one socket from the queue at a time and gives it to the corresponding event processor for processing according to the event type of the socket.
Let's look at a communication process between the client and redis:
To understand, communication is done through socket, students who do not understand can first take a look at socket network programming.
First, when the redis server process initializes, it associates the AE_READABLE event of the server socket with the connection reply processor.
When the client socket01 requests the server socket of the redis process to establish a connection, the server socket generates an AE_READABLE event. After listening to the event generated by the server socket, the IO multiplexer presses the socket into the queue. The file event dispatcher takes the socket from the queue and gives it to the connection reply processor. The connection reply processor creates a socket01 that can communicate with the client and associates the AE_READABLE event of that socket01 with the command request processor.
Suppose that the client sends a set key value request, and the socket01 in the redis generates an AE_READABLE event, and the IO multiplexer presses the socket01 into the queue, and the event dispatcher obtains the AE_READABLE event generated by the socket01 from the queue. Because the previous AE_READABLE event of the socket01 has been associated with the command request processor, the event dispatcher hands the event to the command request processor for processing. The command requests the processor to read the key value of socket01 and complete the setting of key value in its own memory. When the operation is complete, it associates the AE_WRITABLE event of socket01 with the command reply processor.
If the client is ready to receive the return result at this time, the socket01 in redis will generate an AE_WRITABLE event, which is also pressed into the queue, and the event dispatcher finds the associated command reply processor, and the command reply processor inputs a result of this operation to the socket01, such as ok, and then disassociates the AE_WRITABLE event of socket01 with the command reply processor.
This completes a communication. About a communication process of Redis, readers are recommended to read "Design and implementation of Redis-Huang Jianhong" for systematic study.
Why is the redis single-threaded model so efficient?
Pure memory operation.
The core is the IO multiplexing mechanism based on non-blocking.
C language implementation, generally speaking, C language implementation of the program "closer to" the operating system, the execution speed will be relatively faster.
On the contrary, single thread avoids the problem of frequent context switching of multi-thread and prevents the competition problem that may be caused by multi-thread.
Interview questions
What is the concurrency contention problem with edis? How to solve this problem? Do you know the CAS scheme for redis transactions?
Psychological analysis of interviewer
This is also a very common problem online, that is, when multiple clients write a key at the same time, the data that should have arrived first may arrive later, resulting in the wrong version of the data; or multiple clients may obtain a key at the same time, modify the value and then write it back, as long as the order is wrong, the data is wrong.
And redis itself has an optimistic locking scheme for the CAS class that naturally solves this problem.
Analysis of interview questions
At some point, multiple system instances update a key. Distributed locks can be implemented based on zookeeper. Each system acquires distributed locks through zookeeper to ensure that only one instance of the system is operating on a key at a time, and no one else is allowed to read or write.
The data you want to write to the cache is checked from mysql and has to be written to mysql. When you write to mysql, you must save a timestamp, and when you check it out from mysql, the timestamp will also be found.
Before each write, check whether the timestamp of the current value is newer than the timestamp of the value in the cache. If so, you can write, otherwise, you cannot overwrite the new data with the old data.
This is the end of this article on "what are the interview questions about distributed cache in Redis?". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.