Recently, when standardizing the use of platform cache, we found that many businesses use reids distributed locks, but there are some common detailed problems. Based on these problems, this article will try to summarize common problems with distributed locks.
If it is a stand-alone environment, for concurrency issues, you can directly use the synchronized or Lock provided by java to implement it. If it involves a multi-process environment, then you need to rely on A third-party system provides the locking mechanism. (Recommended learning: Redis video tutorial)
As a caching middleware system, redis can provide this kind of distributed (cluster) lock mechanism. Its essence is Occupying a pit in redis, when other processes want to occupy the pit, and find that it has been occupied, just wait and try again later.
We generally use it like this in java:
boolean result = jedis.setnx("lock-key",String.valueOf(System.currentTimeMillis()))== 1L; if (result) { try { // do something } finally { jedis.del("lock-key"); } }
For more Redis-related technical articles, please visit Getting Started Tutorial on Using Redis Database Column for learning!
The above is the detailed content of When to use redis cluster lock. For more information, please follow other related articles on the PHP Chinese website!