Skip to content

Transactions and retries

MULTI, EXEC, WATCH, commit outcomes and when a client may retry.

SQL callers and Redis wire clients share the same PostgreSQL storage tables and WAL, but they enter transactions through different boundaries.

MULTI, EXEC, and WATCH

MULTI starts a command queue in the Go gateway. EXEC dispatches the queued commands inside a single PostgreSQL READ COMMITTED transaction. An idle MULTI or WATCH session on a client connection does not hold an open PostgreSQL transaction in the background.

ConditionBehavior on EXEC
Unknown command or wrong arity while queuingQueuing records the syntax error and EXEC aborts the queue with -EXECABORT Transaction discarded because of previous errors.
Runtime command error inside EXEC (for example WRONGTYPE)That slot in the EXEC array reply contains the error frame; sibling commands in the queue still execute and commit, matching Redis.
Watched key modified before EXECEXEC returns a null array reply (*-1\r\n in RESP2, _\r\n in RESP3) and applies no changes.
PostgreSQL engine error (such as deadlock or out-of-workspace error)The entire PostgreSQL transaction rolls back so no partial EXEC state persists.
  • WATCH on existing and missing keys: WATCH registers key digests in redis._watch. If another client creates and then deletes a watched missing key (or modifies a key and restores its previous value), row triggers record the revision change and EXEC detects the conflict.
  • Deterministic command time and wakeups: All commands inside one EXEC batch share a single timestamp for key expiry checks. Blocked clients waiting on keys touched by EXEC observe the final committed state after the entire queue finishes.

SQL transactions

Multiple calls to redis.command and redis.command_bin inside BEGIN ... COMMIT join the caller’s outer SQL transaction:

  • Relational table rows, Redis key mutations, and queued Pub/Sub / tracking / blocking notifications commit or roll back atomically. See the SQL rollback tutorial.
  • Only READ COMMITTED transaction isolation is supported. Calling redis.command under REPEATABLE READ or SERIALIZABLE raises an explicit error because internal lock-wait re-reads require fresh statement snapshots.
  • Mixing SQL row locks and Redis key mutations across concurrent transactions in different orders can form a PostgreSQL deadlock. PostgreSQL aborts one participant with SQLSTATE 40P01; applications should handle deadlock retries at the transaction boundary.

Handling lost responses (UNKNOWN outcomes)

When a client sends a mutating command (INCR, LPOP, PUBLISH, XADD, EXEC) and the network connection drops or times out before the reply arrives, the PostgreSQL transaction may have already committed. Reconnecting and running PING only proves that the new socket works; it cannot reveal whether the previous write committed.

  • Gateway behavior: When a connection becomes uncertain (read/write timeout, PostgreSQL link drop, or output buffer overflow), the gateway closes the client socket and retires the backend connection without automatically re-running the command.
  • Client library retry pitfalls: Many Redis client libraries enable automatic command retries by default. In Linux acceptance tests (aade9dfa0, 2026-10-15), a default redis-py 6.4.0 client (which defaults to 3 retries) repeated an INCR after a dropped reply and incremented the counter to 2. Setting Retry(NoBackoff(), 0) sent the command once and raised a connection error, leaving the caller responsible for resolving the UNKNOWN state. Even with zero connection retries, redis-py’s Redis.transaction() helper catches WatchError and re-runs its callback when an EXEC reply is lost.
  • Safe retry patterns: For non-idempotent operations, either disable automatic client retries and inspect application state before retrying, or record an idempotency key / outbox row in the same transaction (as shown in the Transactional outbox tutorial).
Scan to open this page
https://mimus.pgsty.com/docs/redis/reference/transactions/