Course contentsShow
← Databases & SQL
Lesson 1356 of 1,95312. Key-Value and Caching EnginesFree lesson

Consistency Modeling

Understand the trade-offs between eventual consistency and strict linearizability.

In this lesson, you will learn how to navigate the fundamental tension between high availability and absolute data accuracy when designing distributed systems.

The Consistency Spectrum

When working with key-value and caching engines, you rarely get perfect performance and perfect consistency simultaneously. You are essentially choosing where your system falls on a spectrum between Eventual Consistency and Strict Linearizability.

Linearizability (Strict Consistency)

Linearizability provides the illusion that your database consists of a single copy of data, and every operation appears to happen instantaneously. If a client writes a value, every subsequent read—regardless of which node it hits—is guaranteed to see that update.

  • The Trade-off: To guarantee this, the system must perform complex coordination (like consensus algorithms or synchronous replication). This introduces latency and makes the system vulnerable to downtime if nodes cannot communicate.
  • Best for: Financial ledgers or inventory counts where "dirty reads" are unacceptable.

Eventual Consistency

Eventual consistency prioritizes availability and speed. A write is acknowledged as soon as it hits a local node, and the system propagates that change to other replicas in the background.

  • The Trade-off: A client might write data, but a subsequent read from a different node might return stale data for a short window of time.
  • Best for: User profile updates, social media feeds, or caching layers where seeing the "latest" version is less critical than keeping the site fast and responsive.

Concrete Illustration

Consider a cache key user_login_status:

-- Conceptual example of a read/write conflict
-- Node A performs the write:
SET user_login_status = 'online';

-- If Node B is lagging due to replication delay:
GET user_login_status; -- Might still return 'offline' (Stale Read)

In an eventually consistent model, Node B catches up milliseconds later. In a linearizable model, the system forces Node B to wait for the update from Node A before responding to the GET request, ensuring the most accurate data at the cost of higher latency.

Key Takeaway: Choose linearizability when data accuracy is your highest priority, but embrace eventual consistency when you need maximum performance and resilience against network partitions.