Replication, Sentinel and Cluster
A single Redis server is fast, but it's also a single point of failure, and it's limited to one machine's memory. This final lesson covers the three ways Redis scales and stays available, plus security and a production checklist.
Replication
A replica keeps an exact copy of a primary (also called master). The primary streams every write to its replicas.
writes
app ───────────────▶ PRIMARY
│ replication stream
┌────────┴────────┐
▼ ▼
REPLICA 1 REPLICA 2 ◀──── reads (optional)# On the replica (or in redis.conf: replicaof 10.0.0.5 6379)
REPLICAOF 10.0.0.5 6379- Replicas are read-only by default, and can serve read traffic.
- Replication is asynchronous: a write acknowledged by the primary may not have reached replicas yet. If the primary dies at that moment, that write can be lost. (
WAIT 1 100makes a client wait for replicas to acknowledge, reducing that risk.) - Replicas also make great backup sources: snapshot the replica, not the busy primary.
The simulator is a standalone primary with no replicas: ROLE returns master, the replication offset, and an empty list of replicas. On a real primary with replicas, that list shows each replica's address and how far it has caught up.
Sentinel: automatic failover
Replication alone doesn't promote a replica when the primary dies. Redis Sentinel does:
- Several Sentinel processes (at least 3) monitor the primary.
- If a majority agree it's down, they promote a replica and reconfigure the others to follow it.
- Clients ask Sentinel "who is the primary for
mymaster?" instead of hard-coding an address.
// node-redis with Sentinel
import { createSentinel } from 'redis';
const client = await createSentinel({
name: 'mymaster',
sentinelRootNodes: [{ host: 'sentinel-1', port: 26379 }, { host: 'sentinel-2', port: 26379 }],
}).connect();Sentinel gives high availability for one dataset that fits on one machine.
Redis Cluster: sharding across machines
When data or write traffic outgrows one server, Redis Cluster splits keys across several primaries (each with its own replicas):
- The keyspace is divided into 16,384 hash slots.
- Each key belongs to slot
CRC16(key) mod 16384. - Each primary owns a range of slots; clients learn the map and send each command straight to the right node.
(A standalone server would reject CLUSTER commands; the simulator allows CLUSTER KEYSLOT so you can explore slots.)
The multi-key catch, and hash tags
In a cluster, a command touching several keys (MGET, SINTER, MULTI/EXEC, Lua scripts) only works if all keys are in the same slot. Otherwise you get a CROSSSLOT error. Hash tags fix this: if a key contains {…}, only the part inside the braces is hashed:
All of user 1001's keys land in the same slot, so they can be used together in one transaction. Choose tags carefully: if one tag gets most of the traffic, that node becomes a hotspot.
| Replication + Sentinel | Cluster | |
|---|---|---|
| Scales reads | ✅ (replicas) | ✅ |
| Scales writes and memory | ❌ (one primary) | ✅ (many primaries) |
| Automatic failover | ✅ | ✅ (built in) |
| Multi-key operations | Any keys | Same slot only |
| Databases | 0–15 | Only 0 |
Managed services (Redis Cloud, AWS ElastiCache / MemoryDB, Google Memorystore, Azure Cache, Upstash) handle replication, failover, patching and backups for you, and are what most teams use.
Security
Redis was designed for trusted networks. Out of the box:
- Never expose port 6379 to the internet. Bots scan for open Redis servers constantly, and an open server can be taken over. Bind to private interfaces (
bind), keepprotected-mode yes, and use firewalls or private networks. - Require authentication with ACLs (Redis 6+): separate users with limited permissions.
ACL SETUSER app on >S3cure-Pa55 ~cache:* ~session:* +@read +@write -@dangerous
ACL SETUSER metrics on >An0ther ~* +info +ping- Use TLS for connections that cross a network.
- Disable or restrict dangerous commands for app users (
FLUSHALL,CONFIG,KEYS,DEBUG).
That AUTH error means this server has no password at all: the default, and exactly what you must change before production. bind and protected-mode show the defaults that keep a fresh server reachable only from the same machine.
Production checklist
- Choose the right role: cache (
allkeys-lru, TTLs everywhere) or data store (noeviction, AOF + RDB) -
maxmemoryset below the machine's RAM (leave headroom for forks and buffers) - Replicas + Sentinel, Cluster, or a managed service with automatic failover
- Backups (RDB) copied off the server, with a tested restore
- Private network, ACL users, TLS; port 6379 never public
- No
KEYSin app code (useSCAN); no huge values; pipelining for bulk work - Monitoring: memory, evictions, hit ratio, connected clients, latency (
LATENCY DOCTOR,SLOWLOG GET), replication lag - Client libraries with timeouts, retries and connection reuse
🎓 Redis track complete!
You know every core data type (strings, lists, hashes, sets, sorted sets and streams), expiry, caching patterns, rate limiting, locks, transactions with WATCH, Pub/Sub, consumer groups, memory management, persistence, and how Redis scales.
Capstone project: a real-time poll service
Build a small API (Node.js, Python, or your favourite language) backed by Redis:
- Polls stored as hashes (
poll:{id}) with a title and options; votes counted withHINCRBY. - One vote per user: a set
poll:{id}:voters, checked withSADD(it returns 0 for repeat voters). Make the check and the vote atomic withMULTI/EXEC(or a Lua script). - Trending polls: a sorted set scored by votes in the last hour.
- Rate limiting: max 10 votes per minute per IP.
- Live results:
PUBLISH poll:{id}:updatesafter each vote; a WebSocket server subscribes and pushes to browsers. - Audit trail:
XADDevery vote to a stream, processed by a consumer group that writes to PostgreSQL. - Polls expire automatically 7 days after creation.
- Use hash tags (
{poll:id}) so each poll's keys would live in one cluster slot.
1.Why is Redis replication described as asynchronous?
2.In Redis Cluster, how do you make sure {user:42}:cart and {user:42}:profile can be used in one MULTI/EXEC?
3.What's the most important Redis security rule?