Cache replication and failover
medium 28 minReplication keeps copies of a cache on other servers.
Video lesson · Chapter 1 of 5
A second copy lags the first
Theory
Read or listen first. About 1 min.
What it is
Cache replication keeps the same cached data on more than one server. Usually one server, the primary, takes all writes and sends them on to replicas. Reads may go to any of them.
Failover is what happens when the primary stops: something notices, picks a replica, makes it the new primary and tells everybody else.
Why it exists
A cache often protects a database that could not serve all reads by itself. If the cache disappears, the database may follow. A warm copy that can take over keeps the site up.
Copies also add read capacity and can sit closer to users. They do not add write capacity: every replica must apply every write.
A short history
2003
Memcached is released without replication. Clients or layers around it add copies if they need them.
2007
Amazon's Dynamo paper describes a store where several servers accept writes and conflicts are resolved later.
2013
Redis 2.8 is released in November with a rewritten Redis Sentinel.
2015
Redis 3.0 adds Redis Cluster, with a replica promoted for a part of the keys when its primary fails.
Words to know
- Primary
- The server that takes writes. Older texts call it master.
- Replica
- A server that follows the primary. Often read-only.
- Replication lag
- How far a replica is behind, in time or in bytes.
- Quorum
- How many watchers must agree before something counts.
- SDOWN and ODOWN
- Subjectively down: one watcher thinks so. Objectively down: enough watchers agree.
- Split brain
- Two servers both act as the primary.
How it works
Step through the idea, or press Play.
Copy every write. The primary answers, then sends the write to its replicas
Understand it
Start here
The drummer and the understudy
A band has one drummer. At the side stands an understudy with a small practice pad, copying each beat a moment later.
The understudy was good because they had been copying all along, not because they were quick to learn.
The idea
Why copy something that can be rebuilt
Availability
Reads and writes go on after a server stops, without a storm of misses.
Read capacity
Reads can be spread over copies.
Data survives one loss
Most of it: the last unsent writes may not.
Nearer to users
A copy in another place can answer local reads.
A primary, two replicas and three watchers
Reads and memory
One writer
Primary and replicas
A write and two reads
Advantages
- One writer, so no write conflicts
- Reads grow with replicas
- Easy to reason about
- A warm copy is ready
Limits
- The primary is a single point of failure until failover works
- Writes are limited to one server
- Lag causes stale reads
- Failover needs coordination
When to answer
Asynchronous, synchronous and in between
| Mode | Answer the client | Write time | Loss when the primary dies |
|---|---|---|---|
| Asynchronous | At once | Lowest | Writes not yet copied |
| Semi-synchronous | When at least N replicas have it | One more trip | None if a replica that has it is promoted |
| Synchronous | When all replicas have it | That of the slowest replica | None, but one slow replica stops writes |
How much a failover can lose
Two settings that limit loss on the primary
| Setting in Redis | Meaning |
|---|---|
min-replicas-to-write | Refuse writes when fewer replicas than this are connected. Off by default |
min-replicas-max-lag | Count only replicas heard from within this many seconds. 10 by default |
Many writers
Several primaries
With several primaries, each takes writes and tells the others. Writing stays possible in each place, but two places can change the same key at the same time. Something must settle that.
Settling a conflict
| Way | Idea | Cost |
|---|---|---|
| Last writer wins | Keep the one with the later time | A write is dropped, and clocks differ |
| Mergeable types (CRDTs) | Types that merge the same way in any order | Only for some shapes of data |
| Vector clocks | Track what each write had seen, to detect a conflict | Something must still resolve it |
| Application code | Your own rule, such as joining two carts | Work for each type of data |
Noticing
Is it down, or just slow?
A watcher sends a ping and waits. No reply could mean a crash, a pause or a broken cable between the two. So one watcher's view is only an opinion.
From an opinion to a decision
Two numbers people mix up
| Number | What it decides | With 3 watchers and quorum 2 |
|---|---|---|
| Quorum | How many must agree that the primary is down | 2 |
| Majority | How many must authorise the failover | 2 |
Majority
Taking over
Failover in five steps
- 1
Detect
The watchers agree: the primary is objectively down.
- 2
Elect
One watcher is authorised by a majority to lead this failover, under a new version number.
- 3
Choose a replica
Skip those cut off too long. Then take the best priority, then the one that processed most data, then the smallest run ID.
- 4
Promote
Send
REPLICAOF NO ONEto it. It now takes writes. - 5
Reconfigure
Point the other replicas and the clients at it. When the old primary returns, it becomes a replica.
sentinel monitor mymaster 127.0.0.1 6379 2sentinel down-after-milliseconds mymaster 30000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 180000Automatic failover
How long writes are unavailable
Two at once
Split brain and empty primaries
Two primaries during a network split
Limit the old side
With a minimum number of replicas, the cut-off primary stops taking writes after a few seconds.
Place watchers well
Put them on machines that fail independently, so the majority is on the side that should win.
Empty restart
A primary without persistence that restarts by itself comes back empty, and its replicas copy the emptiness.
No automatic restart
In that setup, do not restart the process automatically. Let failover happen first.
Trade-offs
Choose for your cache
| Question | One primary | Several primaries | Quorum writes |
|---|---|---|---|
| Write while the primary is down | Not until failover | Yes, elsewhere | Yes, if enough servers remain |
| Conflicts | None | Must be settled | Handled by versions |
| Complexity | Low | High | High |
Do I need replicas?
Summary
The whole topic in one minute.
One primary takes writes, replicas copy them, usually after the client already has its answer. That makes writes fast and lets a replica be slightly behind, so reads can be old and a failover can lose the last moment.
A safe failover needs several watchers in different places, a quorum, a clear choice of replica, and clients that find the new primary. Limit the loss with settings, and make sure losing it does not matter.
- The primary takes writes, replicas follow with a small lag.
- Reads from replicas can be old, and a failover can lose the latest writes.
- One watcher has an opinion. A quorum decides, and a majority authorises.
- Promote the replica with the best priority and the most data.
- Clients must discover the new primary by themselves.