Skip to content

The cancellation that arrived before the booking

Failure story 03, in requests, retries and side effects.

The cancellation reached the booking service first and found no booking. The service replied Canceled. Then the booking command arrived. Is the booking active?

Assumption Value
Channel Commands reach the booking service through a queue with several consumers, so messages can be processed out of order
Booking command CreateBooking(intent bi-90)
Cancellation CancelBooking(intent bi-90); if no booking exists, the service treats the cancellation as complete
Customer promise Once Canceled is shown, the customer won’t be charged
Step What happens
1 The customer books. CreateBooking(bi-90) is queued, but its consumer pauses for 40 seconds during a garbage-collection pause.
2 The customer changes their mind and cancels. CancelBooking(bi-90) is picked up by a second consumer immediately.
3 The service finds no booking for bi-90, concludes there’s nothing to cancel, and replies Canceled.
4 The customer sees Your booking was canceled.
5 The first consumer resumes and processes CreateBooking(bi-90). Nothing forbids it.
6 Booking bi-90 is active and the customer is charged.

Not found was read as canceled. Those are different states: one means “this hasn’t happened yet”, the other means “this must never happen.” When messages can arrive in any order, a cancellation has to leave something behind for the booking to trip over. Without it, the system forgets the customer’s decision as soon as it has acknowledged it.

Aspect Design A: missing means done Design B: record the canceled intent
Cancel with no booking Replies Canceled, stores nothing Stores intent bi-90 with state canceled, then replies Canceled
Late CreateBooking(bi-90) Creates an active booking Finds the intent canceled; records the attempt and creates nothing
Cancel after booking Cancels the booking Cancels the booking
What’s kept Bookings only One row per intent, including intents that never became bookings

Design B treats the intent as a small state machine, with the transition into canceled allowed from any state and the transitions out of it forbidden:

create cancel
(none) ─────────► active ─────────────► canceled
│ ▲
└──────────────── cancel ───────────────┘
canceled + create → stays canceled (logged, no charge)

A database constraint can enforce it: insert the intent row on whichever command arrives first, and make CreateBooking an update that only succeeds WHERE state IS NULL OR state = 'pending'.

  1. Out of order. Deliver CancelBooking(bi-1), then CreateBooking(bi-1). Expected: no active booking, no charge, and an entry recording that a create arrived after cancellation.
  2. In order. Deliver CreateBooking(bi-2), then CancelBooking(bi-2). Expected: the booking exists and is canceled.
  3. Duplicates. Deliver the cancel twice and the create twice, in any interleaving. Expected: the same final state as tests 1 and 2.

Run the checks with a test harness that controls delivery order. Out-of-order arrival almost never happens in a single-consumer test environment, which is why this failure survives testing.

This check is in the checks catalog as test templates in Java and Python.

  • Commands for one intent are guaranteed to be processed in order, for example by a partitioned log keyed by intent ID with one consumer per partition, and no sender retries a create after sending a cancel.
  • The customer is told Cancellation requested, not Canceled, and a later process reconciles. That’s honest, but it’s a weaker promise.

Discussion question. How long must Design B keep a canceled intent with no booking? What determines the answer?

Exercise. Your intent records are deleted after 24 hours to save space. A mobile client was offline for two days, then delivered its queued CreateBooking for an intent the customer canceled from the website. What happens, and what retention rule would prevent it?

Worked answer

The canceled intent was deleted, so the late create finds nothing and creates an active booking: the original failure, two days later. The record must be kept at least as long as a create for that intent can still arrive. That’s set by the longest delay any sender can introduce: the client’s offline queue limit, the message broker’s retention and the longest retry schedule. If a client can’t bound its queue, the server needs another guard, such as rejecting creates whose intent was issued more than N hours ago.

Glossary: at-least-once delivery

Written and edited by Abhinav Taduka, enterprise software architect.