Skip to main content
Interactive service-resilience Configurator →Open Architecture Blueprint ↗
P0 INCIDENTL7 BARM10 · 6 MIN READ

Distributed Lock Split-Brain & Monotonic Fencing Tokens

Learn why TTL-based distributed locks fail under GC pauses and network partitions, and how monotonic Raft term/index fencing tokens enforce linearizable writes.

PRODUCTION SYMPTOM & ALERT SIGNATURE

Two workers concurrently write conflicting state to the ledger after a 14-second stop-the-world GC pause, even though both workers acquired a 10-second distributed lock.

ledger_fencing_token_rejections_total == 0 AND ledger_duplicate_settlement_anomalies_total > 0
01 · ROOT-CAUSE MECHANICS (WHY IT HAPPENS)

A worker holding a 10s lease lock can freeze for 14 seconds during a JVM/Go GC pause, hypervisor steal, or TCP retransmission stall. While frozen, its lease expires and the lock service grants a fresh lease to Worker B.

When Worker A wakes up, it still believes it holds the lock and sends a stale write to the storage server, overwriting Worker B’s newer committed state.

Only the storage layer can prevent split-brain writes: the consensus lock service (etcd/Raft) must issue a strictly increasing fencing token (e.g., Raft revision / term+index), and the storage server must atomically reject any write whose token is less than the highest token already applied.

02 · LIVE TRIAGE COMMANDS (COPY & RUN)
Inspect etcd Raft leader term changes and election churn
$ etcdctl endpoint status --write-out=table
Smoking gun: Frequent RAFT TERM increments indicating leader instability or disk fsync stalls.
Check storage WAL fsync p99 latency causing Raft heartbeat misses
$ curl -s http://etcd:2379/metrics | grep wal_fsync_duration_seconds
Smoking gun: p99 wal_fsync_duration > 25ms starving Raft election timeouts.
Audit storage table for missing monotonic fencing_token guard column
$ psql -c "\d+ ledger_accounts"
Smoking gun: Verify a NOT NULL last_fencing_token BIGINT column enforced inside UPDATE ... WHERE.
03 · PRODUCTION DIFF (BROKEN VS HARDENED)
BROKEN · Broken Lock Pattern (time-only lease without storage-side fencing)
# Worker A acquires 10s lock, pauses 14s in GC, then overwrites Worker B!
if lock_service.acquire(account_id, ttl=10):
    db.execute(
        "UPDATE ledger_accounts SET balance = $1 WHERE id = $2",
        new_balance, account_id,
    )
HARDENED · Linearizable Raft Fencing Token Guard (enforced atomically at storage)
# Consensus lock service (etcd/Raft) returns monotonic revision token
lease = etcd_lock.acquire(f"/locks/account/{account_id}", ttl=10)
token = lease.mod_revision  # Strictly monotonic across leader elections

# Storage layer atomically rejects stale zombie writers!
rows = db.execute(
    """
    UPDATE ledger_accounts
       SET balance = $1, last_fencing_token = $2
     WHERE id = $3 AND last_fencing_token < $2
    """,
    new_balance, token, account_id,
)
if rows == 0:
    raise StaleFencingTokenError(f"Rejected stale lease token={token}")
04 · MINUTE-24 BAR-RAISER CHECKPOINT (L7 · M10)TEST YOUR INSTINCT · ZERO SIGNUP

Worker A holds a 10s Redis/etcd lock, freezes for 15s during a GC pause, and wakes up to write to PostgreSQL after Worker B already took the lock. What guarantees safety?

05 · POST-FIX VERIFICATION CHECKLIST
  • Confirm every mutating write guarded by a distributed lock passes a monotonic fencing token validated inside the storage transaction.
  • Add a chaos test injecting SIGSTOP (15s pause) on Worker A while Worker B acquires the lock and writes, verifying Worker A’s delayed write is rejected.
  • Keep Raft nodes on dedicated low-latency NVMe disks so WAL fsync p99 stays under 10ms.
06 · FREQUENTLY ASKED ENGINEERING QUESTIONS

Can Redis Redlock replace storage-side fencing tokens?

No. Any wall-clock lease algorithm without a strictly monotonic fencing token verified by the storage engine remains vulnerable to process pauses and asymmetric network delays.

Why does Raft quorum require 2f + 1 nodes (e.g., 3 or 5, never 2)?

Any two majorities in a 2f+1 cluster overlap by at least one node, guaranteeing the newly elected leader always sees the highest committed term and index.

What causes accidental Raft leader elections in healthy networks?

Slow disk fsync latency (e.g., shared noisy EBS volumes) stalls Raft WAL appends longer than election_timeout, causing followers to start spurious elections.