Application Scenario
In monolithic deployment, you can use Java concurrency APIs such as ReentrantLock or synchronized to implement mutual exclusion locks. This native lock mechanism provided by Java ensures that multiple threads within a Java VM process are executed concurrently and sequentially.
However, this mechanism may fail in multi-node deployment because a node's lock only takes effect on threads in the Java VM where the node runs. For example, the concurrency level in Internet seckills requires multiple nodes to run at the same time. Assume that requests of two users arrive simultaneously on two nodes. Although the requests can be processed simultaneously on the respective nodes, an inventory oversold problem may still occur because the nodes use different locks.
Solution
To serialize access to resources, ensure that all nodes use the same lock. This requires a distributed lock.
The idea of a distributed lock is to provide a globally unique "thing" for different systems to allocate locks. When a system needs a lock, it asks the "thing" for a lock. In this way, different systems can obtain the same lock.
Currently, a distributed lock can be implemented using cache databases, disk databases, or ZooKeeper.
Implementing distributed locks using DCS Redis instances has the following advantages:
Implementing locks on distributed applications can avoid inventory oversold problems and nonsequential access. The following describes how to implement locks on distributed applications with Redis.
By default, networks in a VPC can communicate with each other.
If the client and DCS Redis instance are not in the same VPC, connect them by establishing a VPC peering connection. For details, see "Does DCS Support Cross-VPC Access?" in Distributed Cache Service User Guide > FAQs.
The development tools and clients mentioned in this document are for example only.
Sample code of DistributedLock.java:
package dcsDemo01;import java.util.UUID;import redis.clients.jedis.Jedis;import redis.clients.jedis.params.SetParams;public class DistributedLock {// Address and port for connecting to the Redis instance. Replace them with the actual values.private final String host = " 192.168.0.220 ";private final int port = 6379 ;private static final String SUCCESS = "OK";public DistributedLock(){}/** @param lockName Lock name* @param timeout Timeout for acquiring locks* @param lockTimeout Validity period of locks* @return Lock ID*/public String getLockWithTimeout(String lockName, long timeout, long lockTimeout) {String ret = null;Jedis jedisClient = new Jedis(host, port);try {// Password for connecting to the Redis instance. Replace it with the actual value.String authMsg = jedisClient.auth(" passwd ");if (!SUCCESS.equals(authMsg)) {System.out.println("AUTH FAILED: " + authMsg);}String identifier = UUID.randomUUID().toString();String lockKey = "DLock:" + lockName;long end = System.currentTimeMillis() + timeout;SetParams setParams = new SetParams();setParams.nx().px(lockTimeout);while(System.currentTimeMillis() < end) {String result = jedisClient.set(lockKey, identifier, setParams);if(SUCCESS.equals(result)) {ret = identifier;break;}try {Thread.sleep(2);} catch (InterruptedException e) {Thread.currentThread().interrupt();}}}catch (Exception e) {e.printStackTrace();}finally {jedisClient.quit();jedisClient.close();}return ret;}/** @param lockName Lock name* @param identifier Lock ID*/public void releaseLock(String lockName, String identifier) {Jedis jedisClient = new Jedis(host, port);try {String authMsg = jedisClient.auth("passwd");if (!SUCCESS.equals(authMsg)) {System.out.println("AUTH FAILED: " + authMsg);}String lockKey = "DLock:" + lockName;if(identifier.equals(jedisClient.get(lockKey))) {jedisClient.del(lockKey);}}catch (Exception e) {e.printStackTrace();}finally {jedisClient.quit();jedisClient.close();}}}
The code only shows how DCS implements access control using locks. During actual implementation, deadlock and lock check also need to be considered.
Assume that 20 threads are used to seckill ten Mate 10 mobile phones. The content of the test class CaseTest.java is as follows:
In DistributedLock.java, set host and port to the connection address and port number of the instance. In the getLockWithTimeout and releaseLock methods, set passwd to the instance access password.
//The lock code is commented out in the test class:public void handleOrder() {String userName = UUID.randomUUID().toString().substring(0,8) + Thread.currentThread().getName();//Lock code//String identifier = DLock.getLockWithTimeout("Mate 10", 10000, 2000);System.out.println("Processing order for user " + userName + "");if(n > 0) {int num = MAX - n + 1;System.out.println("User "+ userName + " is allocated number " + num + " mobile phone. Number of mobile phones left: " + (--n) + "");}else {System.out.println("User "+ userName + " order failed.");}//Lock code//DLock.releaseLock("Mate 10", identifier);}
Processing order for user e04934ddThread-5Processing order for user a4554180Thread-0User a4554180Thread-0 is allocated number 2 mobile phone. Number of mobile phones left: 8.Processing order for user b58eb811Thread-10User b58eb811Thread-10 is allocated number 3 mobile phone. Number of mobile phones left: 7.Processing order for user e8391c0eThread-19Processing order for user 21fd133aThread-13Processing order for user 1dd04ff4Thread-6User 1dd04ff4Thread-6 is allocated number 6 mobile phone. Number of mobile phones left: 4.Processing order for user e5977112Thread-3Processing order for user 4d7a8a2bThread-4User e5977112Thread-3 is allocated number 7 mobile phone. Number of mobile phones left: 3.Processing order for user 18967410Thread-15User 18967410Thread-15 is allocated number 9 mobile phone. Number of mobile phones left: 1.Processing order for user e4f51568Thread-14User 21fd133aThread-13 is allocated number 5 mobile phone. Number of mobile phones left: 5.User e8391c0eThread-19 is allocated number 4 mobile phone. Number of mobile phones left: 6.Processing order for user d895d3f1Thread-12User d895d3f1Thread-12 order failed.Processing order for user 7b8d2526Thread-11User 7b8d2526Thread-11 order failed.Processing order for user d7ca1779Thread-8User d7ca1779Thread-8 order failed.Processing order for user 74fca0ecThread-1User 74fca0ecThread-1 order failed.User e04934ddThread-5 is allocated number 1 mobile phone. Number of mobile phones left: 9.User e4f51568Thread-14 is allocated number 10 mobile phone. Number of mobile phones left: 0.Processing order for user aae76a83Thread-7User aae76a83Thread-7 order failed.Processing order for user c638d2cfThread-2User c638d2cfThread-2 order failed.Processing order for user 2de29a4eThread-17User 2de29a4eThread-17 order failed.Processing order for user 40a46ba0Thread-18User 40a46ba0Thread-18 order failed.Processing order for user 211fd9c7Thread-9User 211fd9c7Thread-9 order failed.Processing order for user 911b83fcThread-16User 911b83fcThread-16 order failed.User 4d7a8a2bThread-4 is allocated number 8 mobile phone. Number of mobile phones left: 2.
Processing order for user eee56fb7Thread-16User eee56fb7Thread-16 is allocated number 1 mobile phone. Number of mobile phones left: 9.Processing order for user d6521816Thread-2User d6521816Thread-2 is allocated number 2 mobile phone. Number of mobile phones left: 8.Processing order for user d7b3b983Thread-19User d7b3b983Thread-19 is allocated number 3 mobile phone. Number of mobile phones left: 7.Processing order for user 36a6b97aThread-15User 36a6b97aThread-15 is allocated number 4 mobile phone. Number of mobile phones left: 6.Processing order for user 9a973456Thread-1User 9a973456Thread-1 is allocated number 5 mobile phone. Number of mobile phones left: 5.Processing order for user 03f1de9aThread-14User 03f1de9aThread-14 is allocated number 6 mobile phone. Number of mobile phones left: 4.Processing order for user 2c315ee6Thread-11User 2c315ee6Thread-11 is allocated number 7 mobile phone. Number of mobile phones left: 3.Processing order for user 2b03b7c0Thread-12User 2b03b7c0Thread-12 is allocated number 8 mobile phone. Number of mobile phones left: 2.Processing order for user 75f25749Thread-0User 75f25749Thread-0 is allocated number 9 mobile phone. Number of mobile phones left: 1.Processing order for user 26c71db5Thread-18User 26c71db5Thread-18 is allocated number 10 mobile phone. Number of mobile phones left: 0.Processing order for user c32654dbThread-17User c32654dbThread-17 order failed.Processing order for user df94370aThread-7User df94370aThread-7 order failed.Processing order for user 0af94cddThread-5User 0af94cddThread-5 order failed.Processing order for user e52428a4Thread-13User e52428a4Thread-13 order failed.Processing order for user 46f91208Thread-10User 46f91208Thread-10 order failed.Processing order for user e0ca87bbThread-9User e0ca87bbThread-9 order failed.Processing order for user f385af9aThread-8User f385af9aThread-8 order failed.Processing order for user 46c5f498Thread-6User 46c5f498Thread-6 order failed.Processing order for user 935e0f50Thread-3User 935e0f50Thread-3 order failed.Processing order for user d3eaae29Thread-4User d3eaae29Thread-4 order failed.