search
HomeDatabaseRedisRedis Learning: Introduction to redis transactions

Redis Learning: Introduction to redis transactions

What it is

can execute multiple commands at one time, which is essentially a set of commands. All commands in a transaction will be serialized, serialized and executed in order without being inserted by other commands, and no blocking is allowed.
Execute multiple redis commands at one time.

What can be done

Execute a series of commands in a queue at one time, sequentially and exclusively.

How to play

The MULTI command is used to open a redis transaction. This command will always reply OK, (I don’t know if it can succeed. ), At this time, the user can execute multiple commands at once instead of executing one by one. redis will enqueue them, and all commands will be called by the EXEC command
DISCARD abandons the batch operation.

Recommended (free): redis tutorial

Common commands

##DISCARDCancel the transaction and abandon the execution of all commands within the transaction block. EXECExecute all commands within the transaction block. MULTIMarks the beginning of a transaction block. UNWATCHCancel the monitoring of all keys by the WATCH command. WATCH key [key …]Monitor one (or more) keys. If this (or these) keys are changed by other commands before the transaction is executed, Then the transaction will be interrupted.

Case

Normal execution

Redis Learning: Introduction to redis transactions

##Abandon transaction

Redis Learning: Introduction to redis transactions All in a row

Redis Learning: Introduction to redis transactions
A mistake, all in a row, no execution

Enemy creditor

Redis Learning: Introduction to redis transactions Regarding this problem, how to understand redis’s support for transactions
Redis partially supports transactions. In this part, the correct ones are executed, and the wrong ones are not executed.

case:watch monitoring

Pessimistic Lock/Optimistic Lock/CAS (Check and set)

Pessimistic Lock (Pessimistic Lock) , as the name suggests, it is very pessimistic. Every time you go to get the data, you think that others will modify it, so you will lock it every time you get the data. In this way, if others want to get the data, they will block until they get the lock. Many such lock mechanisms are used in traditional relational databases, such as row locks, table locks, read locks, write locks, etc., which are all locked before operations. Table lock: Lock the entire table. However, this table may have many pieces of data. At this time, a process needs to make large-scale changes, which will cause more and more threads to be queued.
Row Lock: Lock each record
Optimistic Lock

Optimistic Lock, as the name suggests, 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, but when updating, it will be judged whether others have updated the data during this period. You can use mechanisms such as version numbers. Optimistic locking is suitable for multi-read application types, which can improve throughput. Optimistic locking strategy: The submitted version must be greater than the current version of the record before the update can be performed
Optimistic locking is not blindly optimistic. For example, Zhang San changed his WeChat account and Li Si changed his QQ account at the same time. At that time, the version number was all 1, and then Zhang San changed the WeChat account and submitted it. At this time, the version number changed from 1 to 2. Li Si also submitted it after changing it. At this time, it changed from 1 to 3, and an exception was reported. Restart Revise.
Optimistic locking is generally used at work

Initialize the available balance and debt of the credit card

Redis Learning: Introduction to redis transactions

No tampering, Monitor first and then enable multi to ensure that the two amount changes are within the same transaction

Redis Learning: Introduction to redis transactions While monitoring, it was found that another transaction modified the shared data, causing the transaction execution to fail

Redis Learning: Introduction to redis transactions Before modifying data, watch needs to be locked, otherwise an error will occur. If someone modifies my data, I will report an exception.

There is tampering

The key is monitored. If the key is modified, the execution of the subsequent transaction will be invalid

unwatch

Cancel the monitoring of all keys by the watch command

Once the exec is executed, the monitoring lock added before executing will be canceled

Summary

Watch instruction is similar to optimistic locking. When the transaction is submitted, if the value of Key has been changed by another client, for example, a list has been pushed/popped by another client, the entire transaction queue will not be executed.

Multiple Keys are monitored before transaction execution through the WATCH command. If the value of any Key changes after WATCH, the transaction executed by the EXEC command will be abandoned, and a Nullmulti-bulk response will be returned to notify the caller that the transaction execution failed.

3 Phase

• Open: Start a transaction with MULTI

• Enqueue: Enqueue multiple commands into the transaction. Receiving these commands does not It will be executed immediately, but placed in the transaction queue waiting for execution
• Execution: Transaction triggered by EXEC command

3 Features

Individual isolation operation: All commands in a transaction are serialized and executed sequentially. During the execution of the transaction, it will not be interrupted by command requests sent by other clients.

There is no concept of isolation level: the commands in the queue will not be actually executed until they are submitted, because no instructions will be actually executed before the transaction is submitted, so there is no "query within the transaction to see the transaction" The update in the redis cannot be seen when queried outside the transaction. This is a very troublesome problem.
Atomicity is not guaranteed: if a command fails to execute in the same redis transaction, subsequent commands will still be executed without a response. Roll
does not follow traditional ACID in AI

Command Description

The above is the detailed content of Redis Learning: Introduction to redis transactions. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
Redis: A Guide to Key-Value Data StoresRedis: A Guide to Key-Value Data StoresMay 02, 2025 am 12:10 AM

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis: Caching, Session Management, and MoreRedis: Caching, Session Management, and MoreMay 01, 2025 am 12:03 AM

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.

Redis: Exploring Its Core Functionality and BenefitsRedis: Exploring Its Core Functionality and BenefitsApr 30, 2025 am 12:22 AM

Redis's core functions include memory storage and persistence mechanisms. 1) Memory storage provides extremely fast read and write speeds, suitable for high-performance applications. 2) Persistence ensures that data is not lost through RDB and AOF, and the choice is based on application needs.

Redis's Server-Side Operations: What It OffersRedis's Server-Side Operations: What It OffersApr 29, 2025 am 12:21 AM

Redis'sServer-SideOperationsofferFunctionsandTriggersforexecutingcomplexoperationsontheserver.1)FunctionsallowcustomoperationsinLua,JavaScript,orRedis'sscriptinglanguage,enhancingscalabilityandmaintenance.2)Triggersenableautomaticfunctionexecutionone

Redis: Database or Server? Demystifying the RoleRedis: Database or Server? Demystifying the RoleApr 28, 2025 am 12:06 AM

Redisisbothadatabaseandaserver.1)Asadatabase,itusesin-memorystorageforfastaccess,idealforreal-timeapplicationsandcaching.2)Asaserver,itsupportspub/submessagingandLuascriptingforreal-timecommunicationandserver-sideoperations.

Redis: The Advantages of a NoSQL ApproachRedis: The Advantages of a NoSQL ApproachApr 27, 2025 am 12:09 AM

Redis is a NoSQL database that provides high performance and flexibility. 1) Store data through key-value pairs, suitable for processing large-scale data and high concurrency. 2) Memory storage and single-threaded models ensure fast read and write and atomicity. 3) Use RDB and AOF mechanisms to persist data, supporting high availability and scale-out.

Redis: Understanding Its Architecture and PurposeRedis: Understanding Its Architecture and PurposeApr 26, 2025 am 12:11 AM

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

Redis vs. SQL Databases: Key DifferencesRedis vs. SQL Databases: Key DifferencesApr 25, 2025 am 12:02 AM

The main difference between Redis and SQL databases is that Redis is an in-memory database, suitable for high performance and flexibility requirements; SQL database is a relational database, suitable for complex queries and data consistency requirements. Specifically, 1) Redis provides high-speed data access and caching services, supports multiple data types, suitable for caching and real-time data processing; 2) SQL database manages data through a table structure, supports complex queries and transaction processing, and is suitable for scenarios such as e-commerce and financial systems that require data consistency.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment