Published on

Cache Coherence and Consistency Models - A Guide for Distributed Systems

When designing a caching strategy for distributed systems or multi-core processors, it's essential to understand cache coherence and consistency models. These concepts ensure that data remains accurate and up-to-dateβ€”even when multiple caches are involved.

🧠 What is Cache Coherence?

Cache coherence ensures that all nodes or processors see a consistent view of shared data. In systems with multiple caches, each may hold a copy of a data item. If one copy is updated, all others must reflect that change.

βœ… Common Cache Coherence Techniques:

  1. Write-invalidate

    • When a cache writes to its local copy, it invalidates all other copies in other caches.
    • Other caches must fetch fresh data when needed.
    • βœ… Lower network usage; ❌ Possible performance hit on frequent invalidation.
  2. Write-update (write-broadcast)

    • When a cache writes, it broadcasts the updated value to all other caches.
    • All caches update their copies instantly.
    • βœ… Real-time data propagation; ❌ Higher network traffic.

πŸ“œ Cache Consistency Models

While coherence ensures all copies eventually agree, consistency models define how and when updates become visible across the system. Let’s break down the four consistency models β€” with scenarios, diagrams in words, and when to use which model in real systems.

πŸ” 1. Strict Consistency

βœ… Definition:

Every read must reflect the latest write, immediately and globally.

πŸ“ˆ Example Scenario:

Imagine a stock trading platform. When Alice buys 100 shares, Bob (on another server) should instantly see that change, or else trades could happen with stale data.

πŸ”§ Diagram (conceptual):

Write(W1) β†’ [Node A] Read(W1)
          β†’ [Node B] Read(W1)

Every node instantly reflects the latest value.

🟒 Use When:

  • Absolute accuracy is critical
  • Ex: Banking, real-time trading

πŸ”΄ Avoid When:

  • You have high network latency or distributed nodes across the globe
  • It’s nearly impossible to implement practically

πŸ“š 2. Sequential Consistency

βœ… Definition:

All operations appear in the same sequence for all nodes, but the system doesn't guarantee immediate visibility.

πŸ“ˆ Example Scenario:

In a shared document editor, two users type sequentially. Everyone should see edits in the same order, even if there's a slight delay.

πŸ”§ Diagram:

Client A: Write(W1) β†’ Read(W1)
Client B: Write(W2) β†’ Read(W1 β†’ W2)

All clients see the same order: W1 β†’ W2

🟒 Use When:

  • Order matters, but instant sync isn't required
  • Ex: Collaborative editing, logging systems

πŸ”΄ Avoid When:

  • You need higher performance and can tolerate slight disorder

πŸ” 3. Causal Consistency

βœ… Definition:

Only causally related operations must appear in order. Unrelated ops can appear in any order.

πŸ“ˆ Example Scenario:

In a social media feed:

  • Alice posts β€œI got the job!”
  • Then replies to her own post β€œSo excited!” Anyone seeing the reply should have already seen the original post.

But:

  • Bob posts a cat meme β€” not related to Alice’s post β€” and can appear in any order.

πŸ”§ Diagram:

Alice: Post P1 β†’ Comment C1
      Everyone must see: P1 before C1

Bob: Post B1 (unrelated) β†’ can appear anytime

🟒 Use When:

  • You have user interactions with logical cause-effect
  • Ex: Social networks, collaborative apps

πŸ”΄ Avoid When:

  • You need strict order for every operation

πŸ•’ 4. Eventual Consistency

βœ… Definition:

Every node will eventually reflect the latest value, but no guarantees on when or in what order.

πŸ“ˆ Example Scenario:

You change your Twitter bio β€” it might show the old bio for a few seconds on some devices, but it updates eventually.

πŸ”§ Diagram:

Write(W1) β†’ Node A (immediate)
          β†’ Node B (after delay)
          β†’ Node C (after longer delay)

🟒 Use When:

  • You care about availability and performance
  • Ex: DNS, NoSQL DBs, shopping cart systems

πŸ”΄ Avoid When:

  • Consistency is critical (e.g., banking, seat booking)

πŸ“Š Summary Table

ModelGuarantees Order?Instant Updates?Use Case ExampleReal-world Use
Strict Consistencyβœ… Yesβœ… YesStock TradingRare (theoretical)
Sequential Consistencyβœ… Yes❌ Not alwaysCollaborative editingDistributed logs, Paxos
Causal Consistencyβœ… Related only❌ DelayedSocial media, chat appsFacebook Feed, DynamoDB
Eventual Consistency❌ No❌ NoDNS, product views, commentsAmazon DynamoDB, CouchDB, Cassandra

βš–οΈ How to Choose?

You prioritize...Choose this model
πŸ’° Accuracy over everythingStrict / Sequential
🧠 Logical order mattersCausal Consistency
πŸš€ Speed and uptimeEventual Consistency

πŸ” Final Thoughts

Understanding cache coherence and consistency models is key to building robust, scalable systems. Whether you're optimizing for accuracy or performance, choosing the right model can make or break your application's reliability and speed.