*Consistency Patterns

September 15, 2025

Why consistency patterns matter

Users care that what they read makes sense with what they just wrote. Systems care about availability and latency. Patterns help you choose acceptable guarantees per feature.

Models

  • Eventual consistency: replicas converge over time; great for feeds and caches.
  • Read‑your‑writes: your session sees your latest writes even if others don’t yet.
  • Monotonic reads: you never observe time going backward.
  • Quorums R/W: choose R and W such that R + W > N for read‑after‑write consistency.

Techniques

  • Session affinity or token‑based routing to a primary for read‑your‑writes.
  • Versioning with logical clocks to ensure monotonicity.
  • CRDTs for conflict‑free merges in collaborative apps.

Example: session‑bound consistency

// language-typescript
// After a write, pin user to primary for 30s to ensure read-your-writes
function withPrimaryAfterWrite(req, res, next) {
  if (req.user?.justWrote) {
    res.cookie('sticky', 'primary', { maxAge: 30000, httpOnly: true })
  }
  next()
}

Quorums in practice

-- language-sql
-- Cassandra CQL pseudocode
CONSISTENCY QUORUM;  -- W = 2, R = 2, N = 3 → R+W > N
INSERT INTO profile (id, name) VALUES (?, ?);
SELECT * FROM profile WHERE id = ?;

CRDT sketch (G‑Counter)

// language-javascript
class GCounter {
  constructor(nodes) { this.counts = Object.fromEntries(nodes.map(n => [n, 0])) }
  inc(node) { this.counts[node]++ }
  merge(other) { for (const n in other.counts) this.counts[n] = Math.max(this.counts[n]||0, other.counts[n]) }
  value() { return Object.values(this.counts).reduce((a,b) => a + b, 0) }
}

Analogy

Eventual consistency is like friends taking notes offline and syncing later. Read‑your‑writes is like checking your own notebook—it’s always up to date for you.

FAQ

  • Do I always need strong consistency? No. Use it for money and security; eventual is fine for feeds and analytics.
  • How to avoid user confusion? Add “last updated” hints and optimistic UI with background refresh.

Try it

Simulate replication lag and verify your UI behavior for read‑your‑writes and monotonic reads.

Modèles

  • Éventuelle: convergence avec le temps (caches, timelines).
  • Read-your-writes: sessions, sticky routing.
  • Monotonic reads: versionnement logique, horloges de Lamport.
  • Quorums R/W: R+W > N pour consistance forte observable.

Outils

  • Vector clocks, CRDT pour merges sans conflits.