Recent functional requirements, using Redis as a database, after the launch of an individual player has a baffling error, the inserted data is not inserted, inexplicably lost. Finally, it is possible to understand the concurrency problem by analyzing the user's error data. The first contact concurrency is when looking at the operating system principle, Then look at the database principle of the time also seen concurrency, the principle of concurrency is still relatively clear, but has not encountered. This time in the process of using Reids is a step in the pit.
Today's computers are mostly multi-core CPUs, which means that multiple processes can be executed in parallel. If the multiple running processes read and write to the same data, it is possible that two or more processes are reading the old data, in which case some process-written data is overwritten after the write operation. , resulting in a final error. This data is a critical section for these processes.
Processing concurrency issues under Redis.
1. Lock mechanism is used to handle concurrency in the operating system as well as in the database by using SETNX, although locking can solve concurrency problems, but it reduces the amount of concurrency, so they will reduce the granularity of locks by reading and writing locks.
Locking is actually the way to change parallel reading and writing to serial read and write to avoid resource competition
$redis = new Redis (), $redis->connect (' 127.0.0.1 ', 6370), if (! $redis->setnx (' Lock ', 1)) {usleep (500000);//Wait a while if (! $redis->setnx (' Lock ', 1)) {exit (); }}redis->expireat (' Lock ', 2); Set an expiration time to prevent the process from hanging up causing the lock to not release//Business processing $redis->del (' lock ');
2.watch + things, redis things cannot be rolled back automatically, so in case of failure, the rollback operation is handled. If there are multiple updates in a thing, the rollback operation can be cumbersome,
$redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6370); $redis->watch (' Test '); Must be Watch$redis->hgetall (' test ')//Business processing $result = $redis->multi () before reading->hse T ()->exec ();
3. Reduce the granularity of write data or modify data structure to avoid concurrency, our business uses the Hset way, the user's data are put into a filed, which causes a change to write to the user all the data, by modifying
With hmset, updating data only updates the data that needs to be updated, reducing the granularity of writes to reduce read and write access to the critical section of each interface. This approach may avoid access to the critical section of some interfaces, and the unavoidable interface requires additional
Processing.
4. In the case of excessive concurrency, can be processed through the message middleware, parallel read and write serialization. This approach is a common solution in some high concurrency scenarios, and the simple way to do this is through the Redis list.
In large-scale software, it is necessary to introduce a special message middle layer to deal with.
Solutions for concurrency problems under Redis