Tracing one quickbite order across services
Lesson 1 of 81 in Microservices Interview Mastery. Free to read, no account needed.
Following one order
Tracing means picking one order and following its orderId from service to service. At each step you note which service acts and what state the order is in. QuickBite is a food delivery app, and each of its small services has one job, so one order passes through several of them.
The thread for this lesson is order 42: customerId 7 is ordering from restaurantId 3, and the total is ₹450.
Order 42, hop by hop
Customer 7's order request arrives at api-gateway, the single front door for requests, which forwards it to order-service. From there, order 42 moves through the system in this order:
- order-service creates order 42 and marks it PENDING.
- payment-service charges ₹450 to the customer's UPI account for order 42.
- restaurant-service accepts order 42, so its state moves from PENDING to CONFIRMED.
- delivery-service assigns a rider to the confirmed order 42.
Order 42 does not run inside one database transaction. Each service owns its own data, so the four hops run as a saga: a sequence of local steps, and each service commits its own step. If a later step fails, the earlier steps are undone by compensating actions. For example, if restaurant-service rejects order 42 after payment-service has charged ₹450, the saga refunds the charge and cancels the order, because no database rollback can reach the other services.
Hop-by-hop table
Each row of this table is one hop in the trace. Read down the Order 42 state column and it changes only twice: when order-service creates the order, and when restaurant-service accepts it. The other hops act on an order that is already in progress.
| Hop | Service | Action | Order 42 state |
|---|---|---|---|
| 1 | api-gateway | Receives the request and forwards it | Not created yet |
| 2 | order-service | Creates order 42 | PENDING |
| 3 | payment-service | Charges ₹450 via UPI | PENDING |
| 4 | restaurant-service | Accepts the order | CONFIRMED |
| 5 | delivery-service | Assigns a rider | CONFIRMED |
Predict where order 42 is
Suppose a trace of order 42 shows that payment-service has just charged ₹450 via UPI, and nothing further has been recorded. Which service acted last, and what is the state of order 42?
The last service to act is payment-service, and order 42 is still PENDING. The charge went through, but restaurant-service has not accepted the order yet, so the state has not moved to CONFIRMED. No rider has been assigned either.
Retries and the idempotency key
A network glitch can make the gateway send the same payment request twice. The client sends an Idempotency-Key header holding a unique random string, such as a UUID made once per payment attempt. payment-service saves the result of the first charge against that key, so a retry with the same key returns the saved result and ₹450 is charged only once.
Choose the key with care. Use a random string with enough entropy, never personal data such as a phone number, and keep the same key for every retry. A new key on each retry makes the server treat the retry as a new payment.
Practice questions
- 1.
In a trace of QuickBite order 42, order-service creates the order as PENDING, then itself charges ₹450 via UPI. What is the bug?
- A.
Order 42 should be CONFIRMED before any charge is made.
- B.
order-service should not charge; payment-service owns the UPI charge.
- C.
api-gateway should send the charge straight to restaurant-service.
- D.
order-service should create order 42 only after payment.
Show answer
Answer: B.order-service should not charge; payment-service owns the UPI charge.
The correct option identifies the bug: charging the customer for an order belongs to payment-service, which charges ₹450 via UPI, while order-service only creates the order and records its PENDING state. The option proposing that api-gateway forward the charge is wrong because the gateway only receives the request and forwards it to order-service, and it never reaches restaurant-service for payment. The option claiming order 42 should be CONFIRMED before charging is wrong because CONFIRMED comes only after restaurant-service accepts the order, which happens after payment. The option saying order-service should create the order only after payment is wrong because the order is created first as PENDING, before payment-service acts.
- A.
- 2.
On the QuickBite happy path, delivery-service assigns a rider to order 42. Which state must order 42 be in at the moment that assignment happens?
- A.
CONFIRMED, after restaurant-service has accepted it
- B.
PENDING, because rider assignment does not depend on acceptance
- C.
PENDING, with the UPI charge already recorded
- D.
Not created yet, because the rider is chosen from the request
Show answer
Answer: A.CONFIRMED, after restaurant-service has accepted it
Option 1 is correct: rider assignment is the last happy-path hop and runs only after restaurant-service moves order 42 from PENDING to CONFIRMED. Option 3 is wrong because a successful payment does not change the state, so PENDING with a charge is still too early for a rider. Option 4 is wrong because the order is created by order-service before any rider step, and the request alone does not create it. Option 2 is wrong because the happy path makes rider assignment depend on restaurant acceptance.
- A.
- 3.
A learner thinks api-gateway saves order 42 before forwarding it. What does api-gateway actually do?
- A.
Forwards the request to order-service, with no order record saved.
- B.
Holds order 42 until the restaurant accepts it.
- C.
Charges ₹450 via UPI, then forwards the request.
- D.
Saves order 42 as PENDING, then forwards the request.
Show answer
Answer: A.Forwards the request to order-service, with no order record saved.
api-gateway is the single front door: it receives the customer's request and forwards it to order-service, and its trace row shows the order as not created yet. Saving the order as PENDING is order-service's step, so the option describing a PENDING save at the gateway is wrong. Holding the order until restaurant acceptance is wrong because restaurant-service is the hop that accepts the order. Charging ₹450 via UPI is wrong because payment-service owns the charge.
- A.
- 4.
Restaurant-service rejects order 42 after payment-service has already charged ₹450 via UPI. Which sequence correctly undoes the earlier steps?
- A.
order-service marks order 42 as cancelled, and payment-service refunds ₹450 for orderId 42
- B.
delivery-service cancels the rider, and the bank deletes the charge
- C.
api-gateway deletes order 42 from its request log
- D.
restaurant-service keeps retrying its rejection until the charge disappears
Show answer
Answer: A.order-service marks order 42 as cancelled, and payment-service refunds ₹450 for orderId 42
A saga undoes finished steps with compensating actions rather than a single rollback. Order-service records the cancellation and payment-service refunds the charge. The delivery option is wrong because no rider is assigned after a rejection, and banks reverse charges through refunds rather than deleting them. Deleting a log entry or retrying a rejection does not reverse the payment.
- A.
- 5.
Why does the QuickBite design use a saga across order, payment, restaurant and delivery services instead of one database transaction?
- A.
Each service owns its own database, so no single transaction can span all four
- B.
Sagas require every service to share one orders table
- C.
Sagas remove the need for a state field on the order
- D.
HTTP forbids any transaction that touches more than one service
Show answer
Answer: A.Each service owns its own database, so no single transaction can span all four
Each service keeps its own data, so one database transaction cannot cover order creation, the UPI charge, restaurant acceptance and rider assignment together. A saga instead runs local steps in sequence and uses compensating actions when a later step fails. The shared-table option contradicts the design, and HTTP does not forbid multi-service work; it just does not provide a transaction.
- A.
- 6.
For the payment request for order 42, which way of choosing the idempotency key is correct?
- A.
The customer's phone number, so the key is the same every time.
- B.
A new random key on every retry, so each attempt is unique.
- C.
A random UUID made once per payment attempt and sent again on each retry.
- D.
The order total, ₹450, because the same total always means the same order.
Show answer
Answer: C.A random UUID made once per payment attempt and sent again on each retry.
Option 3 is right: the key is random, so it is unique, and it stays the same across retries, so payment-service can recognise the repeat and return the saved result. A phone number is personal data and should not be used as a key. A new key on each retry defeats the purpose, because the server sees a new payment. The total ₹450 is shared by any other order with the same amount, so retries of two orders could collide.
- A.
- 7.
Why must delivery-service wait until order 42 is CONFIRMED before it assigns a rider?
- A.
A rider should not be sent for an order the restaurant has not accepted, because the order may still be rejected
- B.
Delivery needs the UPI transaction ID before it can find a rider
- C.
Restaurant-service sends its menu only after a rider is assigned
- D.
Riders can be assigned only to orders that are still PENDING
Show answer
Answer: A.A rider should not be sent for an order the restaurant has not accepted, because the order may still be rejected
The restaurant's acceptance is the point where the order is firm, so sending a rider earlier risks a wasted trip if the order is rejected. The UPI transaction ID is not needed to find a rider, and the menu is not tied to rider assignment. The last option reverses the happy path, where assignment comes after CONFIRMED.
- A.
- 8.
Four records from a trace of one QuickBite order arrived out of order. Which record's hop must happen first on the happy path?
Service Action Order state recorded delivery-service Assigns a rider CONFIRMED payment-service Charges ₹450 via UPI PENDING restaurant-service Accepts the order CONFIRMED order-service Creates the order PENDING - A.
order-service creates order 42 as PENDING
- B.
payment-service charges order 42 via UPI
- C.
restaurant-service accepts order 42 as CONFIRMED
- D.
delivery-service assigns a rider to order 42
Show answer
Answer: A.order-service creates order 42 as PENDING
The order must exist before anything can be charged or acted on, so order-service's creation of order 42 as PENDING comes first. The UPI charge by payment-service comes after creation, even though its recorded state is also PENDING. Restaurant acceptance moves the order to CONFIRMED only after payment, and rider assignment is the final happy-path hop after confirmation.
- A.
- 9.
Why does api-gateway send order 42's request to order-service before payment-service?
- A.
Delivery needs the request before assigning a rider
- B.
Payment can only charge an order that already exists
- C.
Order-service holds the restaurant's acceptance of order 42
- D.
Payment-service must run before any order record exists
Show answer
Answer: B.Payment can only charge an order that already exists
Option 2 is correct: payment-service charges Rs 450 for order 42, so order 42 must already exist, and order-service creates it first. Option 4 reverses the order: no charge can precede the order record. Option 3 is wrong because restaurant-service, not order-service, accepts the order. Option 1 is wrong because delivery-service acts only after restaurant acceptance, so it is not the next hop.
- A.
- 10.
Customer 7 placed orders 42 and 43, both from restaurant 3. Which rows belong to a hop-by-hop trace of order 42?
Service orderId customerId restaurantId order-service 42 7 3 payment-service 43 7 3 payment-service 42 7 3 restaurant-service 43 7 3 - A.
The order-service and payment-service rows, which carry orderId 42
- B.
All four rows, since every row shares customerId 7 and restaurantId 3
- C.
Only the order-service row, because it is the one that creates order 42
- D.
Only the payment-service and restaurant-service rows, since they charge and accept
Show answer
Answer: A.The order-service and payment-service rows, which carry orderId 42
A trace follows one orderId, so only rows with orderId 42 belong to it: the order-service row and the payment-service row. Option 2 is wrong because customerId and restaurantId are shared by both orders 42 and 43, so they cannot separate them. Option 3 drops the payment-service row, which also records order 42. Option 4 keeps the payment row but wrongly includes restaurant-service, whose only row is for order 43, and it drops the order-service row for order 42.
- A.
Lessons in How a Microservices System Fits Together
- Tracing one quickbite order across services
- Monolith vs microservices trade-offs
- Bounded context per service
- Synchronous rest vs asynchronous events
- Api gateway as the single entry point
81 lessons across 9 topics. See the full course