getbookNameQUEUED>saddtag"Redis""NewBook"QUEUED>smemberstagQUEUED>e"/> getbookNameQUEUED>saddtag"Redis""NewBook"QUEUED>smemberstagQUEUED>e">
search
HomeDatabaseRedisRedis transaction instance analysis

    Usage in Redis

    Redis implements transaction functions through multi, exec, discard, and watch.

    • multi: start transaction

    • exec: commit transaction and execute

    • discard: cancel transaction

    • watch: Monitor any number of keys before the transaction starts

    > multi
    OK
    > set bookName "Redis"
    QUEUED
    > get bookName
    QUEUED
    > sadd tag "Redis" "New Book"
    QUEUED
    > smembers tag
    QUEUED
    > exec
    1) OK
    2) "Redis"
    3) (integer) 2
    4) 1) "Redis"
       2) "New Book"

    Start transaction

    > multi
    OK

    This command turns on the Redis_multi option , let the client change from non-transactional state to transactional state

    Redis transaction instance analysis

    Command enqueue

    > set bookName "Redis"
    QUEUED
    > get bookName
    QUEUED
    > sadd tag "Redis" "New Book"
    QUEUED
    > smembers tag
    QUEUED

    The Redis command will not be executed immediately, but will first enter an advanced First out of the transaction queue until the transaction is committed. QUEUED indicates that this command has been entered into the transaction queue.

    Execute transactions

    > exec
    1) OK
    2) "Redis"
    3) (integer) 2
    4) 1) "Redis"
       2) "New Book"

    When executing the exec command, Redis executes the commands in the transaction queue in a first-in, first-out manner according to the transaction queue saved by the client: the first command to be queued Executed first, and the last command enqueued is executed last. After executing the exec command, Redis stores the results in the reply queue and sends the queue to the client. The client exits from the transaction state and a transaction is completed.

    discard command

    > multi
    OK
    > set author "lisi"
    QUEUED
    > discard
    OK
    > get author
    (nil)

    discard is a command to cancel a transaction, indicating that the transaction has been cancelled. When the client ends the transaction state and returns to the non-transaction state, the Redis_multi option needs to be turned off.

    Redis transaction instance analysis

    watch command

    # Redis 客户端1
    > watch letter
    OK
    > multi
    OK
    > set letter a
    QUEUED
    > exec
    (nil)
    
    
    # Redis 客户端2
    > set letter b
    OK
    
    # Redis 客户端1
    > get letter
    "b"

    When client 1 enters the transaction, the watch sets the letter key and sets its value to a, but the transaction has not yet submit. Client 2 sets the value of letter to b. After client 1 commits the transaction, the result returned is nil, but by calling the get command, the value of letter can be obtained as b. This means that when the letter key is changed by another client, the transaction will be canceled, will not be executed, and will fail.

    The watch command monitors any number of keys before the transaction starts: when calling the excel command to execute a transaction, if any of the monitored keys has been modified by other clients, the entire transaction will no longer be executed and will return directly. fail.

    Redis transaction instance analysis

    Transaction exception

    Command error

    > set letter ac
    QUEUED
    > get letter ac
    (error) ERR wrong number of arguments for 'get' command
    > exec
    (error) EXECABORT Transaction discarded because of previous errors.

    Command exception in the transaction is a syntax error and will cause the transaction to fail to be executed.

    Runtime exception

    > multi
    OK
    > lpush books "Redis"
    QUEUED
    > incr books
    QUEUED
    > lpush books "Python"
    QUEUED
    > lrange books 0 -1
    QUEUED
    > exec
    1) (integer) 1
    2) (error) WRONGTYPE Operation against a key holding the wrong kind of value
    3) (integer) 2
    4) 1) "Python"
       2) "Redis"

    The above example is that the transaction encountered a failure in the middle of execution, because the incr command cannot be performed on a string. After the transaction encounters a command execution failure, subsequent The command continues to execute, so the value of books continues to be set. This exception can only be avoided by programmers in their code.

    ACID of transactions

    Atomicity

    Atomic means that either they execute successfully together or they fail together and are rolled back. All APIs provided by Redis are atomic operations. Then the Redis transaction only needs to guarantee atomicity in a batch of operations. However, in the case of runtime exceptions, if an exception occurs in a command in a transaction, other commands will continue to be executed. There is no rollback mechanism for the transaction, so the Redis transaction is not guaranteed to be atomic. sexual.

    Consistency

    Transaction exception

    If the command error occurs, the transaction cannot be executed. If it is a runtime exception, Redis will include the error in the return result and will not affect subsequent execution. , so the transaction is consistent.

    The Redis process is terminated

    In pure memory mode, Redis does not perform persistence. After restarting, the database is blank, so it is transactionally consistent.

    In RDB mode, the transaction will not perform the work of saving the RDB file in the middle. The RDB work may start only after the transaction is completed. Therefore, the Redis process is killed during transaction execution, and no matter how successful it is, it will not be saved to the RDB file, so it is consistent.

    In AOF mode, part of the transaction statement is written to the AOF file and saved successfully. Incomplete transactions are saved to the AOF file. When restarting Redis, check that the AOF file is incomplete, and Redis exits with an error. This incomplete transaction needs to be deleted before the restart can be successful, so it is consistent.

    In AOF mode, transactions are not written to the AOF file, so after restarting the Redis database is the latest data successfully saved to the AOF file. There is no data for this transaction, so it is consistent.

    Isolation

    Redis is a single-process program, and it guarantees that the transaction will not be interrupted when executing the transaction. The transaction can run until all commands in the transaction queue are executed. So transactions are isolated.

    Persistence

    In pure memory mode, transactions are definitely not persistent.

    In RDB mode, the server may fail during the period after the transaction is executed and before the RDB file is updated, so transactions in RDB mode are not durable.

    In AOF mode, add the command to the AOF file, but writing the file will not be written to the disk immediately, but will be stored in the buffer first. So there is a very small interval between data being saved to disk. Transactions in this mode are not durable either.

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

    Statement
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    es和redis区别es和redis区别Jul 06, 2019 pm 01:45 PM

    Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

    一起来聊聊Redis有什么优势和特点一起来聊聊Redis有什么优势和特点May 16, 2022 pm 06:04 PM

    本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

    实例详解Redis Cluster集群收缩主从节点实例详解Redis Cluster集群收缩主从节点Apr 21, 2022 pm 06:23 PM

    本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

    Redis实现排行榜及相同积分按时间排序功能的实现Redis实现排行榜及相同积分按时间排序功能的实现Aug 22, 2022 pm 05:51 PM

    本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

    详细解析Redis中命令的原子性详细解析Redis中命令的原子性Jun 01, 2022 am 11:58 AM

    本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

    实例详解Redis实现排行榜及相同积分按时间排序功能的实现实例详解Redis实现排行榜及相同积分按时间排序功能的实现Aug 26, 2022 pm 02:09 PM

    本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

    一文搞懂redis的bitmap一文搞懂redis的bitmapApr 27, 2022 pm 07:48 PM

    本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

    redis error什么意思redis error什么意思Jun 17, 2019 am 11:07 AM

    redis error就是redis数据库和其组合使用的部件出现错误,这个出现的错误有很多种,例如Redis被配置为保存数据库快照,但它不能持久化到硬盘,用来修改集合数据的命令不能用。

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    EditPlus Chinese cracked version

    EditPlus Chinese cracked version

    Small size, syntax highlighting, does not support code prompt function

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft