<>redis High concurrency processing mode of

actually redis There is no concurrency problem , Because it's a single process , Any number of commands are executed one by one . When we use it , Concurrency problems can occur , For example, get and set this pair .Redis Why
There are high concurrency problems ?
Redis The birth of

Redis Is a single thread mechanism nosql database , be based on key-value, Data can be persistent . Because of single thread, so redis There is no concept of lock in itself , Multiple client connections do not compete , But use it jedis Wait for the client to redis There are problems with concurrent access . Connection timeout occurred , Data conversion error , block , The client closes the connection and so on , These problems are caused by the confusion of client connection .

meanwhile , The nature of single thread , Highly concurrent operations on the same key are queued , If the concurrency is large , May cause subsequent requests to time out .

On remote access redis When , Because of network and other reasons, the problem of high concurrent access delay return is caused .

<> solve :

1. Pool connections on the client side , Read and write to the client at the same time Redis Internal lock is used for operation synchronized.
Solutions : take Redis Connection pooling

first ,Redis It also belongs to database generalization , Even if it is NoSQL type , Still for C/S Structural model . The client needs to establish a database connection every time it requests , In the multi client request mode, frequent connection between the server and the client will lead to series blocking , Timeout and so on . The friends who have studied relational database also know , The solution of relational database is to use connection pool to solve the problem of multi request connection .
same ,Redis The same is true for databases , Establish a friendly connection number, so that the client and server maintain a certain amount of connection , When the client needs to connect ,
The connection can be obtained directly from the connection pool , And then go directly to Redis Server .

2. Server angle , utilize setnx Variable direction locking mechanism .
setnx There are two parameters . The first parameter represents the key . The second parameter table value if the current key does not exist , The current key is inserted , Take the second parameter as the value . return 1. If the current key exists , Then it will return 0.

Technology