Transactions and retries
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.
| Condition | Behavior on EXEC |
|---|---|
| Unknown command or wrong arity while queuing | Queuing 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 EXEC | EXEC 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. |
WATCHon existing and missing keys:WATCHregisters key digests inredis._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 andEXECdetects the conflict.- Deterministic command time and wakeups: All commands inside one
EXECbatch share a single timestamp for key expiry checks. Blocked clients waiting on keys touched byEXECobserve 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 COMMITTEDtransaction isolation is supported. Callingredis.commandunderREPEATABLE READorSERIALIZABLEraises 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 defaultredis-py6.4.0 client (which defaults to 3 retries) repeated anINCRafter a dropped reply and incremented the counter to2. SettingRetry(NoBackoff(), 0)sent the command once and raised a connection error, leaving the caller responsible for resolving theUNKNOWNstate. Even with zero connection retries,redis-py’sRedis.transaction()helper catchesWatchErrorand re-runs its callback when anEXECreply 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).
