Exercise · 5 steps · about 10 minutes
Backend architecture: try it yourself¶
Run the ticket seller's backend from the video on your own computer, and take the email company down in the middle of a purchase. With everything in one request, the buyer sees an error, but the card is charged and no ticket goes out. With a queue and a worker, the buyer gets an answer right away, and the ticket goes out when the email company is back.
You need Git and Docker (start Docker Desktop first, or the Docker service on Linux), about 2 GB of free disk space and 1 GB of free memory for Docker. Nothing else: no account and no key. Run the commands in a terminal: Terminal on a Mac, your shell on Linux, and on Windows a WSL terminal, such as Ubuntu, with Docker Desktop's WSL integration turned on.
The card company and the email company are stand-ins: small programs of mine. Every card gets approved, and emails are only counted. No real money moves, and no real email goes out.
The email company from the video: what you see in steps 2 and 4, and in the extra
an error for the buyer, a charged card, no ticket
an answer right away, the ticket when the company is back
one order, the same ticket 8 times
the worker tries 8 times, one email goes out
1. Start the backend¶
Get the lab from my tutorials repository on GitHub, and start it:
git clone --depth 1 --branch main https://github.com/antonputra/tutorials.git
cd tutorials/lessons/286/lab
cp steps/10a-ticket-inside-the-purchase.env .env
docker compose up -d --build --wait
The .env file holds the switches of this step: the purchase does everything in one request. The first start builds the app's image and downloads PostgreSQL and HAProxy, so give it a few minutes. When the command returns, everything is ready: the load balancer, two copies of the app, the database with nine shows and their seats, and the two stand-ins. Every part is a container with one CPU.
2. The email company is down¶
The purchase has four steps, all in one request: hold the seat, charge the card, mark the order paid, then make the ticket and email it. Take the email company down for 30 seconds, and buy seat 114 for the club night:
tools/standin.sh mail POST /mode '{"mode": "down", "seconds": 30}'
tools/pay.sh 1 114 maya@example.com
POST /events/1/hold {"seat": 114}
HTTP 200 OK, in 0.01 s: {"held":114,"seat":"Row F, Seat 14","seconds":120}
POST /checkout {"email": "maya@example.com"}
HTTP 500 Internal Server Error, in 0.08 s: Internal Server Error
the card company's last line: charged $25.00 order=1 card=tok_demo charge=ch_000001
the email company's count: {"role":"mail","mode":"down","emails":0,"orders":0,"orders_with_more_than_one":0,"most_for_one_order":0}
the order in the database:
order | email | order_status | seat_status
-------+------------------+--------------+-------------
1 | maya@example.com | paid | sold
The buyer sees 500 Internal Server Error. But the card company charged $25.00 for order 1, the order is paid and the seat is sold. The email company sent nothing. For the buyer, the purchase failed. For us, it succeeded.
3. Add a queue and a worker¶
Now the purchase only writes a note on a queue, "send the ticket for order 2", and answers. A worker, the same app started another way, picks up the note and sends the email. The queue is RabbitMQ:
Compose starts the queue and the worker, and starts both copies of the app again with the new switch. The database keeps order 1.
4. The email company is down again¶
The same bad half minute, and the next seat:
tools/standin.sh mail POST /mode '{"mode": "down", "seconds": 30}'
tools/pay.sh 1 115 maya@example.com
docker compose exec -T queue rabbitmqctl -q list_queues name messages
POST /events/1/hold {"seat": 115}
HTTP 200 OK, in 0.01 s: {"held":115,"seat":"Row F, Seat 15","seconds":120}
POST /checkout {"email": "maya@example.com"}
HTTP 200 OK, in 0.05 s: {"order":2,"seat":"Row F, Seat 15","price":"$25.00","ticket":"on its way"}
the card company's last line: charged $25.00 order=2 card=tok_demo charge=ch_000001
the email company's count: {"role":"mail","mode":"down","emails":0,"orders":0,"orders_with_more_than_one":0,"most_for_one_order":0}
the order in the database:
order | email | order_status | seat_status
-------+------------------+--------------+-------------
2 | maya@example.com | paid | sold
name messages
tickets 1
sales 0
This time the buyer gets an answer right away: order 2, "ticket": "on its way". The note waits on the queue. The worker can't do the job, so it puts the note back and tries again a second later. Wait until the 30 seconds are over, then look again:
sleep 30
docker compose logs mail --no-log-prefix | tail -3
tools/standin.sh mail GET /stats
docker compose exec -T queue rabbitmqctl -q list_queues name messages
mail: down for 30 seconds
mail: back to normal
sent ticket order=2 to=maya@example.com copy=1
{"role":"mail","mode":"ok","emails":1,"orders":1,"orders_with_more_than_one":0,"most_for_one_order":1}
name messages
tickets 0
sales 0
The email company is back, the worker sent the ticket for order 2, and the queue is empty. docker compose logs worker shows each try: order 2: HTTPStatusError, back on the queue. The buyer never saw any of it.
Extra: slow is not down¶
The trap from the video. The email company is slow: it takes 3 seconds, and the worker waits only 2. The worker gives up, puts the note back and calls again, and every call still sends an email:
tools/standin.sh mail POST /reset
tools/standin.sh mail POST /mode '{"mode": "slow", "seconds": 20}'
tools/pay.sh 1 116 maya@example.com > /dev/null
sleep 25
tools/standin.sh mail GET /stats
{"role":"mail","mode":"ok","emails":8,"orders":1,"orders_with_more_than_one":1,"most_for_one_order":8}
One order, 8 emails: the buyer got the same ticket 8 times. Your count may be one more or less. So a job from a queue may be done twice, and doing it twice has to be safe. Give each email a key, the order number. The email company sends only one email per key:
cp steps/10c-a-key-on-each-email.env .env
docker compose up -d --build --wait
tools/standin.sh mail POST /reset
tools/standin.sh mail POST /mode '{"mode": "slow", "seconds": 20}'
tools/pay.sh 1 117 maya@example.com > /dev/null
sleep 25
tools/standin.sh mail GET /stats
{"role":"mail","mode":"ok","emails":1,"orders":1,"orders_with_more_than_one":0,"most_for_one_order":1}
The worker still tried 8 times. The email company sent one email.
5. Clean up¶
It removes the lab's containers, its volume and the two images it built. The PostgreSQL, HAProxy, RabbitMQ and Python images it downloaded stay in Docker's cache; docker compose down -v --rmi all removes them too. Then you can delete the tutorials folder: cd ../../../.. && rm -rf tutorials.
| The email company | The buyer sees | Ticket sent |
|---|---|---|
| Down, one request | an error | no |
| Down, a queue | the order | later |
| Slow, no key | the order | 8 times |
| Slow, a key | the order | once |
The card is charged in all four.
In the video, the same failure hit the arena sale for a whole minute: with everything in one request, 16,674 paid orders got 8,135 tickets, and 3,501 buyers paid more than once. With the queue and three workers, 18,000 orders got 18,000 tickets, and nobody paid twice.
Free. Unsubscribe anytime. Form not loading? Subscribe here.