Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of realizing distributed transaction by redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the method of redis to achieve distributed transactions, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Problem description:

An e-commerce platform, the first launch of a new mobile phone, each person is limited to 2 sets, is expected to have 10W concurrency, in this case, if the inventory is deducted, it is guaranteed that it will not be oversold

Solution one

Use the database locking mechanism to lock the record and then operate it.

SELECT * from goods where ID = 1 for update;UPDATE goods set stock = stock-1

Using exclusive lock to convert parallel to serial operation, but the performance and user experience of this scheme are poor.

Solution two

Using redis to realize distributed Lock

Use the setnx command (create and set the value to return 0 when the key does not exist) to acquire the lock. In the business logic, we can operate through this scheme.

Jedis client = jedisPool.getResource (); while (client.setnx ("lock", String.valueOf (System.currentTimeMillis () = = 0) {Thread.sleep (10000);} / coding here client.del ("lock")

Scheme binary order

Considering the deadlock problem, that is, after ready-made An acquires the lock, it goes down, so that the lock can not be released all the time. We can obtain the timestamp of the lock through the get command, judge the timeout through him, and release it.

Long TIMEOUT_SECOUND = 120000L; Jedis client = jedisPool.getResource (); while (client.setnx ("lock", String.valueOf (System.currentTimeMillis () = = 0) {Long lockTime = Long.valueOf (client.get ("lock")); if (locktimed timepieces) {client.del ("lock") } Thread.sleep (10000);} .. Client.del ("lock")

Option 2 strengthening

In the algorithm of option 2, in order to ensure that the lock can only be released by the locked thread in the case of non-timeout, the thread signature can be spelled in the value timestamp.

Long TIMEOUT_SECOUND = 120000L; String featureCode = "machine01"; Jedis client = jedisPool.getResource (); while (client.setnx ("lock", featureCode+ ":" + String.valueOf (System.currentTimeMillis () = 0) {Long lockTime = Long.valueOf (client.get ("lock"). Substring (9)) If {client.del ("lock");} Thread.sleep (10000);}. .. If (featureCode.equals (client.get ("lock"). Substring (0,8)) {client.del ("lock");} this is all about how redis implements distributed transactions. Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report