A fan who has worked for 5 years sent me a private message.
He said that he had been preparing for half a year, thinking like an ant financial suit, but the first interview was hung up, and he was very sad.
The question is: "Is there a thread safety issue with Redis?"
On this question, look at the answers of ordinary people and experts.
Ordinary people:
Ok. . . . . . . . . . . .
Expert:
OK, I will answer this question in two ways.
The first one, from the Redis server level.
Redis Server itself is a thread-safe KV database, that is to say, the instructions executed on Redis Server do not require any synchronization mechanism, and there is no thread safety problem.
Although the multi-threading model has been added in Redis 6.0, the added multi-threading is only used to process network IO events. The execution process of instructions is still handled by the main thread, so there will not be multiple threads to notify the execution of operation instructions. Case.
I think there are several reasons why Redis does not use multi-threading to execute instructions.
- The possible performance bottlenecks of Redis Server itself are nothing more than network IO, CPU, and memory. But the CPU is not the bottleneck of Redis, so there is no need to use multiple threads to execute instructions.
- If multi-threading is used, it means that thread safety must be considered for all instruction operations of redis, that is to say, it needs to be solved by locking, and the performance impact brought by this method is even greater.
The second, from the Redis client level.
Although the execution of instructions in Redis Server is atomic, if there are multiple Redis clients executing multiple instructions at the same time, atomicity cannot be guaranteed.
Assume that two redis clients obtain key1 on the Redis Server at the same time, modify and write at the same time, because the atomicity in the multi-threaded environment cannot be guaranteed, and the contention of shared resource access in the multi-process situation makes the data security. cannot be guaranteed.
Of course, there are many solutions to the thread safety problem at the client level, such as using the atomic instructions in Redis as much as possible, or locking the resource access of multiple clients, or implementing the operation of multiple instructions through Lua scripts and many more.
The above is my understanding of the problem.
Summarize
Regarding thread safety, it is a very important, very important knowledge.
Although we rarely actively use threads in actual development, threads are ubiquitous in projects. For example, Tomcat uses multiple threads to process requests.
If you don't understand thread safety, you are likely to have various production accidents and inexplicable problems.
This is also the reason why big manufacturers will definitely ask about multi-threaded concurrency.
If you need a master interview document (with a 100,000-word interview document inside Ali) or have any technical interview questions you don’t understand and want to consult, you can send a private message or follow the public account of the same name.
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source forMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。