Redis Tutorial 0/42 lessons ~6 min read Lesson 26

    Replication

    Replication streams writes from master to one or more replicas — read scaling and failover candidates.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Replication streams writes from master to one or more replicas — read scaling and failover candidates. Replicas are async by default; a read immediately after write on replica may be stale ( eventual consistency ).

    PSYNC partial resync avoids full RDB transfer on brief disconnect. Chain replication (replica of replica) adds lag — prefer star topology from master.

    Never run slow commands or KEYS on replicas serving production reads without understanding lag impact.

    Understanding the topic

    Key concepts

    • REPLICAOF host port — configure replica.
    • Async replication — master doesn't wait replica ack (unless WAIT).
    • INFO replication — lag, offset, role.
    • READONLY on replica rejects writes.
    • Replica serves reads to offload master.
    • WAIT numreplicas timeout — sync wait rare.
    text
    flowchart LR
    Master -->|stream| Replica1
    Master -->|stream| Replica2

    Step-by-step explanation

    1. Replica connects sending REPLCONF.
    2. Full sync: master BGSAVE RDB to replica.
    3. Partial sync: backlog offset catch-up.
    4. Master propagates command stream to replicas.
    5. Replica applies commands in order on event loop.

    Syntax reference

    Common commands

    • master_repl_offset vs slave repl offset — lag metric.
    • READONLY default on replicas.
    • Use WAIT only when strong need — hurts latency.
    bash
    INFO replication
    ROLE
    REPLICAOF redis-master 6379
    CONFIG SET replica-read-only yes

    Informative example

    Check replication lag during incident:

    bash
    redis-cli INFO replication
    # master_repl_offset:1000000
    # slave_repl_offset:999500 ← 500 bytes behind
    redis-cli -h replica.internal GET hot:key

    Large lag — network, slow replica CPU, or huge write burst. Don't read-your-writes from replica without routing logic.

    Real-world use

    Real-world use cases

    • Read scaling for cache-heavy GET traffic.
    • Hot standby for manual promotion.
    • Analytics SCAN on replica spare master.
    • Cross-AZ read local replica.
    • Backup BGSAVE from replica node.

    Best practices

    • Monitor repl lag alert >10MB or seconds.
    • Route sticky read-after-write to master.
    • Dedicated replicas for backup not prod reads.
    • min-replicas-to-write on master prevent split-brain writes.
    • Test promote replica drill.
    • Avoid KEYS on lagging replica.

    Common mistakes

    • Read-your-writes from replica after POST.
    • Writing to replica — fails or breaks topology.
    • Ignoring lag during incident debugging.
    • Too many chained replicas.

    Advanced interview questions

    Q1BeginnerReplication async?
    Default yes — master acks client before replica applies.
    Q2BeginnerReplica purpose?
    Read scale, backup source, failover candidate.
    Q3IntermediateReplication lag causes?
    Network, replica CPU, large writes, slow commands on replica.
    Q4IntermediateWAIT command?
    Blocks until N replicas acknowledge offset — rare strong consistency tool.
    Q5AdvancedPromote replica to master?
    REPLICAOF NO ONE; update apps; fix old master; resync — use Sentinel/Cluster automate.

    Summary

    Master streams writes to replicas async.

    Ready to mark this lesson complete?Track your journey across the entire course.