Home  >  Article  >  Database  >  What is transaction ACID? Can Redis transactions implement ACID?

What is transaction ACID? Can Redis transactions implement ACID?

青灯夜游
青灯夜游forward
2022-01-22 09:26:462297browse

Does Redis transactions support ACID? The following article will take you to understand Redis transactions, introduce how Redis implements transactions, and talk about whether Redis transactions support ACID. I hope it will be helpful to you!

What is transaction ACID? Can Redis transactions implement ACID?

Tencent interviewer: "Do you understand the transactions of Redis? Can its transaction mechanism implement ACID properties?"

Cheng Xuyuan : "Scratching my head, this... I know that Lua scripts can implement transactions..."

Tencent interviewer: "Okay, go back and wait for notification."


Code Brother , I got a lot of offers, but I didn’t expect that I finally lost on the question “How does Redis implement transactions?”

Let’s analyze it step by step:

  • What is transaction ACID?

  • How does Redis implement transactions?

  • What properties can Redis transactions achieve?

  • Lua script implementation.

What is the ACID of affairs

The Captain Mojin in Ghost Blowing the Lamp's "Yunnan Worm Valley" has a saying called "The rule of cooperation" "Life, division, death." In order to find the Muchen Bead, the three of them had a clear division of labor and worked together to advance and retreat in order to succeed.

Transaction is a unit of concurrency control, which is composed of a sequence of operations. Either all of these operations are executed or none of them are executed. [Related recommendations: Redis Video Tutorial]

"It is an indivisible work unit."

When a transaction is executed, special attribute guarantees will be provided:

  • Atomicity: multiple operations of a transaction must be completed, or none of them must be completed ( ps: How does MySQL achieve atomicity? Comments in the message area are welcome);

  • Consistency (Consistency): After the transaction execution is completed, the integrity constraints of the database have not been destroyed, and the transaction The order before and after execution are all legal data states.

    Integrity constraints of the database include but are not limited to:

    • Entity integrity (such as the primary key of the row exists and is unique);
    • Column integrity (such as field The type, size, and length must meet the requirements)
    • Foreign key constraints;
    • User-defined integrity (for example, the sum of the balances of the two accounts should remain unchanged before and after the transfer).
  • #Isolation: Operations within a transaction are isolated from other transactions, and concurrently executed transactions cannot interfere with each other.

    focuses on the interaction between different transactions. Strict isolation corresponds to serializable (Serializable) in the isolation level.

  • Durability: Once a transaction is committed, all modifications will be permanently saved in the database, and the data will not be lost even if the system crashes and is restarted.

# Brother Ma, after understanding the specific requirements of ACID, how does Redis implement the transaction mechanism?

How Redis implements transactions

MULTI,EXEC,DISCARD andWATCH The command is the basis for Redis to implement transactions.

The execution process of Redis transaction consists of three steps:

  • Start the transaction;

  • Command enqueue;

  • Execute transaction or discard;

Explicitly start a transaction

Customer The end explicitly indicates opening a transaction through the MULTI command, and subsequent commands will be queued and cached and will not be actually executed.

Command enqueuing

The client sends a series of instructions to be executed in the transaction to the server.

It should be noted that although the instructions are sent to the server, the Redis instance only temporarily stores this series of instructions in a command queue and will not execute them immediately.

Execute transaction or discard

The client sends a command to submit or discard the transaction to the server, and let Redis execute the command sent in the second step Specific instructions or clear queue commands will give up execution.

Redis only needs to schedule the execution of queue commands when calling EXEC .

You can also use DISCARD to discard the commands saved in the queue in the second step.

Redis transaction case

Execute our sample code through the online debugging website: try.redis.io

Normal execution

Through MULTI and EXEC execute a transaction process:

# 开启事务
> MULTI
OK
# 开始定义一些列指令
> SET “公众号:码哥字节” "粉丝 100 万"
QUEUED
> SET "order" "30"
QUEUED
> SET "文章数" 666
QUEUED
> GET "文章数"
QUEUED
# 实际执行事务
> EXEC
1) OK
2) OK
3) OK
4) "666"

