In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how Redis uses optimistic locks to ensure data consistency, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.
Scene
There is often a situation in Redis that reads the value of a key, does some business logic processing, and then calculates a new value based on the read value and re-set it.
If client A just reads the key value, and then client B modifies the key value, then there will be concurrency security issues.
Problem simulation
Suppose Redis Server has a key with the key name test, which holds an json array [1, 2, 3].
Let's simulate the situation in which client An and client B access the modification at the same time. The code is as follows:
Client A:
Class RedisClientA (username: String, password: String, host: String, port: Int) {val jedis: Jedis init {val pool = JedisPool (JedisPoolConfig (), host, port) jedis = pool.resource jedis.auth (username, password)} fun update (key: String) {val idStr = jedis.get (key) val idList = Json.decodeFromString (idStr) / / wait 2 seconds Simulation business TimeUnit.SECONDS.sleep (2L) idList.add (4) println ("new id list: $idList") jedis.set (key, Json.encodeToString (idList))} fun getVal (key: String): String? {return jedis.get (key)}} fun main () {val key = "test" val redisClientA = RedisClientA ("default", "123456", "127.0.0.1" 6379) redisClientA.update (key) val res = redisClientA.getVal (key) println ("res: $res")}
Client B:
Class RedisClientB (username: String, password: String, host: String, port: Int) {val jedis: Jedis init {val pool = JedisPool (JedisPoolConfig (), host, port) jedis = pool.resource jedis.auth (username Password)} fun update (key: String) {val idStr = jedis.get (key) val idList = Json.decodeFromString (idStr) idList.add (5) println ("new id list: $idList") jedis.set (key Json.encodeToString (idList)} fun getVal (key: String): String? {return jedis.get (key)}} fun main () {val key = "test" val redisClientB = RedisClientB ("default", "123456", "127.0.0.1", 6379) redisClientB.update (key) val res = redisClientB.getVal (key) println ("res: $res")}
Client A blocked for 2 seconds to simulate the processing of time-consuming business logic. While processing, client B accesses "test" and adds id:5.
When client A's time-consuming business logic is finished, id:4 is added and id:5 is overwritten.
In the end, the content of "test" is as follows:
CAS to ensure data consistency
The WATCH command provides check-and-set (CAS) behavior for Redis transactions. Keys that are subject to WATCH will be monitored and will see if they have been changed. If at least one monitored build is modified before EXEC execution, the entire transaction is cancelled and EXEC returns Null replay to indicate that the transaction failed. We just need to repeat the operation and hope that there will be no new competition during this period of time. This form of lock is called optimistic lock, and it is a very powerful locking mechanism.
So how to implement the CAS approach? We just need to modify the code in the update () method of RedisClientA as follows:
Fun update (key: String) {var flag = true while (flag) {jedis.watch (key) val idStr = jedis.get (key) val idList = Json.decodeFromString (idStr) / / wait 2 seconds Simulation business TimeUnit.SECONDS.sleep (2L) val transaction = jedis.multi () idList.add (4) println ("new id list: $idList") transaction.set (key, Json.encodeToString (idList)) transaction.exec ()? .let {flag = false}}
The final "test" reads as follows:
It can be seen that we achieve data consistency by using WATCH and TRANACTION commands and using CAS optimistic locks.
Thank you for reading this article carefully. I hope the article "how to use optimistic locks to ensure data consistency in Redis" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.