A declined card
Make the bank slow, watch the first payment time out, and see the retry turn it into a paid order.
At 21:03 the real order #4711 was declined: the bank's fraud check timed out during its nightly batch. Here you make the fake bank just as slow.
| Time | 15 minutes |
| You need | The lab running, a fresh order (run Reset the lab first if #4711 is already paid) |
1Make the bank slow#
curl -X POST localhost:8081/admin/latency -d '{"ms": 25000}'
The fake bank now takes 25 seconds to answer, longer than the 15 seconds the payment service waits.
2Read the retry#
This is the part of the payment service that saves the order, in the three languages the lab ships. Pick yours; the choice sticks on every page.
var key = order.id();
var result = bank.authorize(card, amount, key);
if (result.isTimeout()) {
Thread.sleep(Duration.ofSeconds(30));
result = bank.authorize(card, amount, key);
}
return result;
- The order id is the idempotency key, the same on both calls.
- One retry only, thirty seconds later.
- A second timeout is returned as it is: the customer sees "still checking".
key = order.id
result = bank.authorize(card, amount, key)
if result.is_timeout:
time.sleep(30)
result = bank.authorize(card, amount, key)
return result
- The order id is the idempotency key, the same on both calls.
- One retry only, thirty seconds later.
- A second timeout is returned as it is: the customer sees "still checking".
key := o.ID
res, err := bank.Authorize(ctx, card, amount, key)
if errors.Is(err, ErrTimeout) {
time.Sleep(30 * time.Second)
res, err = bank.Authorize(ctx, card, amount, key)
}
return res, err
- The order id is the idempotency key, the same on both calls.
- One retry only, thirty seconds later.
- A second timeout is returned as it is: the customer sees "still checking".
The same key goes with both calls, so the bank cannot charge the card twice.
3Pay and wait#
time curl -X POST localhost:8082/orders/4711/pay
{"order": 4711, "status": "paid", "attempts": 2, "elapsed_ms": 30412}
real 0m30.4s
The tracking page showed Still checking your payment for thirty seconds, then turned the payment tile green.
This is how the talk tells the same moment:
Warning
Set the bank back to normal before the next page:
curl -X POST localhost:8081/admin/latency -d '{"ms": 200}'.