- 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:
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.
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
Model | Guarantees Order? | Instant Updates? | Use Case Example | Real-world Use |
---|---|---|---|---|
Strict Consistency | β Yes | β Yes | Stock Trading | Rare (theoretical) |
Sequential Consistency | β Yes | β Not always | Collaborative editing | Distributed logs, Paxos |
Causal Consistency | β Related only | β Delayed | Social media, chat apps | Facebook Feed, DynamoDB |
Eventual Consistency | β No | β No | DNS, product views, comments | Amazon DynamoDB, CouchDB, Cassandra |
βοΈ How to Choose?
You prioritize... | Choose this model |
---|---|
π° Accuracy over everything | Strict / Sequential |
π§ Logical order matters | Causal Consistency |
π Speed and uptime | Eventual 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.