Header Ad Placement

Redis MULTI and EXEC: Why an Isolated Transaction Can Still Leave Partial Results

DATABASE CORRECTNESS

Uninterrupted execution, stale-read detection and crash survival are different promises. A Redis transaction should be reviewed against each one separately.

By Hossam

A transaction is not automatically an undo boundary

A developer groups several Redis writes between MULTI and EXEC and assumes that any error will restore the original state. That assumption confuses an execution boundary with a rollback mechanism. Redis documents isolated, sequential execution of the queued commands, but it does not provide transaction rollback when a command fails during execution.

This distinction matters when several keys represent one application decision. A successful write beside an error reply may leave a state that the application never intended, even though no other client interrupted the transaction. The transaction mechanism and the business invariant must be evaluated separately.

This guide is an analysis of Redis documentation, not a report of a server experiment. The inventory sequence below is fictional. No Redis instance, client package, cluster, failover or production workload was tested for this article.

Documentation: Redis transactions, EXEC command.

MULTI queues work; EXEC returns a result for each command

MULTI starts a transaction block. Commands that are accepted into the block are queued rather than immediately executed. A QUEUED reply therefore confirms acceptance into the queue, not that the requested data change succeeded. EXEC runs the queued commands and returns their replies in the order they were issued.

Redis states that another client request is not served in the middle of that transaction execution. This is the useful isolation guarantee. It does not make reads performed earlier by the client part of the same protected interval, and it does not convert every command reply into a success.

DISCARD abandons the queued transaction before execution. It is not an undo command for work that EXEC has already performed. Similarly, a client-side exception is an observation made by a library, not proof that the server restored all keys.

Documentation: Redis transactions, EXEC command.

Separate queue-time errors from execution-time errors

A syntax error or another detected problem can prevent a command from being queued. The transactions guide says Redis has detected errors during command accumulation and refused to execute the transaction at EXEC since version 2.6.5. Its historical note describes different behavior before that version. Do not mix the historical rule with the documented modern one.

An execution-time error is different. A command may have valid syntax but encounter a value of the wrong type when it runs. The guide states that the other queued commands are still processed, including commands after the failure. Successful writes are not rolled back.

The client must interpret the complete result, including command-level errors and the resulting state. A wrapper that raises one exception may conceal the difference between an aborted transaction and a completed transaction containing an error. Verify the behavior of the specific client and its options rather than inferring server semantics from exception wording.

Documentation: Redis transactions, EXEC command.

An invented inventory sequence exposes the gap

Assume three disposable keys in a fictional single-instance example. A stock counter contains the integer 10. A separate audit key mistakenly contains a string where the application expects a list. A third key will record an order status. These are invented application details, not captured server responses.

The client queues a decrement of stock by one, an append to the wrongly typed audit key, and a write marking the order as reserved. At execution, the decrement can succeed, the list operation can return a wrong-type error, and the final status write can still succeed. The documented runtime-error rule permits that combination.

The intended application rule might require the reservation and its audit record to exist together. Isolation alone does not enforce it. The example is not a recommendation to implement inventory this way; it identifies the acceptance condition that a design must actually prove.

Checking types and validating the proposed operation can reduce avoidable failures, but a separate unprotected check is not a general correctness proof. Another writer can change relevant state before execution. The validation and concurrency design must cover the state on which the decision depends.

Documentation: Redis transactions.

WATCH protects a read-dependent decision, not every failure

WATCH makes a later EXEC conditional on watched keys not being modified before EXEC. If a watched key changes, the transaction aborts rather than executing its queued commands. EXEC reports a null array in RESP2 or null in RESP3 for this watched-key abort.

The useful sequence is to watch the relevant keys, read the state used by the decision, compute the proposed changes, then enter MULTI and queue the writes. If the condition fails, the application needs a fresh decision from current state. Merely replaying an old computed value defeats the reason for watching.

WATCH does not reserve a key and stop other clients from modifying it. It detects relevant modification for conditional execution. Nor does it promise rollback for a later wrong-type error after the watched condition has passed. Missing watched dependencies and invalid command assumptions remain application concerns.

Documentation: Redis transactions, EXEC command, WATCH command.

Expiration, eviction and connection lifetime affect the condition

The transactions guide includes changes made by Redis itself, such as expiration and eviction, among modifications relevant to WATCH. It explicitly notes that before Redis 6.0.9 an expired watched key would not abort the transaction. An expiry-sensitive design needs the deployed version, not an unqualified statement about every Redis release.

Queued commands inside MULTI do not trigger the watched condition merely by being queued. EXEC clears the watch state whether the watched transaction executes or aborts. Closing the client connection also clears watches; UNWATCH can clear them explicitly.

These connection-scoped rules matter when a client uses pooling or automatic reconnection. Confirm that the client keeps the watched read and transaction on the intended connection and handles cleanup. Do not assume that a newly acquired connection inherits the old watch, or that a reconnection preserves an in-progress conditional decision.

Documentation: Redis transactions, WATCH command, redis-py pipelines and transactions.

