0% completed
Introduction
On This Page
The Module's Question
Synchronous and Asynchronous
Jobs and Facts
Push and Pull
At-Least-Once Delivery
The Store This Module Follows
Three Questions for a System You Know
A system made of several services works only if those services can send data to each other. One service writes a message, the network carries it, and another service reads it and acts on it. The message may ask for work, like "charge this card $40." It may also report something that already happened, like "order 1042 was placed."
Two services can exchange a message in several ways. The sender may wait for an answer, or it may continue at once. One program may receive the message, or many may. The receiver may ask for news, or the sender may send news as soon as there is some. A fixed way of making these choices is called a communication pattern, and this module teaches how to choose one for each connection between services.
The module covers seven communication patterns, the one question that helps you choose among them, and four ideas that every pattern uses.
The Module's Question
Draw any design as boxes and arrows, and each arrow is a connection that needs a pattern. One question decides most of them: does the caller need the answer before it can continue?
If the answer is yes, use request-response. The caller sends a request and waits for one response. A buyer who taps Place order cannot see "Order confirmed" until the store has confirmed the order, so that connection waits.
If the answer is no, the caller does not need to wait. A receipt email can arrive a minute later, and the buyer does not need it before the page changes. That connection belongs to one of the other six patterns.
Request-response is the default. It is the simplest pattern to build, test, and debug, and it fits almost everything a person waits for on a screen. Choose another pattern only when the answer to the question is no, and state the reason.
Read the map from the top left. A yes goes straight to request-response, numbered 1. A no leads to a second question about the receiver, and each of its six rows ends at one pattern. The numbers 1 to 7 are also the order of the lessons in this module.
Synchronous and Asynchronous
When the caller waits for the answer, the communication is synchronous. The caller's next step depends on the answer, so the caller does nothing else on that request until the answer arrives. When the caller sends the message and continues without the answer, the communication is asynchronous. The work happens later, at a time the receiver chooses.
Waiting has a cost that grows with every call. When the store's checkout makes four calls one after another, the buyer waits for the sum of their times: 30 + 150 + 20 + 50 = 250 ms.
Each waiting request also holds one of checkout's 200 threads. Checkout receives 300 requests per second. Suppose the payment service slows from 150 ms to 2 seconds. Each request then takes about 2.1 seconds, so that traffic needs 300 × 2.1 = 630 threads. Checkout has only 200, so it stops accepting orders.
Asynchronous communication usually puts a broker between the two services. A broker is a server that stores messages after the sender writes them and delivers them to receivers. The sender waits only for the broker to confirm that the message is stored. During a flash sale (a short sale with large discounts), the store's API answers "Order received" in about 10 ms this way, and workers process the orders afterward.
Jobs and Facts
A message carries one of two kinds of content. A job is work that one program must do once, like "process order 1042." A fact is a record of something that already happened, like "order 1042 was placed." A fact is named in the past tense, and any number of programs may need it.
The difference decides the pattern. A job goes to a queue, and exactly one worker takes it. A fact goes to a topic, a named channel on the broker, and every service that subscribed to the topic receives its own copy. In the store, five teams need the fact that an order was placed: fulfillment, email, analytics, fraud, and loyalty.
Sending a fact to a queue as if it were a job loses work. If the fulfillment and email teams read from one queue, each order goes to only one of them. At 200 orders per second, about 100 orders per second would never ship, and the other 100 would ship without a receipt.
Push and Pull
A receiver learns about new data in one of two ways. In pull, the receiver asks the sender "anything new?" again and again on a timer. This repeated asking is called polling, and each poll is an ordinary request-response call. In push, the sender sends a message when the news exists, and the receiver waits for it.
Pull is simple, but its cost grows with the number of receivers, not with the amount of news. The store's tracking page once polled every 3 seconds. In the busiest hour, 30,000 open pages sent 30,000 ÷ 3 = 10,000 requests per second, and the store had only about 17 status changes per second to report.
With push, the cost grows with the amount of news instead. It fits when news arrives at times nobody can predict. Three patterns in this module push: another company pushes to the store, the store pushes to a browser, and both sides of a chat push to each other.
At-Least-Once Delivery
Networks lose messages, and programs crash in the middle of their work. To avoid losing a message, a sender that receives no confirmation sends the message again. However, the sender cannot tell a lost message from a lost confirmation. As a result, anything that retries can deliver a message twice.
A delivery promise of "every message arrives one or more times, and none is lost" is called at-least-once delivery. Most brokers make this promise, and so do the companies that send webhooks (messages that one company's server sends to another company's server). This promise means that every receiver must be idempotent: processing the same message twice must have the same effect as processing it once.
Follow the four steps. The broker sends event evt_5521 for a $40 order, and the loyalty service adds 40 points, from 120 to 160. Loyalty's "done" message is lost, so the broker sends the same event again. Loyalty finds evt_5521 in its list of processed events and skips it, so the balance stays at 160. Without that check, the buyer would have 200 points for one order.
This rule appears again later in the module, for the queue worker, every subscriber, and the webhook receiver. The Idempotency lesson shows how to build these checks.
The Store This Module Follows
The lessons follow one online store as its communication needs grow. The first four lessons follow one store in order, and each lesson after the first starts with a problem that the previous lesson left unsolved. The fifth shows the store's link to its payment provider, and the last two show the same store from the buyer's screen.
- Request-Response: a buyer taps Place order, and checkout makes four calls in a chain that takes 250 ms.
- Message Queue: a flash sale sends 5,000 orders per second to a pipeline that handles 400, and a durable queue (one that keeps messages on disk) stores the extra orders.
- Publish-Subscribe: five teams need every order, so checkout publishes
order.placedonce instead of calling each team. - Event-Driven Architecture: the event carries only an ID, so every team calls checkout back for the details. The fix is to put the order's details in the event.
- Webhooks: the external payment provider tells the store when a payment succeeds, fails, or is refunded.
- Server-Sent Events: the order-tracking page stops polling and receives updates over one HTTP response that stays open.
- Bidirectional Streaming: the support chat needs both sides to send at any moment.
The module ends with Mini-Capstone: Designing a Notification System. It builds one system that uses all seven patterns and uses the module's question to choose the pattern for each arrow.
Three Questions for a System You Know
Pick a system you have worked on, and ask these three questions about its connections.
1. Which callers wait for an answer they do not use? Find one synchronous call and ask whether the caller needs the answer before it can continue. If it does not, the call adds waiting and risk for no benefit.
2. Is each message a job or a fact? Count the services that need it. If one service does the work, a queue fits. If several services need to know, or one of them reads another service's tables directly, the message is a fact.
3. What happens if a message arrives twice? Choose one message that changes data, like a charge or a points update. Find the ID that the receiver checks before it acts. If there is no such ID, a single retry can repeat the effect.
Reading Progress
0%
On This Page
The Module's Question
Synchronous and Asynchronous
Jobs and Facts
Push and Pull
At-Least-Once Delivery
The Store This Module Follows
Three Questions for a System You Know