Redis is heavily used in distributed environments, and it becomes a problem to solve the locks in a distributed environment. For example, our current hand-travel program, server side is divided by Business module server, there are applications, combat clothing, etc., but these two VMS can also change the player's properties, which if under the same VM, it is easy
Distributed caching, can solve the single server memory can not be unlimited expansion of the bottleneck. In the application of distributed caching, multiple client contention issues are encountered. This time, need to use a distributed lock, the client has access to the lock
exists. If the program can run out of 20s, then the distributed lock can be counted to meet the requirements, so the effect of the test should be the same as in different JVMs (that is, in a real distributed environment), and the following is the code for the test class:
Package cc.lixiaohui.DistributedLock.DistributedLock;
Import Java.util.HashSet;
Impor
Locks are a basic service for many systems, but implementing a lock on a distributed environment is not a simple matter. Fortunately now all kinds of components complete, today we introduce the Redis-based lock Java implementation-redssion. (Redis-based SETNX native implemen
) {//indicates the cache value expires //Set 3min timeout to prevent the Del operation from failing, the next cache expiration cannot load DB if (Redis.setnx (Key_mutex, 1, 3 * 60) = = 1) { //For setting success value = Db.get (key); Redis.set (key, value, expire_secs); Redis.del (Key_mutex); } else { //This time the other threads at the same time are already load db and back to the cache, then retry to get the cach
Implement distributed locks Based on Redis and distributed locks Based on Redis
BackgroundIn many Internet product applications, some scenarios require locking, such as seckilling, global incremental ID, and floor generation. Most solutions are implemented based on databases. Redis
result in inconsistent data read, data expiration and other issues.Use that third-party media to store distributed locks?The current popular is zookeeper and Redis, both have advantages, Redis popular memory cache, and can be horizontal expansion while also improve the request load, in the face of high and distributed
-related APIs, but these APIs are not available in distributed scenarios. This means that simple Java APIs do not provide the ability to distribute locks. So there are many schemes for the implementation of distributed locks at present.
For the implementation of distributed locks, there are several common schemes: implementing
realization way
For the implementation of distributed locks, the following schemes are commonly used at present:
Implementing distributed lock based on database based on cache (Redis,memcached,tair) to realize distributed lock ba
First, Introduction
This blog post shows you how to build a Redis-based distributed lock step at a pace. Will start with the most original version, then adjust according to the problem, and finally complete a more reasonable distributed lock.
This article divides the implem
Redis Lock Structure and redis lockSingle thread and isolation
Redis uses a single thread to execute transactions. transactions are run in a serial manner. That is to say, the execution of a single command in Redis and the execution of a transaction are thread-safe and will
Use Redis to implement locks (Supporting Distributed applications) (organizing network data), redisImplement locks using Redis (supports distributed applications) 1. Introduction
Use Redis commands such as setnx, expire, and getset to access mutex Resources
The content of t
Redis builds distributed locks and redis builds
1. Preface
Why should we build a lock? Because building a proper lock can maintain data consistency in high concurrency, that is, the data locked when the client executes a coherent command is not changed by other clients and a
the code perfect? The answer is almost as yet. Imagine if a request to update the cache for a long time, or even longer than the duration of the lock, resulting in the cache during the update process, the lock is invalidated, the other request will acquire the lock, but the previous request when the cache is updated, if you do not judge to delete the
-related APIs, but these APIs are not available in distributed scenarios. This means that simple Java APIs do not provide the ability to distribute locks. So there are many schemes for the implementation of distributed locks at present.For the implementation of distributed locks, there are several scenarios that are commonly used today:
Implementation of
1. ConceptThe reason of distributed lock: Single Application single-machine deployment environment, in order to solve multi-threaded concurrency problem, we will use REENTRANTLCOK or synchronized to solve mutual exclusion problem, but the business needs, single-machine deployment evolved into a distributed system, in a distri
communication.It's dangerous for everyone to connect to a Redis server, and we need to set a password for Redis on 103.How to set it up, you need to edit the Redis configuration file/etc/redis/6379.conf[Plain]View Plaincopy
[Email protected] utils]# vim/etc/redis/6
EF + Redis (StackExchange. Redis) implements distributed locks, which is feasible in self-testing. stackexchange. redis
E-commerce platforms will have a flash sale, for example, 1 yuan flash sale. The most important thing about flash sales is inventory. In many cases, if the inventory is not properly processed, the ove
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.