We see that the return result after the execution of each read and write instruction is QUEUED, which means thank you for the operation. It is temporarily stored in the command queue and has not been actually executed.

When the EXEC command is executed, you can see the specific response data of each command.

Abandon transaction

Discard queue command via MULTI and DISCARD:

# 初始化订单数
> SET "order:mobile" 100
OK
# 开启事务
> MULTI
OK
# 订单 - 1
> DECR "order:mobile"
QUEUED
# 丢弃丢列命令
> DISCARD
OK
# 数据没有被修改
> GET "order:mobile"
"100"

Brother Code, can Redis transactions guarantee ACID properties?

This is a good question, let’s analyze it together.

Do Redis transactions satisfy ACID?

Redis transactions can execute multiple commands at one time, and come with the following three important guarantees:

  • Batch commands are executed The EXEC command will be put into the queue for temporary storage;

  • After receiving the EXEC command, it will enter transaction execution. If any command in the transaction fails to execute, the remaining commands will still be executed;

  • During transaction execution, commands submitted by other clients will not be inserted into the current command execution sequence.

Atomicity

Code brother, if an error occurs during transaction execution, atomic performance is guaranteed What?

During a transaction, two kinds of command errors may be encountered:

  • Before executing the EXEC command, the command itself is incorrect. As follows:
    • The number of parameters is wrong;
    • The command name is wrong and a non-existent command is used;
    • Insufficient memory (the Redis instance is configured using the maxmemory directive memory limit).
  • After executing the EXEC command, the command may fail. For example, the data types of the command and operation do not match (a List list operation is performed on a value of String type);
  • When executing the EXEC command of a transaction. A failure occurred in the Redis instance, causing transaction execution to fail.

EXEC reports an error before execution

When the command is queued, Redis will report an error and record the error.

At this time, we can still continue to submit command operations.

After the EXEC command is executed, Redis will refuse to execute all submitted command operations and return the result of transaction failure.

In this way, all commands in the transaction will no longer be executed, ensuring atomicity.

The following is an example of an error in command enqueuing, resulting in transaction failure:

#开启事务
> MULTI
OK
#发送事务中的第一个操作,但是Redis不支持该命令,返回报错信息
127.0.0.1:6379> PUT order 6
(error) ERR unknown command `PUT`, with args beginning with: `order`, `6`,
#发送事务中的第二个操作,这个操作是正确的命令,Redis把该命令入队
> DECR b:stock
QUEUED
#实际执行事务,但是之前命令有错误,所以Redis拒绝执行
> EXEC
(error) EXECABORT Transaction discarded because of previous errors.

EXEC reports an error after execution

Transaction operation is enqueued When the command and operation data types do not match, the Redis instance does not detect the error.

However, after executing the EXEC command, Redis will report an error when it actually executes these instructions.

Knocking on the blackboard: Although Redis will report errors for incorrect instructions, the transaction will still execute the correct command. At this time, the atomicity of the transaction cannot be guaranteed!

Brother Ma, why does Redis not support rollback?

In fact, Redis does not provide a rollback mechanism. Although Redis provides the DISCARD command.

However, this command can only be used to actively abandon transaction execution and clear the temporary command queue. It will not have the effect of rollback.

When EXEC is executed, a failure occurs

If Redis turns on the AOF log, then only part of the transaction operations will be recorded in the AOF log.

We need to use the redis-check-aof tool to check the AOF log file. This tool can remove unfinished transaction operations from the AOF file.

In this way, after we use AOF to restore the instance, the transaction operation will no longer be executed, thus ensuring atomicity.

Simple summary:

  • An error will be reported when the command is enqueued, and transaction execution will be abandoned to ensure atomicity;
  • When the command is enqueued No error is reported, but an error is reported during actual execution, and atomicity is not guaranteed;
  • The instance fails when the EXEC command is executed. If the AOF log is turned on, atomicity can be guaranteed.

Consistency

Consistency will be affected by incorrect commands and the timing of instance failures. According to the command error, there will be two instance failures. The timing of dimension occurrence can be analyzed in three situations.

