ealeng

Episode 46 of 55 · Multi-region

Across an ocean: a write costs 154 ms, and losing a region sold 14 to 18 seats twice

A write from Europe to a primary database in the US took 154 ms at 75 ms each way, and a three-write transaction took 769 ms, because each statement waits for its own round trip. Writing to a database in the same region took 0.2 ms. With asynchronous replication, the US region confirmed about 193 bookings a second, and when it went dark, Europe was taking bookings again about 5.4 seconds after the cut, and sold 14 to 18 seats a second time. Synchronous replication lost nothing and sold nothing twice, but confirmed about 9 bookings a second before the failure.

Lab 19 · Multi-Region and Geo-Distribution12 min read
Video transcript8:43

0:00Cold open

0:01

My booking database lives in the US. I wrote to it from Europe, over a link slowed to New York-to-Singapore distance.

0:10

One write took a hundred fifty-four milliseconds. The same write to a database next door: under a fifth of a millisecond.

0:19

A transaction of three writes took seven hundred sixty-nine.

0:25

Then I switched off the whole US region. Europe took over, and with async copying, in every run it sold between fourteen and eighteen seats a second time.

0:37

So what does distance cost, and what happens when a whole region goes dark?

0:44Intro

0:46

Hands-on system design, part forty-six.

0:49

Today: what distance does to a write, and what you lose when a whole region goes dark.

0:56Meet Lucía

0:57

I’m Lucía, a platform engineer. Our booking database has one primary: the one database that takes writes. It’s in the US.

1:06

It has replicas, copies that follow it: one in Europe, one in Asia. Each sits in a different region, a group of data centres in one part of the world.

1:17

Between continents, a message takes tens of milliseconds each way, and nothing makes light go faster. There and back is called a round trip.

1:27

Think of a company ledger at head office in New York, with a copy at the Frankfurt branch. A write from Frankfurt is a phone call to New York and back. And DNS, which tells clients where the primary is, is a phone list each branch copies and re-checks only every five seconds.

1:4701 · What you need

1:48

To run it yourself, you need Docker. The databases are real Postgres. Distance is faked: a delay on the links, all on one laptop.

1:58

The quick tests run in a couple of seconds, without Docker. No cloud, and nothing to pay for.

2:0702 · The test

2:08

The test: each region is a set of containers on my laptop. A small relay, Toxiproxy, sits between them and holds each message back.

2:19

Zero, twenty, forty and seventy-five milliseconds each way. Roughly: one city, US coast to coast, New York to Frankfurt, and New York to Singapore.

2:31

For each path, two hundred writes, a hundred for the transactions, one after another, timed by the client.

2:4003 · Distance

2:41

First, a client in Europe. Writing to a separate database in its own region: under a fifth of a millisecond, at every distance.

2:51

Writing to the US primary: forty-four, then eighty-four, then a hundred fifty-four milliseconds as the distance grows.

3:01

Every time, one round trip: twice the distance, plus a few milliseconds. At the far end, about a thousand times the local write.

3:1204 · A transaction

3:13

Now a transaction from Europe: writes that all happen or none do. Begin, three inserts, commit. At seventy-five milliseconds each way, how long do you think it took?

3:26

Seven hundred sixty-nine milliseconds.

3:31

Each statement waits for its answer before the next one goes. Five statements, five round trips of a hundred fifty: seven hundred fifty.

3:42

So across regions, pack the work into one statement, not five that each wait.

3:4905 · Waiting for the copy

3:50

Now a client in the US, writing to the primary next door. With synchronous replication, the copying, the primary waits for Europe’s copy before it says yes. A hundred fifty-four milliseconds again: the round trip moved onto every write.

4:07

With asynchronous replication, it says yes first and copies later. About a fifth of a millisecond.

4:15

The catch: anything it said yes to but hadn’t copied yet exists only in the US. So I switched off the US.

4:2506 · Losing a region

4:26

The second test. The primary in the US. This time Europe is forty milliseconds away, about New York to Frankfurt, plus a replica in Asia. Clients in both regions book seats. Fifteen seconds in, the US region goes dark: every link to it is cut.

4:45

The clients find the primary through DNS. DNS here is a stand-in: a file the clients re-read every five seconds.

4:54

A failover script, which switches to the copy, sits in Europe and checks the US about every half second. After three failed checks in a row, about two seconds after the cut, part of that my test harness, it promotes the European replica: it becomes the primary. Then it updates DNS.

