Transactions vs. Failover

What it meansDoes MULTI/EXEC give it?
Execution atomicityAll commands run together on the primary as one uninterrupted unit — no other client's command interleaves; you never observe "debit ran, credit didn't" in memory.✓ yes
Durability atomicity across failoverOnce acknowledged, the whole transaction survives a crash + replica promotion.✗ no

MULTI/EXEC is a promise about execution order on one node, not a promise about what survives when that node dies.

What MULTI/EXEC actually guarantees

MULTI            # start queuing
DECRBY A 100     # queued, not executed yet
INCRBY B 100     # queued, not executed yet
EXEC             # NOW all queued commands run, back-to-back, atomically
  • No interleaving — no other client's command can slip between DECRBY A and INCRBY B.
  • All-or-nothing execution — the queued block runs as one unit.
  • In-memory only — this all happens in RAM on the primary.

Why a failover still breaks it

Durability in Redis lives on two separate, asynchronous pipes: (1) AOF fsync to the primary's own disk, and (2) replication streamed to the replica after the client already got OK.

t0  Primary: MULTI; DECRBY A 100; INCRBY B 100; EXEC  → OK   (both applied in RAM ✓)
t1  Primary streams those writes to replica           → ASYNC, still in flight
t2  Primary CRASHES
t3  Cluster promotes the replica
t4  Replica never received the transaction
t5  New primary has NEITHER the debit nor the credit
t6  Old primary rejoins, sees higher configEpoch, discards its own version

The execution atomicity held perfectly on the primary — but the primary is gone, and durability rode a separate async pipe. Whatever hadn't reached the replica at crash time is lost, transaction or not.

"Debit committed, credit lost" — the precise version

Careful readers spot an imprecision: for a single MULTI/EXEC, Redis wraps it in MULTI/EXEC on the replication stream too, so the replica applies it all-or-nothing as well. A single transaction won't cleanly split. So the half-committed outcome happens in one of two realistic ways:

CaseWhat happens
1 — Separate writes
the truly partial case
t0  DECRBY A 100 → OK, replicated ✓
t1  INCRBY B 100 → OK, NOT yet replicated
t2  Primary crashes, replica promoted
    → replica has the debit, never got the credit
    → money vanished from A, never appeared in B
2 — Whole transaction lost
still corrupts the books
Even if both legs vanish as a unit, your ledger is still wrong — because the real-world action already happened based on the OK: the user saw "Transfer complete," a downstream API was called, a confirmation email went out. Reality and the ledger now disagree. Losing both legs "atomically" doesn't help when the outside world already acted on the acknowledgment.

Two different failure modes, don't conflate them

ProblemCauseSymptom
CROSSSLOT errorThe keys hash to different shards; a transaction can't span shardsCommand rejected up front — you never even get to run it
Failover data lossAsync replication drops acknowledged writesCommand succeeds, then silently disappears

CROSSSLOT is a design-time wall (multi-key ops must share a shard, often forced via hash tags {...}). Failover loss is a runtime durability gap. Both are reasons Redis is the wrong system of record for money.

Seeing it in Go

// --- Plain MULTI/EXEC via TxPipeline ---
// Both commands are queued and sent as one MULTI...EXEC block. They execute
// atomically ON THE PRIMARY, IN MEMORY. That is the ONLY guarantee — it says
// nothing about surviving a failover.
_, err := rdb.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
    pipe.DecrBy(ctx, "balance:A", 100) // debit
    pipe.IncrBy(ctx, "balance:B", 100) // credit
    return nil
})
if err != nil {
    log.Fatalf("txn failed: %v", err)
}
fmt.Println("EXEC returned OK — atomic on the primary, in RAM.")
fmt.Println("But this OK does NOT mean the write reached the replica's disk.")

// --- Optimistic locking with WATCH (check-and-set) ---
// WATCH aborts the EXEC if the key changed underneath us. Still only
// primary-side atomicity — WATCH adds no cross-failover durability.
transfer := func(tx *redis.Tx) error {
    bal, err := tx.Get(ctx, "balance:A").Int()
    if err != nil {
        return err
    }
    if bal < 100 {
        return errors.New("insufficient funds")
    }
    _, err = tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
        pipe.DecrBy(ctx, "balance:A", 100)
        pipe.IncrBy(ctx, "balance:B", 100)
        return nil
    })
    return err
}

// Retry loop: WATCH → if balance:A changed, EXEC fails with TxFailedErr.
for i := 0; i < 3; i++ {
    err = rdb.Watch(ctx, transfer, "balance:A")
    if err == nil {
        break
    }
    if errors.Is(err, redis.TxFailedErr) {
        continue // key changed mid-flight, retry
    }
    log.Fatalf("transfer error: %v", err)
}

The critical takeaway from the code: every OK above is a primary-in-memory guarantee. Neither TxPipeline nor WATCH waits for the replica. That is the exact gap.

Making a write actually wait for replicas: WAIT

// Block until 1 replica confirms, or 500ms passes.
acked, err := rdb.Wait(ctx, 1, 500*time.Millisecond).Result()
if err != nil {
    log.Fatal(err)
}
if acked < 1 {
    // Fewer replicas than required acked in time — durability NOT assured.
    log.Println("WARNING: write may be lost on failover")
}

This is why the conclusion is Postgres + synchronous replication: there, COMMIT itself does not return until the WAL record is flushed on both primary and replica disk. Atomicity and durability are bound into the same acknowledgment, not split across two async pipes.