Shipping a Placed Order
In the previous chapter, we handled Mugshop’s flash sale with two queues. The commerce service accepts each order as PROCESSING and puts it on the order queue. The order worker takes the next order, runs the transaction, takes the payment, and sets the order to PLACED. Then it puts a message on the email queue, and the notification service sends the confirmation email.
For each order, the order worker works with three services:
- the payment service takes the payment,
- the notification service sends the confirmation email,
- the shipping service ships the order.
The email already goes through a queue. Recall that when we split Shopend into services, the commerce service called the payment service and the shipping service with RPC. Should the order worker also use a queue for the payment service, and for the shipping service?
Recall the rule for when to use a queue. Work should go through a queue when the caller does not need the result in its response, and the work comes in bursts larger than the system can handle at once, takes too long to do inside a request, or goes to a service that may be slow or down.
The payment service
The order worker needs the result of the payment. The order becomes PLACED if the payment goes through and FAILED if it is declined. So the order worker keeps calling the payment service with RPC.
The shipping service
The shipping service passes both parts of the rule.
First, the order worker does not need the result. It sets the order to PLACED before the order is shipped, and nothing it does next depends on the shipping service.
Second, the work goes to a service that may be slow or down. If the shipping service is slow, the order worker waits on each call, and the orders behind order 93 wait longer in the order queue. If the shipping service is down, the call fails. Either the shipment is lost, or the order worker keeps trying while the orders behind it wait.
We used the same reasoning for the email queue. So the order worker puts a second message on a shipping queue, and the shipping service takes the messages from it.
Three queues
The broker now holds three queues:
graph TD
H[Commerce service: handles requests] --> OQ
subgraph B[Broker]
OQ[Order queue]
EQ[Email queue]
SQ[Shipping queue]
end
OQ --> W[Commerce service: order worker]
W --> EQ
W --> SQ
EQ --> N[Notification service]
SQ --> Sh[Shipping service]
The order worker is the consumer of the order queue and the producer for the other two queues.