5:15

The first booking in Europe came about five point four seconds after the cut in the async runs, up to five point seven with sync. That’s the recovery time. The target a team sets for it is called the RTO.

5:3007 · In code

5:32

Each booking is one statement, so one round trip: add one to the event’s seat counter, and record that seat for this user. Whatever the counter reaches is your seat number.

5:43

And each client finds the primary through DNS, and keeps the answer until it’s five seconds old.

5:5208 · What was lost

5:53

With async replication: in every run, ten to thirteen bookings the US primary had confirmed were missing in Europe.

6:03

That’s about the last tenth of a second of US bookings: a few still crossing, most likely the rest saved after the link was already down.

6:13

And a lost booking came back as a seat sold twice. Europe’s counter never heard of those seats, so it handed out the same seat numbers again: fourteen to eighteen per run.

6:25

Why more double sales than losses? Most likely the old primary, still alive in the dark region, also saved a few bookings whose yes never got out.

6:37

With sync: nothing lost, and no seat sold twice, in all three runs.

6:4409 · The phone list

6:45

Back to those five point four seconds. Europe was promoted about halfway through.

6:52

The rest, about two point nine seconds, the European clients spent on an old DNS answer, still pointing at the dark US.

7:01

In this test, the clients re-checked DNS just after the cut, before Europe was promoted, so they kept the old answer five more seconds. In real life that wait is anywhere from zero to five.

7:1710 · What sync cost

7:18

And sync wasn’t free before the failure, either. With async, both regions together confirmed about a hundred ninety-three bookings a second. With sync: about nine.

7:30

Most likely: every booking updates one counter row, and with sync that row stays locked about eighty milliseconds while Europe confirms. So bookings go one at a time: about twelve a second at best. And most US bookings failed.

7:47

On one busy row, sync protected the data and nearly stopped the business before anything failed.

7:55Back to Lucía

7:56

So, three lessons.

7:59

Every write across regions pays a round trip, and a transaction pays one per statement.

8:06

Async loses whatever it hadn’t copied yet, and here it came back as seats sold twice.

8:13

And sync loses nothing, but puts the round trip on every commit, even when nothing has failed.

8:20

Next time: writes in every region at once, and who wins when two of them disagree.

8:27

For today: count the round trips.Lucía: Count the round trips.

8:31Summary

8:32

A write across regions costs a round trip; async loses whatever it hadn’t copied yet; sync pays on every commit.

Key takeaways

  • ✓A write to a primary in another region costs one round trip: 154 ms at 75 ms each way in the lab, against 0.2 ms for a write to a database in the same region, and the cost was 2 × the one-way delay plus a few milliseconds at every distance tested.
  • ✓A transaction of three inserts from Europe to the US primary took 769 ms, because it waits for BEGIN, each insert and COMMIT in turn, which is five round trips of about 150 ms, so across regions one statement beats five.
  • ✓Synchronous replication puts the round trip on every commit: a client sitting next to the US primary waited 154 ms per write, against 0.2 ms with asynchronous replication, which confirms first and copies later.
  • ✓When the US region was cut off, asynchronous replication lost 10 to 13 bookings the US had already confirmed in each of three runs, and the Europe copy sold 14 to 18 seats a second time, because it did not know those lost bookings had already taken their seats.
  • ✓Recovery took 5.4 seconds in the asynchronous runs: about 2.2 seconds to detect three failed health checks, 0.3 seconds to promote the European replica, and the rest waiting for clients to let go of a DNS answer cached for 5 seconds.
  • ✓Synchronous replication lost 0 bookings and sold 0 seats twice in all three runs, but it confirmed only 126 to 135 bookings in the 15 seconds before the failure, about 9 a second across both regions, against about 193 a second with asynchronous replication, most likely because each booking held one shared counter row for the 80 ms round trip.

Writing across an ocean was 154 ms a write in the lab, and a three-write transaction took 769 ms. When the US region was switched off, asynchronous replication lost the bookings it had confirmed but not copied, and Europe sold 14 to 18 of the same seats again. This chapter counts the round trips a write pays across regions, then looks at what replication has to wait for, and what a region failure does to the seats.

