0% completed
Request-Response
On This Page
What Request-Response Means
The Four Outcomes of a Call
Wait Times Add Up Along a Chain
Waiting Holds Resources
A Checklist for Every Call
When to Use Request-Response
Check Yourself
Summary
A buyer taps Place order on the store's checkout page. The phone sends one message to the store's servers and shows a spinner. A quarter of a second later, a second message comes back with the words "Order confirmed," and the spinner disappears. During that quarter second, the phone did nothing except wait for the answer.
That exchange is the most common way for two programs to communicate. One program asks, the other answers, and the program that asked waits for the answer before it continues. This lesson explains how the pattern works, what the waiting costs, and how to decide whether a connection in a design should use it.
What Request-Response Means
In request-response, one program sends a request and waits until another program sends back a response. The program that asks is the caller, also called the client, and the program that answers is the server. Each request receives exactly one response, and the caller can match that response to its request. The name lists the two messages in the order they travel.
Most traffic on the web follows this pattern. A browser loading a page uses it, and so does a mobile app calling a REST API. One service calling another through gRPC (a framework for calls between services) uses it too, as does an application sending a SQL query to its database. Each of them sends one request and waits for one answer. The message formats differ, but the exchange is the same.
The defining property is that the caller waits. This is called synchronous communication: the caller's next step depends on the answer, so the caller cannot continue until the answer arrives. The checkout page cannot show "Order confirmed" before the store has confirmed the order. That dependency is the reason to choose request-response, and it is also the source of its costs.
To handle the buyer's single request, the checkout service sends requests of its own. It asks the inventory service to reserve the item, asks the payment service to charge the card, writes the order to the orders database, and asks the fulfillment service to schedule shipping. Each of those four connections is also request-response, so one tap on the phone produces five calls.
The Four Outcomes of a Call
A caller that sends a request receives one of four outcomes, and each outcome needs different handling. HTTP, the protocol that most web services use, reports the first three with a numeric status code.
The diagram shows checkout asking the payment service to charge $40, with one row for each outcome:
- Success, a 2xx code like
200 OK. The card was charged, and checkout continues to the next step. - Client error, a 4xx code like
400 Bad Request. The request itself was wrong, for example because the card number was missing. Sending the same request again usually produces the same error, so the caller should not retry it. The common exception is429 Too Many Requests, which asks the caller to slow down and try again later. - Server error, a 5xx code like
503 Service Unavailable. The server could not handle the request this time, perhaps because it was overloaded or restarting. A later attempt may succeed. - No answer. The caller's time limit ran out, or the connection broke before a response arrived.
The fourth outcome causes the most damage, because the caller does not know what happened. The payment service may never have received the request. It may also have charged the card and then failed to send the response. From the caller's side, those two cases look exactly the same.
That uncertainty decides how retries must work. If checkout retries a charge that already succeeded, the buyer pays twice. The safe design attaches a unique ID to each charge, called an idempotency key, and the payment service ignores a second request that carries a key it has already processed. Doing the operation twice then gives the same result as doing it once, a property called idempotency, which has its own lesson.
Wait Times Add Up Along a Chain
When a caller makes several calls one after another, the person at the start of the chain waits for the sum of all of them.
The timeline shows one checkout request on a normal day. Inventory takes 30 ms, payment takes 150 ms, the database write takes 20 ms, and fulfillment takes 50 ms. The buyer's spinner stays on the screen for the whole 250 ms, and the payment call alone is 60 percent of that time.
Two other numbers get worse with every call added to a chain. An average response time does not show either of them.
Slow responses. Suppose each of the four services answers within its normal time on 99 calls out of 100 and is slow on the remaining one. A checkout avoids every slow call only when all four calls are fast. That happens 0.99 × 0.99 × 0.99 × 0.99 of the time, or about 96 percent. About 4 percent of buyers therefore wait longer than normal, which is four times the rate of any single service. A page that waits for 100 such calls meets at least one slow call about 63 percent of the time.
Availability. Availability is the share of time a service answers correctly. Suppose each of the four services is available 99.9 percent of the time, and their failures are independent. Checkout works only when all four work, and 0.999 to the fourth power is about 99.6 percent. Each service alone is down about 43 minutes in a 30-day month. Checkout, which needs all four, is down about 173 minutes, nearly three hours.
Two design changes shorten a chain. Calls that do not depend on each other can run at the same time, so the caller waits for the longest call instead of the sum. Calls whose result the caller does not need can be removed from the chain, which is the subject of the next lesson.
Waiting Holds Resources
A waiting request is not free for the server that sends it. While checkout waits for the payment service, the request occupies a thread, memory, and often a database connection on the checkout server. A thread is one worker inside a program that runs its own sequence of steps. In this design, each thread handles one request at a time, so a checkout server with 200 threads can work on at most 200 requests at once.
Little's law, from the Back-of-the-Envelope Math lesson, gives the number of requests in progress. Requests in progress equal the arrival rate multiplied by the time each request takes.
The top half of the diagram shows a normal day. Checkout receives 300 requests per second, and each one takes 0.25 seconds, so 300 × 0.25 = 75 threads are busy and 125 are free for sudden extra traffic.
The bottom half shows what happens when the payment service slows from 150 ms to 2 seconds per call. Each checkout request now takes about 2.1 seconds, so the same traffic needs 300 × 2.1 = 630 threads. Only 200 exist, and new requests fill all of them in about two-thirds of a second (200 ÷ 300). After that, every new buyer waits in a line or receives an error. The checkout code has no bug, because it is doing exactly what it was written to do: wait for an answer.
This is the main cost of request-response: the caller fails when a service it calls is slow or down. A slow server is often more dangerous than a stopped one. A server that refuses requests returns an error at once and releases the caller's thread, while a slow server keeps the thread for as long as the caller is willing to wait.
A Checklist for Every Call
Every request-response call has this risk, so each call needs four decisions before it goes into production:
- A time limit. Decide how long the caller waits before it gives up. Without a timeout, one slow server can occupy every thread, as the diagram above shows.
- A retry rule. Retry server errors and missing answers, but not client errors other than 429. Wait longer before each new attempt, as retry with exponential backoff describes, so retries do not overload a server that is already overloaded.
- Duplicate protection. Any request that changes data, like a charge or an order, carries an idempotency key, so a retry after a missing answer cannot repeat the effect.
- A plan for failure. Decide what the user sees when the call fails. When a server keeps failing, a circuit breaker stops calling it for a while and returns an error immediately.
When to Use Request-Response
Ask the same question about every connection in a design: does the caller need the answer before it can continue? The rest of this module asks it of every pattern. If the answer is yes, use request-response. If the answer is no, the caller is waiting for nothing, and another pattern in this module fits better.
The table applies the question to each connection in the store's checkout:
| Connection | Does the caller need the answer to continue? | Pattern |
|---|---|---|
| Phone to checkout: place the order | Yes, the buyer needs to know whether the order succeeded | Request-response |
| Checkout to inventory: reserve the item | Yes, an order for an item that is out of stock must stop | Request-response |
| Checkout to payment: charge the card | Yes, the order cannot be confirmed without payment | Request-response |
| Checkout to orders database: save the order | Yes, a confirmed order must be stored first | Request-response |
| Checkout to fulfillment: schedule shipping | No, the buyer does not wait to see a shipping plan | Not request-response |
| Checkout to email: send a receipt | No, a receipt can arrive a minute later | Not request-response |
Four of the six connections need the answer, which is typical. Request-response is the right default for anything a person is looking at, like a product page, a search, a login, or a coupon check. The fulfillment row shows the problem in the current design. Every buyer waits 50 ms for work they never see. When traffic suddenly grows, the whole chain of waiting calls stops working, as the next lesson, Message Queue, shows.
Request-response fits poorly in three other cases:
- Work that takes longer than a person will wait, like building a large report. A short request-response call accepts the job and returns
202 Acceptedwith a job ID, and the client checks the job's status later. - Updates that the server should send as they happen, like delivery tracking. Asking the server "anything new?" every few seconds is called polling. It is request-response repeated many times, and most of the answers say "no". Later lessons in this module let the server send updates instead.
- One fact that many services need, like "an order was placed". If one caller sends this fact to five services, it must wait for all five and handle a failure in each one.
Check Yourself
Q1. A product page calls five services one after another: product details (40 ms), price (30 ms), reviews (80 ms), recommendations (50 ms), and stock (20 ms). None of the calls depends on another. How long does the buyer wait now, and how long could they wait?
<details> <summary>Show answer</summary>The calls run one after another, so the buyer waits for the sum: 40 + 30 + 80 + 50 + 20 = 220 ms. The calls are independent, so the page can send all five at the same time and wait only for the slowest one, the 80 ms reviews call. Recommendations are also optional for this page, so a short time limit on that call protects the page if the service slows down.
</details>Q2. Checkout calls the payment service with a time limit of 1 second, and no response arrives. Should checkout retry the charge?
<details> <summary>Show answer</summary>Only if the charge carries an idempotency key. A missing response does not tell checkout whether the card was charged. The payment service may have charged the card and then failed to answer. With a key, the payment service recognizes the retry and returns the original result instead of charging again. Without a key, a retry can charge the buyer twice, so the safer choice is to check the payment's status before trying again.
</details>Q3 (incident). The checkout page shows a "You might also like" box, filled by a call to the recommendations service. One afternoon, that service starts taking 10 seconds per call, and checkout stops accepting orders, although payment and inventory are healthy. Checkout has 200 threads and receives 300 requests per second. What happened, and what should change?
<details> <summary>Show answer</summary>Each checkout request now holds a thread for about 10 seconds, so the traffic needs 300 × 10 = 3,000 threads. The 200 threads fill in under a second, and every later buyer waits or fails. The buyer does not need recommendations to place an order, so this call fails the module's question: checkout can continue without the answer. The call needs a short time limit, around a few hundred milliseconds, and the page should render without the box when the call fails or times out.
</details>💡 In the interview: Draw the arrows first, then ask the module's question aloud for each one. A strong answer sounds like this: "Placing the order is request-response, because the buyer needs a yes or a no. Inventory and payment are also synchronous, because the order cannot be confirmed without them. Fulfillment and the receipt email do not need to finish before we answer the buyer, so I take them out of the chain the buyer waits for. Every synchronous call gets a timeout, retries only on server errors and missing answers, and an idempotency key on anything that changes data." Expect a follow-up question about a slow service that checkout calls. Answer it with the thread calculation: requests per second multiplied by seconds per request gives the threads in use.
Summary
| <span style="white-space:nowrap">Problem</span> | A buyer taps Place order, and the checkout page cannot show "Order confirmed" until the store has confirmed the order. To handle it, checkout calls inventory, payment, the orders database, and fulfillment, so one tap produces five calls. |
| <span style="white-space:nowrap">Mechanism</span> | The caller sends one request and waits for exactly one response from the server, which is synchronous communication. A call ends in success (2xx), client error (4xx), server error (5xx), or no answer. With no answer, the caller cannot tell whether the work happened, so a retry needs an idempotency key. |
| <span style="white-space:nowrap">Costs</span> | Wait times add up along a chain, so four calls cost the buyer 250 ms. Four services at 99.9 percent availability give checkout about 99.6 percent. Waiting holds a thread, so when payment slows to 2 seconds, 300 requests per second need 630 threads, and the 200 available threads fill in about two-thirds of a second. |
| <span style="white-space:nowrap">When to skip</span> | Skip it when the caller does not need the answer to continue, like scheduling shipping or sending a receipt (use a message queue). Work longer than a person will wait returns 202 Accepted with a job ID. Updates the server should send as they happen should not use polling. |
| <span style="white-space:nowrap">30-second answer</span> | Placing the order, inventory, and payment are request-response, because the buyer needs a yes or a no. Fulfillment and the receipt email leave the chain the buyer waits for. Every synchronous call gets a timeout, retries only on server errors and missing answers, and an idempotency key on anything that changes data. |
Flashcards Review
What is request-response?
Reading Progress
0%
On This Page
What Request-Response Means
The Four Outcomes of a Call
Wait Times Add Up Along a Chain
Waiting Holds Resources
A Checklist for Every Call
When to Use Request-Response
Check Yourself
Summary