A pipeline name does not identify the server guarantee

Protocol pipelining sends multiple commands without waiting for each reply. It reduces request-response overhead; batching by itself is not the MULTI/EXEC isolation contract. Confusing these mechanisms can produce a test that measures fewer round trips while never checking the concurrency behavior the application needs.

There is also a client-library naming trap. Redis documents that redis-py pipeline objects execute as transactions by default, and that transaction=False disables that behavior. This is a specific documented client behavior, not a rule that every method called pipeline in every library opens a transaction.

In the documented redis-py WATCH flow, commands execute immediately after watch until multi begins buffering again. Review that phase change when translating pseudocode. A read needed to compute a write must actually return a value before the calculation; buffering everything is not equivalent.

Documentation: redis-py pipelines and transactions, Redis pipelining.

A single-instance example is not a cross-shard transaction

The multi-key documentation distinguishes deployment configurations. For the clustered configurations it lists, keys in a MULTI/EXEC transaction are restricted to a single hash slot. A design that works with arbitrary local keys on one standalone instance should not be presented as a general transaction across cluster shards.

Check the product, clustering mode, key layout and client routing before choosing the transaction boundary. Related keys can be designed around documented slot constraints, but a convenient name or one successful local request is not proof that a distributed deployment will accept the same operation.

This article does not test cluster migration, Active-Active conflict behavior or client routing recovery. Those are separate acceptance questions. Avoid widening a single-slot guarantee into a claim about a business workflow spanning several independent data stores.

Documentation: Multi-key operation boundaries, EXEC command.

Successful execution is not the same as durable recovery

Redis persistence has several configurations, including RDB snapshots, AOF logging, both, or no persistence. The durability question is therefore not answered merely by receiving a successful EXEC result. Review what was persisted, when it was synced, and what the recovery procedure can establish.

The persistence reference distinguishes appendfsync always, everysec and no. It notes that everysec can lose roughly a second of writes in a disaster, while the no policy leaves syncing to the operating system. These are documented configuration tradeoffs, not measurements from this article or a guarantee about an unspecified host.

WAIT adds another distinct observation: how many replicas acknowledged prior writes on the current connection. It returns the achieved count even when a timeout occurs, so the client must compare that count with the requested level. Putting WAIT inside MULTI does not turn it into a blocking durability barrier; its reference says it returns immediately in that context.

The same reference explicitly says WAIT does not make Redis strongly consistent and that failover can still lose acknowledged writes. Do not rename a replication acknowledgment as disk persistence, rollback support or guaranteed survival of every failure.

Documentation: WAIT command and consistency, Redis persistence.

Test final state, conflict handling and uncertain outcomes

A useful isolated acceptance test should distinguish a queue-time error, a runtime wrong-type error and a WATCH conflict. For each, inspect every relevant final key and the complete command results. A test that checks only whether a library raised an exception can miss successful writes left behind.

Exercise the actual deployed server and client versions, watch cleanup, expiry behavior and the intended connection handling. For clustered deployments, include the real slot layout. Durability and failover tests need their own documented environment and recovery checks; a local happy-path transaction is not a substitute.

If the connection is lost after an EXEC may have reached the server, do not equate the missing reply with an aborted transaction. Reconcile the operation against application evidence before repeating a non-idempotent change. This is an engineering recovery requirement, separate from a confirmed WATCH abort in which the queued commands were not executed.

The practical review has distinct questions: Were the commands accepted, did their individual effects succeed, was the read-dependent decision still valid, and what survives a failure? Redis provides mechanisms for parts of that review. The application remains responsible for showing how the mechanisms fit its invariant.

Documentation: Redis transactions, EXEC command, redis-py pipelines and transactions, WAIT command and consistency, Redis persistence.

Related reading

For the HTTP version of stale-state rejection, see what If-Match protects and what it does not. That article concerns representation validators, not Redis command-error handling. The guide to idempotency-key lifetimes addresses duplicate actions and unknown outcomes rather than transaction rollback.

Sources and scope

Documentation checked November 23, 2026. Sources are Redis command references, server behavior and operations guides, and its redis-py client guide. They are operator documentation, not independent benchmarks. The example and proposed tests are illustrative; no server experiment, performance measurement, cluster deployment or recovery drill was executed.


 

Comments

Docker Build Contexts: What .dockerignore Excludes and COPY Does Not

CONTAINER BUILD INPUTS The context decides which files a build can access. A copy instruction decides what one step puts into a stage. Review those boundaries separately. By Hossam Start with the input boundary, not the final image A Dockerfile that copies only application files can still have a wider build context than its author intended. The context is the set of files available to the build, while COPY describes one operation using a source. A selective copy is useful, but it is not the same control as excluding an input from the context. Docker documents .dockerignore as a way to remove matching files and directories from the context before it is sent to the builder. That makes context selection and ignore rules worth reviewing even when the final runtime image appears small. A final-image inspection answers a different question from a build-input review. This guide analyzes Docker documentation. Its example repository layout and review steps are illustrative, not captured b...

Editorial Governance & Pages