← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

Eventual Consistency in Databases

Database Advanced
debt(d7/e7/b7/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The term's detection_hints explicitly state 'automated: no' — there are no automated tools that can reliably detect eventual consistency misuse. Issues manifest as subtle data inconsistencies that only appear under load, during network partitions, or in production when users report seeing stale/conflicting data. Code review might catch obvious anti-patterns like immediate read-after-write, but the systemic issues require runtime observation.

e7 Effort Remediation debt — work required to fix once spotted

Closest to 'cross-cutting refactor across the codebase' (e7). The quick_fix mentions WebSocket/SSE but that addresses only the UI symptom, not the root consistency model choice. Fixing eventual consistency misuse—like switching from an AP database to a strongly consistent one for financial data, or implementing read-your-writes routing—requires changes across data access layers, potentially database migration, and rearchitecting how the application handles reads and writes. This isn't a single-file fix; it's a fundamental data layer change.

b7 Burden Structural debt — long-term weight of choosing wrong

Closest to 'strong gravitational pull' (b7). The choice of consistency model is architectural—it applies to web/api contexts and shapes how every feature that touches that data store must be designed. Once you've built features assuming eventual consistency (or incorrectly assuming strong consistency from an eventually consistent store), every new feature must account for this. The common_mistakes show this burden: routing decisions, data model choices for financial vs. non-financial data, and handling replication lag all flow from this one architectural decision.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception field directly states developers believe 'eventual consistency means data is always correct after a few milliseconds' when in reality 'under continuous write load, divergence can persist indefinitely.' This contradicts intuition from working with traditional single-node databases where writes are immediately visible. The common_mistakes reinforce this: developers assume sub-100ms lag, read immediately after write expecting consistency, and conflate different types of consistency issues.

About DEBT scoring →

Also Known As

eventual consistency BASE AP system stale reads

TL;DR

A consistency model where replicas are not immediately synchronised — all nodes will converge to the same state given no new writes, trading consistency for availability and partition tolerance.

Explanation

Eventual consistency (AP systems in CAP) allows reads from different nodes to return different values temporarily. DynamoDB, Cassandra, CouchDB are AP by default. Techniques: read-your-writes (user always reads from the node they wrote to), monotonic reads (user never sees older data than previously seen), causal consistency (operations that causally depend see each other in order), and vector clocks for conflict detection. PHP applications must handle stale reads explicitly — cache invalidation bugs are a form of eventual consistency issue.

Diagram

sequenceDiagram
    participant APP as PHP App
    participant PRI as Primary DB
    participant REP as Replica
    APP->>PRI: UPDATE cart - add item
    PRI-->>APP: OK - committed
    Note over PRI,REP: Replication lag: 200ms
    APP->>REP: SELECT * FROM cart
    REP-->>APP: Old cart - item missing!
    Note over APP,REP: Read-your-writes fix:<br/>route to primary after write

Common Misconception

Eventual consistency means data is always correct after a few milliseconds — eventual consistency guarantees convergence with no new writes; under continuous write load, divergence can persist indefinitely.

Why It Matters

A shopping cart stored in an eventually consistent DB can show different items to the user than what was actually ordered — understanding consistency models prevents data integrity bugs.

Common Mistakes

  • Reading from an eventual consistent store immediately after writing without read-your-writes routing.
  • Using eventual consistency for financial data — bank balances require strong consistency.
  • Assuming eventual consistency lag is always under 100ms — under network partitions it can be unbounded.
  • Conflating Redis replication lag with application-level eventual consistency bugs.

Code Examples

✗ Vulnerable
// Eventual consistency bug — reads stale data:
function addToCart(int $userId, int $productId): void {
    $this->cartDb->write($userId, $productId); // Written to primary
}

function getCart(int $userId): array {
    return $this->cartDb->read($userId); // Read from replica — may not have the write yet!
    // User adds item, sees empty cart — support ticket: 'cart lost my items'
}
✓ Fixed
// Read-your-writes: route to primary for consistency:
function addToCart(int $userId, int $productId): void {
    $this->cartDb->write($userId, $productId);
    // Invalidate cache — force next read from primary:
    $this->cache->delete("cart:{$userId}");
}

function getCart(int $userId): array {
    // Session-based routing: if user wrote recently, use primary:
    $db = $this->userWroteRecently($userId) ? $this->primary : $this->replica;
    return $db->read($userId);
}

Added 16 Mar 2026
Edited 22 Mar 2026
Views 99
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping S 0 pings M 1 ping T 0 pings W 1 ping T 0 pings F 2 pings S 0 pings S 0 pings M 3 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 2 pings M 0 pings T 2 pings W 0 pings T 2 pings F 0 pings S 3 pings S 0 pings M 1 ping T 0 pings W 1 ping T 0 pings F 1 ping S 1 ping S 0 pings M
No pings yet today
Amazonbot 1
Google 18 Amazonbot 10 SEMrush 8 PetalBot 8 Ahrefs 7 ChatGPT 4 Perplexity 3 Bing 3 Scrapy 3 Applebot 3 Unknown AI 2 Twitter/X 2 Meta AI 1
crawler 70 crawler_json 2
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
Database database A database is an organized collection of data stored electronically, designed so programs can efficiently retrieve, add, update, and delete information.

Nearly every application needs to remember information between sessions. Databases provide the reliable, fast, and organized storage that makes persistent data possible at any scale.

💡 Always use prepared statements with placeholders—never concatenate user input directly into database queries.

Ask Codex about Database →
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Use WebSocket for bidirectional real-time communication (chat, live collaboration) and Server-Sent Events for one-way server push (notifications, feeds) — SSE is simpler and works over HTTP/2
📦 Applies To
any web api
🔗 Prerequisites
🔍 Detection Hints
JavaScript polling every 1-2 seconds instead of WebSocket or SSE; real-time feature without persistent connection
Auto-detectable: ✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: File Tests: Update


✓ schema.org compliant