The examples come from Lab 19 (Multi-Region and Geo-Distribution) of the system design labs: script 01_latency.py for the distance test, and script 02_region_failover.py for the region loss. Read the setup carefully before the numbers. Every region ran as containers on one laptop, an Apple M4 Max with Docker Desktop, 16 virtual CPUs, and Postgres 17. The "regions" are not in different places. The distance between them is a delay that a relay called Toxiproxy adds to each cross-region link, in both directions. The databases are real Postgres, so the writes, commits and failovers are real. The delay is simulated. Each configuration is a single recorded run of the distance test, and three runs of the failover test.

The regions are named by the lab. us is the US region, which holds the primary: the one database that takes writes. eu is Europe, with a replica (a copy that follows the primary). ap is Asia, also with a replica. Throughout this chapter, "Europe" means eu and "the US" means us.

What distance does to a write

Light in fibre covers about 200 km in a millisecond, and no software makes it faster. A write from Europe to a primary in the US has to travel there and come back before the client hears "saved". That is a round trip. The lab measured the cost at four distances, with 200 writes per path, one after another:

Path 0 ms 20 ms 40 ms 75 ms
Client in Europe, to a database in Europe 0.15 ms 0.14 ms 0.18 ms 0.15 ms
Client in Europe, to the US primary (one write) 0.29 ms 43.8 ms 83.5 ms 154.1 ms

Those are medians, the p50 of each run. The delays are the one-way values, so at 75 ms the round trip is 150 ms, and the measured 154 ms is that plus a few milliseconds of overhead. The rule the numbers show is simple: a remote write costs one round trip, and a write in the same region costs almost nothing. At 75 ms, the remote write was about a thousand times slower.

The distance widget below uses these same medians, on one scale, so the local writes are slivers next to the others.

Try it · What distance does to a writerecorded run, 01_latency.py

75 ms each way (about New York to Singapore). Median (p50) write latency, 200 writes per path.

Client to its own region's database
0.14 ms
Client to the US primary (one write)
154.1 ms
Client to the US primary (transaction, 3 INSERTs)
768.8 ms
US client, synchronous replica in eu
154.4 ms
US client, asynchronous replication
0.20 ms

Bars share one scale, set by the 769 ms transaction. Toxiproxy adds the delay on one machine; real links add jitter and loss.

Think of a company ledger kept at head office in New York, with a copy at a branch in Frankfurt. A Frankfurt clerk who wants a line changed has to phone New York and wait for the answer, every time. The phone list the branches use to find head office is itself copied out every five seconds. That is the whole problem in one picture.

A transaction pays per statement

A transaction groups several writes so they all happen or none do. The client sends BEGIN, the statements, then COMMIT, and each one waits for its answer before the next is sent. The lab ran a transaction of three inserts from Europe to the US primary:

Distance (each way) One write Transaction of 3 inserts
20 ms 43.8 ms 217.9 ms
40 ms 83.5 ms 419.0 ms
75 ms 154.1 ms 768.8 ms

Counting the messages: BEGIN, three inserts and COMMIT are five statements, so five round trips. At 75 ms each way, five round trips of about 150 ms come to about 750 ms, and the measured 769 ms includes the overhead. Across regions, the number of round trips matters more than how much work the database does. Packing the work into one statement, such as a single insert that also records the seat, or a stored procedure, takes one round trip instead of five.

Synchronous or asynchronous: who waits for the copy

Replication is how the replica gets the primary's writes. There are two ways to wire it, and they differ on when the primary says "yes" to the client.

  • Synchronous: the primary does not confirm a write until the replica has it. If the replica is in another region, every write waits a round trip, even for a client sitting next to the primary.
  • Asynchronous: the primary confirms first and sends the write to the replica afterwards. The client's wait is local, but anything confirmed and not yet copied exists only in the primary's region.

The lab measured a US client writing to the US primary, with the replica in Europe:

Replication At 75 ms each way
Synchronous, replica in Europe 154.4 ms per write
Asynchronous 0.20 ms per write

The synchronous figure is the same round trip as before, now paid on every commit, even though nothing has failed. The asynchronous figure is the local cost. That gap is the reason people choose asynchronous replication, and the reason it can lose data, which is the next part.

Losing a region

The second test is the one that matters most. The primary is in the US, Europe is 40 ms away, and Asia has a replica too. Clients in the US and in Europe each make 100 bookings a second. Each booking is one statement that adds one to the event's seat counter and records the seat for that user, so the seat number is whatever the counter reaches.

Fifteen seconds into the run, the US region goes dark: every link to it is cut. Three things have to happen before traffic works again:

  1. Detection. A failover script running in Europe checks the US every 0.5 seconds. After three failed checks in a row, it decides the US is gone. That took about 2.2 seconds.
  2. Promotion. The script promotes Europe's replica to primary, so it accepts writes. That took about 0.3 seconds.
  3. Moving traffic. Clients find the primary through DNS, the name service that maps a name to an address. In the lab, DNS is a stand-in: a file that each client reads and caches for 5 seconds. Clients keep the old answer until it expires.

The recovery time, the RTO (recovery time objective: how long the service is down), was 5.4 seconds in every asynchronous run. Half of it was the DNS wait, as the caveats below explain.

What a region loss does to the seats

With asynchronous replication, the new primary in Europe is missing the bookings the US had confirmed but not yet sent across the 40 ms link. The lab counted them against the bookings the US had confirmed:

Replication Confirmed by the US, per run Confirmed but missing in Europe Seats sold twice
Asynchronous (3 runs) 2,893 to 2,903 10 to 13 14 to 18
Synchronous to Europe (3 runs) 126 to 135 0 0

The loss is small, about 0.1 seconds of US bookings at 98 a second. The cost is larger than the loss. A confirmed booking that the new primary never received is a seat the new primary does not know is taken. Europe's counter hands the same seat number out again, to someone else. So a lost booking becomes a seat sold twice, and the lab saw more double sales than losses, because the old US primary was still alive in the dark region and may have saved a few bookings whose confirmation never got out. That part is the lab's most likely explanation, not something it tested directly.

The lesson is not that rows go missing. It is that two customers can end up holding the same seat.

With synchronous replication, the US primary waited for Europe before it confirmed each booking. Nothing confirmed was lost, and no seat was sold twice. The price came before the failure.

The workflow and the results

The failover test ran three times per replication mode. The table is the lab's recorded results:

Run Mode US bookings/s before the cut Confirmed by US Lost Sold twice Detected Promoted RTO
1 Async 97.7 2,893 11 18 2.2 s 2.5 s 5.4 s
2 Async 97.9 2,903 10 14 2.2 s 2.5 s 5.4 s
3 Async 97.8 2,898 13 17 2.2 s 2.5 s 5.4 s
1 Sync 3.7 126 0 0 2.3 s 2.6 s 5.6 s
2 Sync 2.7 135 0 0 2.3 s 2.5 s 5.4 s
3 Sync 3.9 127 0 0 2.4 s 2.7 s 5.7 s

Two things are worth reading off the table.

Throughput before the failure. Asynchronous confirmed about 2,898 bookings in the 15 seconds before the cut, which is about 193 a second across both regions. Synchronous confirmed 126 to 135, about 9 a second. The lab's explanation: each booking updates the same event counter row, and with synchronous replication that row stays locked for the 80 ms round trip to Europe. Bookings across both regions went through one at a time, so the best case was about 12 a second. Also, 1,432 to 1,451 US bookings timed out waiting for the lock in each sync run. The lab did not profile the database to confirm the lock is the cause, so read it as the most likely cause.

Where the RTO goes. The 5.4 seconds breaks into about 2.2 seconds of detection, 0.3 seconds of promotion, and about 2.9 seconds of clients still holding the old DNS answer. The single-region failover in the earlier Lab 18, with no distance, took 2.3 seconds, so a region adds detection you have to be sure of, and traffic you have to move.

Try it · Losing the US region, async vs syncrecorded runs, 02_region_failover.py

The US primary confirms a booking before Europe has it. When the region goes dark, the confirmed-but-unreplicated bookings are gone from the new primary, and Europe's copy never heard of them, so it can sell the same seats again.

Lost, per run
10–13
Sold twice, per run
14–18
RTO (3 runs)
5.4–5.4 s
Confirmed per second
193
RunUS bookings/s beforeConfirmed by USLostSold twiceDetectedRTO
197.72,89311182.2 s5.4 s
297.92,90310142.2 s5.4 s
397.82,89813172.2 s5.4 s

Confirmed per second = confirmed bookings in the 15 s before the cut, averaged over the three runs (async about 193, sync about 9 for both regions together). One machine, Toxiproxy delay, not a real ocean.

Caveats

  • The distance is simulated, on one machine. All three regions were containers on one laptop, joined by Toxiproxy delay. The results describe what that delay does to real Postgres. They do not include the jitter, packet loss or bandwidth limits of a real link, which all make cross-region writes slower and less predictable.
  • The DNS share depends on the harness. In these runs the clients re-checked DNS just after the cut, before the promotion, so they kept the old answer for the full 5 seconds. The 2.9 seconds is that phase, not a law. In real life, the wait after promotion can be anywhere from zero to five seconds, depending on when the client last looked.
  • One kind of failure. The lab cut every link to the US, which is a clean failure. It did not test a region that is slow, partly reachable, or flapping, and it did not test what happens when the old primary comes back and has writes the new one lacks.
  • Failover to one replica. Only the European replica was promoted, by a script with a fixed rule of three failed checks. There was no quorum (a vote among regions) and no test of two regions disagreeing about who is primary.
  • Small runs. Three failover runs per mode and one distance run are enough to show the pattern. They are not enough for tail statistics across many failures.
  • Not tested here: writes in every region at once (the next chapters cover that with last-write-wins and CRDTs), routing by location, data residency rules, and the lab's suggested fix of a shorter DNS cache lifetime (TTL, time to live), which the lab lists as an exercise.

The checklist

  1. Count the round trips. A write across regions costs one round trip, and a transaction costs one per statement. Send one statement where you can.
  2. Know what asynchronous replication loses. Everything confirmed but not yet copied is at risk. Decide whether losing it is acceptable, and what the system does with the seats, orders or balances it touched.
  3. Keep synchronous replication off hot rows. A round trip paid while holding a shared counter row turns into a queue. Split the row, or do not hold it across the round trip.
  4. Budget the failover. Detection, promotion and DNS caching all add to the recovery time. Measure each one separately.

Quiz

Try it · Check your understanding0 of 3 answered
  1. 1.A client in Europe writes one row to a primary database in the US, 75 ms each way. Roughly how long does the write take, and why?

  2. 2.Asynchronous replication lost 10 to 13 confirmed bookings, but 14 to 18 seats were sold twice. Why more double sales than losses?

  3. 3.With synchronous replication, the lab confirmed about 9 bookings a second across both regions, against about 193 with asynchronous replication. What is the most likely cause?

Check your understanding

Try each question before opening its answer.

A write from Europe to the US primary costs 154 ms at 75 ms each way, but a write to a database in Europe costs 0.2 ms. What is the 154 ms made of?

One round trip across the link: 75 ms out and 75 ms back, which is 150 ms, plus a few milliseconds of overhead. The local write never leaves the region, so it pays almost nothing for distance.

Why did the three-insert transaction take about five times as long as one write?

Because it was five statements, each waiting for its answer before the next was sent: BEGIN, three inserts and COMMIT. Five round trips of about 150 ms come to about 750 ms, and the measured 769 ms includes overhead. Sending the work as one statement takes one round trip.

Asynchronous replication lost 10 to 13 bookings in each run. Why did 14 to 18 seats end up sold twice?

Because the European copy never received the lost bookings, so it did not know those seats were taken, and it handed them out again. The old US primary, still alive in the dark region, may also have saved a few bookings whose confirmation never reached a client. That second part is the lab's likely explanation, not something it tested directly.

With synchronous replication, why did the sites confirm only about 9 bookings a second, when the failover itself lost nothing?

Because every booking updated one shared counter row, and with synchronous replication the row stayed locked for the 80 ms round trip to Europe. Bookings in both regions went through that row one at a time, so the best case was about 12 a second. Nothing was lost, but the business nearly stopped before the failure.

The RTO was 5.4 seconds. How much of it was waiting for DNS, and why does that number change in real life?

About 2.9 seconds. Detection took about 2.2 seconds and promotion about 0.3 seconds. The remaining wait was clients holding a DNS answer cached for 5 seconds. In these runs the clients had re-checked just before the promotion, so they waited the full cache time. In real life, the wait depends on when each client last looked, and it can be anywhere from zero to five seconds.

Next up: Chapter 47, last-write-wins vs CRDTs: this chapter kept one primary, and every region still had a single place that took writes. The next one lets every region write at once, and asks what happens to a like count, a shopping cart and a shared document when two regions change the same thing.

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.