
Two taps, one queue: how DrInCity handles retries and races
Idempotency keys, versioned writes and one transaction under a per-doctor lock. How a live queue survives double taps, lost responses and simultaneous clicks.
- An idempotency key makes a retried booking return the first answer, not make a second booking.
- A version on every write means a stale screen loses cleanly instead of overwriting.
- Many small writes with correct guards can still tear a queue. Use one transaction.
- Emergency insert and queue advance take the same lock, so they queue up and never collide.
- The pass condition for a race test is exactly one winner and positions one to N with no gaps.
DrInCity series · The engineer’s view
Quick answer
A booking has to survive a double tap, a lost response and two staff pressing the same button. DrInCity uses an idempotency key for retries, a version on every write for conflicts, and one transaction under a per-doctor lock for anything that renumbers a queue, then tests with concurrent races and not single clicks.
The short answer
A booking has to survive a double tap, a lost response and two staff pressing the same button. Use an idempotency key for retries, a version on every write for conflicts, and one transaction under a per-doctor lock for anything that renumbers a queue. Then test with races, not single clicks.
Three ways a queue breaks
| What happens | Without a guard | The guard |
|---|---|---|
| The same request arrives twice (flaky network, impatient thumb) | Two bookings | Idempotency key |
| Two people change one row together | The later write silently wins | Version on every write |
| A change touches many rows and fails halfway | A queue with duplicates and gaps | One transaction under a lock |
We wrote a generic version of this in how to stop double bookings. Here is the DrInCity version.
Retries

The phone sends a key with every booking. The function claims the key and takes the slot in the same database call. If the response is lost and the phone retries, the key is already marked done, so it returns the first answer. One booking, two requests, no drama.
Booking runs as a single round trip to the database: check the role, check the payment gate, check the doctor’s window, build the day’s slots once, take the slot, complete the key. Fewer round trips means fewer places to fail between steps.
Slots are 15 minutes with room for one booking each. If two patients grab the same slot, exactly one wins and the other gets a clean message. A patient who already holds the slot gets it back instead of being told it is full.
Conflicts
Every write to a queue row carries the version the screen last saw. If someone else has moved the row since, the write is refused and the screen reloads. A receptionist who taps End on a stale screen loses cleanly. They never overwrite a newer truth.
Torn writes
This is the one that surprised us.
Advancing the queue touches every row behind the patient being finished. We first wrote it as many small writes, each guarded individually. Each guard was correct. Together they were useless: a failure halfway left the first rows renumbered and the rest not, and a retry could not repair it, because the retry no longer saw the state it expected.

Our own review found this, not a clinic. The fix moved the whole advance into one transaction, under a lock per doctor’s queue. The emergency insert takes the same lock, so an emergency and an End tapped in the same second queue up behind each other instead of colliding.
The same review found a second bug, older: an emergency insert placed the patient at position one even when a consultation was in progress, producing two patients at position one. The emergency patient now goes immediately after the consultation in progress, which is what the original design intended.
What we test to prove it
Dozens of simultaneous writers on one slot. An advancer racing emergency inserts. A forced overlap on the lock. The pass condition is not “no error”, it is “exactly one winner and positions run one to N with no gaps”. That is the next article: break it before a clinic does.
What we don’t claim
We designed for peak hours and tested for them on our own harness. We publish no load figures, and we write “not verified” in our notes next to anything we have not seen run for real. If a vendor tells you a booking system is concurrency-safe, ask to see the race test.
The retry, version and transaction patterns, and the two bugs found in our own review, come from DrInCity’s migration notes and test harnesses. The advance fix is deployed. No load or latency figures are published. Pattern-level detail only, by design.
More by GarunaCDX
Let's talk about your build.
Tell us what you are building. A founding engineer replies within one working day with how we would approach it.
