Home  >  Article  >  Database  >  What is a Redis transaction

What is a Redis transaction

hzc
hzcOriginal
2020-06-28 14:31:082626browse

Redis transaction can execute multiple commands at one time. It is essentially a set of commands. All commands in a transaction will be serialized and then executed serially in order without being inserted. Other commands.

What is a Redis transaction

1. What is a redis transaction?

Multiple commands can be executed at one time, which is essentially a set of commands. All commands in a transaction will be serialized and then executed serially in order without being inserted into other commands

2. What can Redis transactions do?

In a queue, a series of commands are executed in a certain, sequential and exclusive manner

3. How to use the redis command?

1. Transaction-related commands:

(1) DISCARD: cancel the transaction and give up executing all commands in the transaction block

(2) EXEC: execute Commands in the transaction block

    (3) MULTI: Mark the start of a transaction

                    

#    

#                            」   

# key [key...]: Monitor one (or more) keys. If this (or these) keys are modified by other commands before executing the transaction, the transaction will be interrupted.

2. Transaction error reporting problem:

(1) Statement error: An error will be reported directly when adding the queue. If this error occurs, the entire transaction will be rolled back

(2) Logic error: For example, if a string 1 is given, an error will be reported during execution. This kind of error will not affect other operations in the transaction. Only this article will report an error


3. Watch monitoring:

(1) Optimistic lock:

Optimistic Lock (Optimistic Lock), It is an optimistic lock. Every time you go to get the data, you think that others will not modify the data, so it will not lock. However, when updating, it will judge whether others have updated the data during this period. You can use Mechanisms such as "version number" are optimistically used in multi-read application types, which can improve throughput.

Optimistic lock strategy: The submitted version must be greater than the current recorded version before it can be updated

(2) Pessimistic lock

Pessimistic lock (Pessimistic Lock) is a very Pessimistic lock, every time you go to get the data, you will think that others will operate according to the modification, leading to problems such as overwriting. So every time you get the data, it will be locked, so if someone else wants to get the data, it will be blocked. After the modification is completed, the lock can be unlocked and used. Traditional relational databases use many such lock mechanisms. , such as row locks, table locks, read locks, and write locks, all of which lock the table before performing operations.

Pessimistic locking ensures data security, but will degrade performance

Four and three features

1. Separate isolation operation:

All commands in the transaction will be serialized and executed in order. During transaction execution, it will not be interrupted by command requests sent by other clients.

2. There is no concept of isolation level:

The commands in the queue will not be actually executed before they are submitted, because no instructions will be actually executed before the transaction is submitted, so there is no There is a problem that "queries within a transaction must see physical updates, but queries outside the transaction cannot see this problem"

 3. Atomicity is not guaranteed:

 If redis in the same transaction If one command fails to execute, subsequent commands will still be executed without rollback.

(Unless an error occurs when joining the queue, that is, a compile-time exception similar to Java and an execution-time exception. Compilation will cause rollback, and execution-time exception will not roll back)

Recommended tutorial : "Redis Tutorial

" ###

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn