Skip to content

Global messaging

The chat case study ended with a service that works in one region. This lesson takes the same service to users on three continents. It adds three problems that one region does not have: a message can have to cross an ocean to reach its recipient, the members of one conversation can be written to from different regions at once, and some users’ messages may not be kept outside their own region. The lesson does not repeat how gateways, the registry, the store and the delivery workers work. It treats the whole of the chat design as one unit, a chat stack, and asks how many of them to run and how to connect them.

Functional requirements. The same as chat: one-to-one and group messages, delivered at once online and later offline, in one order per conversation. Add: a user is served by a region near them, and a conversation can have members in several regions.

Non-functional requirements. These are the targets we choose, not figures from a real service. A user sees a message accepted in under 200 ms wherever they are. A message that crosses regions arrives in a few seconds at worst. The service survives the loss of one region. A user’s messages are kept only in regions their rules allow.

Scale. 200 million daily users, each sending 30 messages a day, 40% in North America, 30% in Europe and 30% in Asia-Pacific. 15% of the messages go to someone whose home region is another one. As in chat, 20% of the daily users are connected at the busiest moment.

Mental model. Chat in one region is one post office. Global messaging is a post office in each of three countries and a mail plane between them. Most letters never use the plane. The design is about what happens to the ones that do: how long they wait, in what order they arrive, and which post offices are allowed to keep a copy.

The lesson on why go multi-region names three reasons: latency, availability and data residency. Here all three apply, and they are worth separating, because they ask for different things. Latency asks for a region near each user. Availability asks for spare capacity in the regions that remain. Residency asks for control over where copies exist, and it can force a region to exist at all.

Quantity Working Result
Messages per day 200 million × 30 6 billion
Messages per second, on average 6 billion ÷ 86,400 about 69,000
Peak, assuming 3 times the average 69,444 × 3 about 208,000, rounded to 200,000 messages per second
Messages that cross regions 15% of 200,000 30,000 per second
Traffic between regions 30,000 × 200 bytes about 6 MB/s, which is 48 Mbit/s
Copies written if every region keeps every message 200,000 × 2 other regions 400,000 per second
Copies written if only the regions that need one do 30,000 × 1 other region 30,000 per second
Storage per year, one copy of every message 6 billion × 200 bytes × 365 about 438 TB
Storage with 3 replicas in a region, and every region keeping everything 438 TB × 3 × 3 about 3.9 PB
Storage with 3 replicas, and a second copy only for the 15% that cross 438 TB × 1.15 × 3 about 1.5 PB
Mean round trip, user to a single region in North America 0.40 × 20 + 0.30 × 80 + 0.30 × 160 80 ms
Mean round trip, user to the nearest of three regions all users 20 ms 20 ms

The 15%, the 3 times, the 200 bytes and the round trips (20 ms nearby, 80 and 160 ms to the next and the far continent) are assumptions. Three things follow whatever the exact values. Bandwidth between regions is small, 48 Mbit/s: this is not a bandwidth problem. The volume of copies depends entirely on a rule: 400,000 a second if every region keeps everything, 30,000 if only the regions that need a message get one, a factor of 13. And the distance is paid by the users: 80 ms on average to a single region, against 20 ms with one in each continent.

Nothing changes for the phone. It opens one WebSocket to the nearest region, and sends and receives the same frames as in chat. What changes is the data. Every conversation now has two more fields.

Field Meaning
home The region that numbers the conversation’s messages. It is the only place where seq is assigned.
allowed The set of regions that may hold the conversation’s messages, decided by a policy from the rules of its members. The home is one of them.

A message is (conversation, seq, id, sender, body), as before. The seq comes from the home region, and a copy of the message in another region is the same row with the same seq.

The lab starts with the chat design in one region and adds one thing at a time. One request in it is one message sent, and the chat stack is one component whose capacity is what the last stage of the chat lab had: about 107,000 messages per second. Each stage is designed for a load, and the slider raises it to 3 times and 10 times that. The stage loads rise, from 15,000 to 200,000, because each stage can carry more than the one before it. Every figure is model output with illustrative inputs.

The chat service of the previous case study, now for users on three continents: messages that cross regions, one order per conversation across regions, and data that must stay where its owner’s rules say. Here one request is one message sent. Step through five stages, each adding the one thing the previous stage’s numbers called for, and raise the load to see what breaks.

Stage 1 of 5

Start with the Chat design in one region

Design load of this stage: 15K req/s

What we add
The complete Chat design (gateways, registry, store, log and delivery workers) runs as one stack in one region, in North America, and every user in the world connects to it.
Why: the number that forced it
One stack takes about 107,000 messages per second, the limit of the last stage of the Chat case study, and at 15,000 it is at 14%. The cost is distance: with users split 40%, 30% and 30% between three continents, the mean round trip to a single region in North America is `0.40 × 20 + 0.30 × 80 + 0.30 × 160`, which is 80 ms, and 95% of messages are handled within 88 ms.
What it costs
No new part to run, and one region to keep in step with itself. The price is that every user in Europe and Asia-Pacific pays the distance on every connection and every message, that one region is one failure domain, and that all the data lives in one jurisdiction.
What breaks next
At 10 times the load, 150,000 messages per second, the one stack is at 141% of its capacity and 29% of the messages fail, and the planet’s peak is about 200,000.
Design load (15K req/s)
80 ms0.3 msUsersworldwideHealthy<1%Global router(GeoDNS)Healthy<1%Chat stackin a regionHealthy14%
80 ms0.3 msUsersworldwideHealthy<1%Global router(GeoDNS)Healthy<1%Chat stackin a regionHealthy14%

Each dot stands for many requests. The numbers show the real rate.

No bottleneck: every component has headroom.

Figures at this load

Busiest component
Chat stack in a regionHealthy14% of its capacity
p95, whole request
88 ms
p99, whole request
90 ms
Requests served
100%15K req/s
Requests failed
0%0 req/s
Bottleneck
none
0 ms
Metrics
ComponentStateUtilisationOffered (req/s)Served (req/s)Rejected (req/s)p50p95p99
Users worldwideHealthy<1%15K15K00 ms0 ms0 ms
Global router (GeoDNS)Healthy<1%15K15K00.03 ms0.15 ms0.23 ms
Chat stack in a regionHealthy14%15K15K02.1 ms9 ms14 ms
Traffic
15,000 req/s
Successful
15,000 req/s
Error rate
0%
Mean latency
83 ms
All stages at a glance

Each cell is the engine’s steady state for that stage at that load: the busiest component, how much of its capacity it uses, and the share of requests that fail.

All stages at a glance
StageDesign loadDesign load3× the design load10× the design load
1. Start with the Chat design in one region15K req/s15K req/sChat stack in a region: 14%none fail45K req/sChat stack in a region: 42%none fail150K req/sChat stack in a region: 141%29% fail
2. Run a chat stack in each of three regions60K req/s60K req/sCross-region calls: 38%none fail180K req/sCross-region calls: 113%1.7% fail600K req/sCross-region calls: 200%51% fail
3. Carry messages between regions through a relay log70K req/s70K req/sCross-region relay log: 35%none fail210K req/sCross-region relay log: 105%none fail700K req/sChat stack in a region: 219%54% fail
4. Give each conversation a home region and split the relay by conversation80K req/s80K req/sCopies written in the other regions: 36%none fail240K req/sCopies written in the other regions: 107%none fail800K req/sChat stack in a region: 250%60% fail
5. Keep each conversation in the regions its members’ rules allow200K req/s200K req/sChat stack in a region: 63%none fail600K req/sChat stack in a region: 188%47% fail2M req/sChat stack in a region: 625%84% fail

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • Requests arrive at random, as a Poisson process, at a constant average rate.
  • Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
  • Service times are random with the stated mean; by default they are exponentially distributed.
  • Each node is analysed separately, with the traffic it receives treated as Poisson.
  • One request is one message sent. Users are 40% in North America, 30% in Europe and 30% in Asia-Pacific, and 15% of the messages are for a recipient homed in another region. The same 15% is used for the messages sent by a member who is outside the conversation’s home region. These are teaching figures, not measurements of a real service.
  • A chat stack is one component with the capacity of the last stage of the Chat case study: 106,667 messages per second. A round trip is 20 ms to the nearest region, 80 ms to a region on the next continent and 160 ms to the far one, and a call between regions is taken to take 100 ms on average. These are illustrative inputs, not measurements.
  • Every number (service times, capacities, the design loads) is illustrative model output, chosen to make the limit of each stage visible. None of them is a benchmark of a real database, cache or server.

Simplifications

  • Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
  • Percentiles assume an exponential tail. A service-time variability other than exponential changes the mean wait but not the shape of the tail.
  • A node frees its slot as soon as it has done its own work, before waiting on the services it calls. Real thread-per-request servers hold the slot, so a slow dependency exhausts them faster than this model shows.
  • A request calls its dependencies one after another. Parallel fan-out is not modelled.
  • When a caller times out and gives up, the callee still finishes the work, so abandoned requests keep consuming capacity.
  • Retries add load. Their backoff and jitter decide when that load arrives, which only the per-request simulation shows.
  • Retry feedback is solved from the healthy starting point, so it finds one stable outcome. Systems that stay stuck in overload after the cause is gone are not shown.
  • A throughput limit (for example maximum writes per second) is modelled as slower service, not as a separate rate limiter.
  • A caller’s timeout counts the mean time its dependencies take, not their full distribution.
  • Latency percentiles for a whole request are estimated, over every request, including those that are rejected and fail quickly. The mean is exact, and the percentiles come from a gamma distribution that has that mean and the variance of the time spent in each component.
  • Each stage is shown in a steady state: the load is held and the engine says where queues build. A region failing, a link going down and a deploy are not modelled; the lesson explains what each stage does then.
  • The engine measures load, not guarantees. Order, duplicates and lost messages are not simulated. An asynchronous relay that falls behind shows as a load above its capacity and no failed message, because the sender does not wait for it; in a real system that is delivery that arrives later and later.
  • The three regions are drawn as one component each, with their capacities added, and copies written in a region by the relay are counted in a separate component. Their inner structure is the Chat case study’s.
  • Residency is modelled only as which copies are made. Which law applies to whom, where metadata, backups and logs live, and how a user moves between regions are not modelled, and nothing here is legal advice.

Parameter values are illustrative. They are not benchmarks of any real product.

Stage 1, the chat design in one region. One stack takes about 107,000 messages per second, and at 15,000 it is at 14%. The cost is distance, not load: the mean round trip to a single region in North America is 80 ms, and 95% of messages are handled within 88 ms. At 10 times the load, 150,000 messages per second, the stack is at 141% and 29% of the messages fail. The planet’s peak is about 200,000, so the single stack is too small, and users on two continents are far from it.

Stage 2, a chat stack in each of three regions. Three stacks take 320,000 messages per second, and each user connects to the nearest, 20 ms away. At the stage’s 60,000 messages per second the stacks are at 19%. A message for a user homed in another region, 15% of the messages, is delivered by a synchronous call to that region, which takes about 100 ms. The 9,000 calls a second keep 900 connections busy out of 2,400: 38%. The send path now depends on the other region, and the slowest 5% of messages take 145 ms. At 3 times the load, 180,000 messages per second, the stacks are at 56% but the calls ask for 27,000 a second against 24,000: the slowest 5% of messages take about 7.5 s and 2% of the messages fail.

Stage 3, a relay log between regions. The sender’s stack no longer calls the other regions. It appends the message to a relay log twice, once for each other region, and answers the sender at once; the other regions read the log and write the message into their stores. The slowest 5% of messages take 28 ms again, instead of 145. At 70,000 messages per second the relay takes 140,000 appends a second, 35% of its 400,000, and the other regions write 140,000 copies a second, 31% of their 450,000. At 3 times the load, 210,000 messages per second, the relay is asked for 420,000 appends against 400,000, which is 105%, and the copies written in the other regions are at 89%. No sender gets an error, because it does not wait. The relay falls behind, and the other regions get messages later and later. A single stream also makes no promise about order when two regions append to the same conversation at the same moment.

Stage 4, a home region for each conversation, and a relay split by conversation. A message from a member in another region first goes to the conversation’s home region, in one synchronous hop, where it receives the next number. Then it is appended to the relay, which is split into 8 partitions by conversation id, and every region reads each partition in order. The 8 partitions take 2,400,000 appends a second, and the 420,000 that overloaded the single stream are 18% of that. The price is in the latency: a member elsewhere waits for a round trip to the home, and the slowest 5% of messages are back at 145 ms. At 3 times the load, 240,000 messages per second, the copies written in the other regions are 480,000 a second against 450,000, which is 107%.

Stage 5, a residency policy and selective copies. A policy service says which regions may hold each conversation, and a message is copied only to a region that has a member and is allowed. At the planet’s peak, 200,000 messages per second, copying every message to both other regions would be 400,000 copies a second, 89% of the 450,000 the regions can write. Copying only the 15% that cross, to the one region concerned, is 30,000 copies a second, 7% of that capacity. The policy is consulted for every message, at 10% of its capacity. At 3 times the load, 600,000 messages per second, the three stacks are at 188% and 47% of the messages fail. The next steps, which the lab leaves out, are to split each stack into independent cells, to add regions nearer to the users, and to shed load to protect the rest.

One order per conversation, across regions

Section titled “One order per conversation, across regions”

Inside one region the order is easy: the partition of the conversation numbers its messages. Across regions it is not. Two members of a group, one in Europe and one in Asia-Pacific, send at the same moment. If each region numbers its own messages and relays them, each region sees its own message first, and the two orders disagree. Ordering by the time on the sender’s clock does not fix it: the lesson on replication and conflicts shows how last-write-wins loses a write, and how a clock 0.5 s behind makes the older write look newer.

There are three honest answers.

  1. One writer per conversation. Stage 4’s answer: one region, the home, numbers every message of the conversation. The messages of other regions travel to it first. The order is whatever order the home accepted, and every region applies the messages in that order. It is simple and gives one total order per conversation, and it costs a round trip for the members who are not in the home region.
  2. A logical order. Lamport’s paper defines the order in which events could have affected each other, using counters instead of clocks, as the lesson on logical clocks shows. Messages are then ordered by counter, with the sender’s id to break ties. This needs no home, and the order of two concurrent messages is arbitrary, but it is the same everywhere. A message can appear above one a member has already read, once it arrives late.
  3. A partial order. The Matrix specification links the events of a room into a directed acyclic graph, which it describes as a partial ordering: each event points to the events it follows. Servers that disagree reconcile by a defined rule instead of by a single writer. It avoids the round trip and the home, and the price is a client that has to display a graph and a rule that has to be implemented and tested.

The lab models the first. It is the one with a number to show: the hop to the home region is the extra 117 ms of the slowest 5% of messages between stage 3 and stage 4 (145 ms against 28).

If the home region is lost, a conversation cannot take new messages from members elsewhere until another region takes over. Taking over needs a decision that no two regions can make at once, so it uses a lease with a fencing token: the new home gets a higher token, and a stale home that comes back is refused when it uses its old one.

Residency is a rule about where copies may exist. The multi-region lesson notes that Article 44 of the GDPR says a transfer of personal data to a third country takes place only if its conditions are met. The course gives no legal advice, and the rules for your users come from the law that applies to them. What a design can do is make the rule enforceable.

Three consequences matter for a messaging service. First, a copy is any place the message ends up: the store, backups, search indexes, logs, the relay log, and push payloads. A rule that is applied only to the store is not applied. Second, the home region of a conversation is chosen among the regions all its members allow, so a conversation between a European and an American user needs a policy that says what happens when their rules differ. That is a product decision, and the infrastructure only carries it out. Third, copies are selective: in stage 5 a message goes only to a region that has a member and may hold it, which is also what makes the copy volume 30,000 a second and not 400,000.

A region fails at the peak. The router sends its users to the two remaining regions, as in the lesson on regional failure. Two stacks take 2 × 106,667, which is 213,333 messages per second, against the peak of 200,000: they are at 94%, with almost no room. That is the price of availability: spare capacity in every region, or a plan to shed load. Slack describes the same shape in its gateway servers, which run in several geographic regions, with a draining mechanism that moves the users of a bad region to the nearest good one.

The messages that the lost region had accepted and not yet relayed are in it, out of reach until it returns. How many depends on the lag of the relay, as the replication lesson showed for the recovery point. A conversation whose home was in the lost region needs a new home, as above.

Section titled “What happens if the link between two regions is cut”

Inside each region everything continues. The 15% of messages that cross wait in the relay log: 30,000 a second is 1.8 million messages a minute, so ten minutes leaves 18 million. When the link returns, the copies drain at what the receiving regions can write beyond their normal load: about 450,000 - 30,000, which is 420,000 a second, so the backlog of ten minutes takes about 43 s to clear. During the cut, a user sees their own side of every conversation, and the messages from the other side arrive in a burst afterwards, in the order of each conversation’s numbers. Every copy carries its id, so a retried copy is ignored. What does not work during the cut is a conversation whose home is on the other side of the link: its members here cannot get a message numbered.

Each row is a decision in the lab, written with what it buys, what it costs and when the other choice is better.

Decision What you gain What you pay When to choose otherwise
A stack in each region Users 20 ms from a region, and capacity that adds up Three stacks to run, and spare capacity in each When users are all in one place, or one region meets the availability target
An asynchronous relay between regions The sender does not wait, and a slow region does not slow it Delivery that is late when the relay is behind, copies that can repeat When the other side must confirm before the sender goes on, as for a payment
One home region numbers each conversation One order per conversation for everyone A round trip for members elsewhere, and a failover that needs a lease When a logical or partial order is acceptable and the extra hop is not
Copies only where a member lives and may hold them 30,000 copies a second instead of 400,000, and residency rules that can be kept A policy to run for every message, and rules to test When every region must be able to serve every user, as for a global directory with no residency limit

An interviewer asks you to take the chat service multi-region in 45 minutes. A good answer begins with why (which of latency, availability and residency applies), keeps the traffic local, keeps the sender off the cross-region path, gets one order per conversation from one writer, and treats residency as a rule on copies. Write your answer before you read the strong one.

Interview question

Writing and reviewing an answer needs JavaScript.

The interviewer asks

Your chat service runs in one region and now has users on three continents, some of whom are legally required to keep their messages in their own region. How do you take it multi-region, and how do you keep messages in order across regions?

Assume 40% of the users in North America, 30% in Europe and 30% in Asia-Pacific, a peak of 200,000 messages a second, and that 15% of the messages go to someone homed in another region. Round trips are about 20 ms nearby and 80 to 160 ms between continents.

Write your answer first. The strong answer unlocks once you have written a real attempt.

0 of 80 characters written to unlock the strong answer

  • 200 million users at 30 messages make 6 billion messages a day: about 69,000 a second on average and about 200,000 at the peak. 15% cross regions: 30,000 a second, 48 Mbit/s. Bandwidth is not the problem; latency, order and copies are.
  • One stack takes about 107,000 messages per second and is at 141% at 150,000. Three stacks take 320,000 and put every user 20 ms from a region, against 80 ms on average to a single one.
  • A synchronous call to another region holds a connection for about 100 ms: the cross-region calls are at 38% at 60,000 messages per second and at 113% at 180,000, with the slowest 5% of messages at 145 ms and then about 7.5 s.
  • A relay log takes the sender off the path (28 ms for the slowest 5%) and gives replication lag instead of errors: 105% at 210,000 messages per second with no failed message.
  • A home region numbers each conversation: one order, at the price of a round trip for members elsewhere (145 ms again). Split in 8, the relay takes the 420,000 appends at 18%.
  • Copying only where a member lives and may hold the message is 30,000 copies a second instead of 400,000. Losing a region at the peak leaves two stacks at 94%, and a cut link of ten minutes leaves 18 million messages to drain in about 43 s.

Next: Search engine, where crawling, an inverted index and ranking meet.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.