Home > Database > Redis > When to use redis cluster lock

When to use redis cluster lock

(*-*)浩
Release: 2019-06-17 10:28:22
Original
2378 people have browsed it

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.

When to use redis cluster lock

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");
    }
 }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template