How to implement transaction with rollback in Redis

后端 未结 2 1892
感动是毒
感动是毒 2021-01-13 12:58

My program needs to add data to two lists in Redis as a transaction. Data should be consistent in both lists. If there is an exception or system failure and thus program onl

2条回答
  •  囚心锁ツ
    2021-01-13 13:42

    Redis transactions are different. It guarantees two things.

    1. All or none of the commands are executed
    2. sequential and uninterrupted commands

    Having said that, if you have the control over your code and know when the system failure would happen (some sort of catching the exception) you can achieve your requirement in this way.

    1. MULTI -> Start transaction
    2. LPUSH queue1 1 -> pushing in queue 1
    3. LPUSH queue2 1 -> pushing in queue 2
    4. EXEC/DISCARD

    In the 4th step do EXEC if there is no error, if you encounter an error or exception and you wanna rollback do DISCARD.

    Hope it makes sense.

提交回复
热议问题