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

What is the operation method of Redis transaction processing

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What this article shares to you is about the operation method of Redis transaction processing, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

I. brief introduction

Redis uses optimistic locking for transaction control. It uses the watch command to monitor a given key. When exec (commit transaction), if the monitored key has changed since the call to watch, the whole transaction will fail. You can also call watch to monitor multiple key multiple times. Note that the key of watch is valid for the entire connection, and if the connection is disconnected, monitoring and transactions are automatically cleared. Of course, the exec,discard,unwatch command clears all monitoring from the connection.

Redis guarantees that all commands in a transaction are either executed or not executed (atomicity). If the client is disconnected before sending the EXEC command, Redis clears the transaction queue and all commands in the transaction are not executed. Once the client sends the EXEC command, all commands are executed, even if the client is disconnected later, because all the commands to be executed are recorded in the Redis.

Common instructions:

Multi starts a transaction

Exec commit transaction

Discard cancels the transaction

Watch monitoring, if the monitored value changes, the transaction will fail when it is committed

Unwatch, remove the monitoring.

Second, simulate the use of simulated money transfer operation

After the transaction is opened, all operations are entered into a queue and executed together when committed.

Simulate cancellation of transaction

The redis transaction is so simple that there is no rollback, only cancellation.

When an error occurs in a statement in the queue, the transaction is automatically cancelled

Demonstration of optimistic lock use

Optimistic lock (Optimistic Lock), as the name implies, is very optimistic. Every time you go to get the data, you think that others will not modify it, so it will not be locked. During this period, the data can be easily read by others, but during the update, you can judge whether others have updated the data during this period. You can use mechanisms such as version number.

The version number mechanism is the most commonly used way of optimistic locking, that is, add a version number field to the table, check it before updating, and then update it as the where condition of the update statement. If the data has changed after obtaining the version number and before the update, the update will fail, because 0 pieces of data have been updated finally, and if the number of updates obtained by the java backend is 0, then the update failed. There is a concurrency problem, and then do specific treatment.

1. Add test data statement

Turn on monitoring, start transactions, and execute statements

two。 Set up another redis to modify the data

3. Return to the first database commit transaction

Here we can see the failure to execute the transaction. A becomes 666.

Third, optimistic lock second kill ticket grabbing exercise / * implement a simple multi-thread ticket grabbing operation based on reids * focus on the application of delayed optimistic lock * / public class SecondsKillDemo02 {/ / define ticket grabbing logic public static void KillTicket () {/ / 1. Connect Jedis jedis = JedisDataSource.getConnection (); / / 2. Key String a = jedis.get ("a") specified in the monitoring reids; if (a = = null | | Integer.valueOf (a) = = 0) throw new RuntimeException ("no ticket"); jedis.watch ("a", "b"); / / 3. Open transaction execution business Transaction multi = jedis.multi (); try {multi.decr ("a"); multi.incrBy ("b", 100); / / 4. Commit transaction multi.discard (); System.out.println ("ok");} catch (Exception e) {multi.exec ();} finally {/ / 5. Cancel monitoring jedis.unwatch (); / / 6. Release jedis.close ();}} public static void main (String [] args) {/ / 1. Define initial data Jedis jedis = JedisDataSource.getConnection (); jedis.set ("a", "1"); jedis.set ("b", "0"); / / 2. Create multiple threads and execute ticket grabbing Thread T1 = new Thread (new Runnable () {@ Override public void run () {KillTicket ();}}); Thread T2 = new Thread (new Runnable () {@ Override public void run () {KillTicket ()) }}); t1.start (); t2.start ();}} these are the operation methods of Redis transactions, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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

Development

Wechat

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

12
Report