Before execution of EXEC, if an error is reported after entering the queue,

the transaction will be abandoned, so consistency can be guaranteed.

After EXEC is executed, an error will be reported during actual execution.

The execution with errors will not be executed. The correct instructions can be executed normally and the consistency can be guaranteed.

When EXEC is executed, the instance fails.

After the instance fails, it will be restarted. This is related to the method of data recovery. We need to open RDB or AOF will be discussed on a case-by-case basis.

If we do not enable RDB or AOF, then after the instance fails and restarts, the data will be gone and the database will be consistent.

If we use RDB snapshot, because RDB snapshot will not be executed when the transaction is executed.

So, the results of the transaction command operation will not be saved to the RDB snapshot. When using the RDB snapshot for recovery, the data in the database will be consistent.

If we use the AOF log and the instance fails before the transaction operation is recorded in the AOF log, then the database data restored using the AOF log will be consistent.

If only some operations are recorded in the AOF log, we can use redis-check-aof to clear the completed operations in the transaction, and the database will be consistent after recovery.

Isolation

Transaction execution can be divided into command enqueuing (before the EXEC command is executed) and command actual execution (after the EXEC command is executed) Two stages.

So during concurrent execution, we analyze these two stages in two situations:

  • Concurrent operations are executed before the EXEC command, Isolation needs to be guaranteed through the WATCH mechanism;

  • Concurrent operations can be guaranteed after the EXEC command.

Brother Ma, what is the WATCH mechanism?

Let’s focus on the first situation: when the EXEC command of a transaction has not been executed, the command operation of the transaction is temporarily stored in the command queue.

At this time, if there are other concurrent operations and the same key is modified, you need to check whether the transaction uses the WATCH mechanism.

The function of the WATCH mechanism is to monitor the value changes of one or more keys before a transaction is executed. When the transaction calls the EXEC command to execute, the WATCH mechanism will first check whether the monitored keys have been modified by other clients. .

If modified, the transaction execution will be abandoned to prevent the isolation of the transaction from being destroyed.

At the same time, the client can execute the transaction again. At this time, if there is no concurrent operation to modify the transaction data, the transaction can be executed normally, and isolation is also guaranteed.

What is transaction ACID? Can Redis transactions implement ACID?

No WATCH

If there is no WATCH mechanism, concurrent operations will read and write data before the EXEC command is executed.

When EXEC is executed, the data to be operated within the transaction has changed, and Redis does not achieve isolation between transactions.

What is transaction ACID? Can Redis transactions implement ACID?

Concurrent operations are received and executed after EXEC

As for the second case, because Redis uses a single thread to execute commands, and , after the EXEC command is executed, Redis will ensure that all commands in the command queue are executed before executing subsequent instructions.

So, in this case, concurrent operations will not destroy the isolation of the transaction.

What is transaction ACID? Can Redis transactions implement ACID?

Persistence

If Redis does not use RDB or AOF, then the persistence attribute of the transaction must be No guarantees.

If Redis uses the RDB mode, then after a transaction is executed but before the next RDB snapshot is executed, if an instance crashes and data is lost, in this case, the transaction modification Data is also not guaranteed to be durable.

If Redis adopts AOF mode, there will be data loss because of the three configuration options of AOF mode: no, everysec and always.

Therefore, the durability properties of transactions are still not guaranteed.

No matter what persistence mode Redis adopts, the durability properties of transactions are not guaranteed.

Summary

  • Redis has a certain degree of atomicity, but does not support rollback.
  • Redis does not have the concept of consistency in ACID. (Or Redis ignored this when designing)
  • Redis is isolated.
  • Redis does not guarantee durability.

Redis’ transaction mechanism can guarantee consistency and isolation, but it cannot guarantee durability.

However, because Redis itself is an in-memory database, persistence is not a necessary attribute. We are more concerned about the three attributes of atomicity, consistency and isolation.

The situation of atomicity is more complicated. When the command syntax used in the transaction is incorrect, atomicity is not guaranteed. In other cases, the transaction can be executed atomically.

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of What is transaction ACID? Can Redis transactions implement ACID?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete