Redis EXEC Failed. Why Did the Other Writes Still Happen?
A runtime error inside Redis EXEC does not roll back other commands. Separate queue-time failure, mixed results and WATCH conflicts before retrying the batch.
Your Redis transaction reports an error, but a counter still increased and a status key still changed. That can be the documented result of MULTI/EXEC. A command that fails while the queued commands are executing doesn't roll back successful commands or prevent the remaining commands from being attempted. Before retrying the whole batch, identify whether the failure happened while queueing, during execution, or before a watched transaction could run.
This is an AI-prepared, documentation-based explainer, not a personally reproduced incident. Commands and outcomes below are illustrative and unexecuted. See the editorial policy.
The transaction label hid two different questions
Imagine an application that increments a count, adds an item to a list, and marks an operation complete. It wraps the commands in a transaction. Then the list command fails. Seeing the completion marker afterwards feels like the server ignored the failure.
The missing distinction is between keeping other clients out of the middle of the batch and undoing successful work after a command error. Redis's transaction contract provides isolated, sequential execution. It explicitly doesn't provide transaction rollbacks. The useful diagnostic question is when the error became visible, rather than whether the application used a method called transaction().
Follow the replies, one command at a time
This illustrative command sequence assumes fresh disposable example keys and one Redis connection. The shared {exec-demo} hash tag also lets the keys occupy the same Redis Cluster hash slot; cluster routing still belongs to the client configuration.
SET {exec-demo}:count 0
MULTI
INCR {exec-demo}:count
LPUSH {exec-demo}:count item
SET {exec-demo}:status complete
EXECThere is an intentional type mistake. The count key contains a string value suitable for INCR, while LPUSH expects a list. The commands have valid syntax, so queue acceptance doesn't establish that every operation can succeed against the data it will encounter.
Under these assumptions, the increment succeeds, the list operation fails with a wrong-type error, and the status write can still succeed. These are predicted consequences of the documented rules, not output captured from a test. The EXEC reply gives the replies in command order. Treat it as several results that need interpretation, rather than one reassuring success flag.
If a client library turns one command error into an exception, inspect its transaction response and error-handling API. An exception at the application boundary doesn't tell you which other operations Redis executed. The catch block has excellent manners; it doesn't own a time machine.
Why a different broken command seems to roll everything back
Now consider a queueing error, such as issuing LPUSH {exec-demo}:count without an element. That's a different failure boundary. The current transaction guide says that, starting with Redis 2.6.5, errors detected while accumulating commands cause the server to refuse execution of that transaction when EXEC is called. That can look like rollback, but the queued writes didn't run in the first place.
DISCARD similarly abandons commands that are still queued. It isn't a command to undo an EXEC that has already changed data. Redis's own explanation of its rollback design is useful background, but the practical distinction is right there in the command replies.
| Observed boundary | What to establish before retrying |
|---|---|
| Error while commands are queued; EXEC refuses the transaction | Confirm the queue-time failure and fix the invalid request before trying again. |
| EXEC returns individual command results including an error | Inspect every result and resulting state; successful writes may already exist. |
| WATCH condition prevents execution | Read fresh state and recompute the intended operation. |
| Connection fails around EXEC | The transport failure alone doesn't establish whether the batch ran. |
A blind retry can repeat the part that worked
In the illustrative batch, replaying everything after the list error can increment the count again. Increasing the retry limit doesn't repair the type mismatch. It only makes the successful increment more enthusiastic.
Define the operation's intended final state and its retry identity before deciding how to recover. If several writers share a key namespace, determine which component owns the key's type. A malformed key from one writer can turn a previously valid transaction into a mixed-result batch. In tests, seed the wrong type deliberately instead of assuming a clean database forever.
Where validation depends on values read before the write, WATCH can detect intervening changes to watched keys. It doesn't add rollback to commands that subsequently fail inside EXEC. Validation and optimistic concurrency solve particular problems; neither makes every possible execution error disappear.
When an invariant truly requires undoing a group of changes on failure, choose a data model or transaction mechanism that supplies that contract. Don't infer it from the presence of MULTI. For retry design more generally, Debugly's retry and idempotency review asks the related question: what happens if an operation is repeated?
Test the boundary you actually depend on
Use separate tests for a queue-time syntax error, a runtime wrong-type error, a watched-key conflict, and a lost connection around execution. Inspect state as well as the client exception. For the runtime case, assert what happens to valid commands before and after the failing command. These are proposed checks, not results measured for this article.
Keep this distinct from other Redis side effects. A successful SET may change a key's expiry according to its own options; the SET and TTL explainer covers that behavior. Here the surprise is which commands ran despite another command failing.
In a code review, put the queueing replies, EXEC result handling and retry policy next to each other. If the code catches one error and replays the entire batch, ask which successful writes that catch block is about to repeat.