🎓 A1Learner
🔍
0 of 47 lessons completed

Scaling Database Reads

Scaling database reads means sharing read work across replicas so one database machine does not carry every read. A read replica is a follower copy that serves reads while the leader keeps taking writes.

Reads shared across replicas

This demo shares 100 reads across the leader and its replicas. Each machine prints the share it served.

machines = ["Leader", "Replica 1", "Replica 2", "Replica 3"]
counts = {}
for name in machines:
    counts[name] = 0
for i in range(100):
    machine = machines[i % len(machines)]
    counts[machine] = counts[machine] + 1
for name in machines:
    print(name + ":", counts[name], "reads")

Writes stay with the leader

This demo sends 10 writes through the same system. The counts show every write landing on the leader.

machines = ["Leader", "Replica 1", "Replica 2", "Replica 3"]
counts = {}
for name in machines:
    counts[name] = 0
for i in range(10):
    counts["Leader"] = counts["Leader"] + 1
for name in machines:
    print(name + ":", counts[name], "writes")

A read that arrives before the copy

The leader holds the new city and the replica still holds the old city. The read from the replica prints the older value until the copy arrives.

leader = {"city": "Porto"}
replica = {"city": "Lisbon"}
print("Leader city:", leader["city"])
print("Replica city before the copy arrives:", replica["city"])
print("Read from replica:", replica["city"])
replica["city"] = leader["city"]
print("Replica city after the copy arrives:", replica["city"])

Example

machines = ["Leader", "Replica 1", "Replica 2", "Replica 3"]
read_counts = {}
write_counts = {}
for name in machines:
    read_counts[name] = 0
    write_counts[name] = 0
for i in range(120):
    read_counts[machines[i % len(machines)]] += 1
for i in range(12):
    write_counts["Leader"] += 1
for name in machines:
    print(name + ":", read_counts[name], "reads,", write_counts[name], "writes")
leader = {"plan": "Premium"}
replica = {"plan": "Free"}
print("Read of plan from replica before the copy:", replica["plan"])
replica["plan"] = leader["plan"]
print("Read of plan from replica after the copy:", replica["plan"])

Key points

  • Read replica: a follower copy that serves reads for the leader.
  • Even share: each machine serves 25 reads when the reads are spread evenly across the leader and its replicas.
  • Lag: a replica can be a step behind the leader just after a write, before the copy arrives.
  • Use a read replica to add read capacity without changing how writes work.

In this example, reads spread across the leader and its replicas. Every write lands on the leader. A replica read taken before a copy arrives shows the older value. The same read shows the new value after the copy.

Note

A read that must show the newest value can go to the leader.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

A user updates a profile and then reads it back from a replica before the copy arrives. What can the read show?