AWS Builder Center
Turnstile: Making Sure Every Seat on DynamoDB Sells Exactly Once

Turnstile: Making Sure Every Seat on DynamoDB Sells Exactly Once

How a DynamoDB seat-purchasing system guarantees no double-sells under thousands of concurrent buyers, and why the design deliberately keeps TTL cleanup timing completely separate from the actual correctness guarantee.

Architect of invisible things that keep visible things alive.

Turnstile: Making Sure Every Seat on DynamoDB Sells Exactly Once

Ticket systems fail in one very specific way: two people end up thinking they bought the same seat. Everything else can be slow or a little ugly and the system still basically works. That one thing happening even once is the actual failure mode, and it's the whole brief for AWS's second DynamoDB Data Modeling Challenge, 100,000 seats, hundreds of fans buying at once, zero double-sells.
This is a genuinely different problem than the first challenge in this series. That one was about ordering under a massive write burst. This one is about mutual exclusion under concurrency, and no amount of clever ordering helps if two fans can still both end up holding the same seat.

The mistake I almost made

The obvious first instinct is to give a seat hold a TTL and let DynamoDB expire it automatically. Set an 8-minute timer, DynamoDB deletes the item when it's up, done.
Except deleting the seat item doesn't release the seat, it destroys it. A seat isn't like a queue entry that should disappear once it's served. It has to exist for the whole life of the event no matter how many times it gets held and released along the way. If TTL is sitting on the seat record itself, an abandoned hold quietly erases inventory instead of freeing it back up.
So the actual design splits a seat into two separate items that live in the same partition: a durable seat record that never gets deleted, and a separate, ephemeral hold ticket that's the only thing carrying a TTL. When someone abandons checkout, the hold ticket expires and disappears. The seat record stays exactly where it is.

Where the real guarantee comes from

Here's the part that took the longest to get right: the system's correctness against double-selling can't depend on that TTL cleanup running on time. DynamoDB TTL deletion isn't instant, it can lag by minutes in the worst case. If "no double-sell" depended on a sweeper having already reset the seat before anyone else could claim it, a slow sweeper would create a window where the seat map lies about what's actually available.
So the condition expression on every hold attempt checks the expiry directly against the seat record itself:
attribute_not_exists(status) OR status = :available OR holdExpiresAt < :now

That third clause is doing the real work. A new hold can succeed against a seat that's technically still marked HELD, as long as its own expiry timestamp has already passed, without waiting for any cleanup process to have touched it first. If a background sweeper were delayed by 30 minutes, nothing about correctness changes, the seat map just looks a little stale in the meantime. That's a UX problem, not a data integrity problem, and keeping those two completely separate is the actual design decision this whole thing rests on.
Confirming a purchase works the same way, one  TransactWriteItems  call that flips the seat to SOLD, deletes the now-unnecessary hold ticket, and writes the purchase record, all conditioned on the fan still being the current holder and the hold not having expired. If two fans click buy within milliseconds of each other, exactly one of those transactions succeeds. The other fails cleanly with a  ConditionalCheckFailedException , and that fan just sees "someone got there first" instead of anything worse.

What I considered and didn't use

Worth being honest about the alternatives, because a couple of them look reasonable on paper:
An external lock service like Redis would work, but it means running a second stateful system to solve a problem DynamoDB's own conditional writes already solve natively, single-writer-wins on one item is exactly what a conditional write guarantees. Optimistic locking with a version number would also work correctly, I set it aside mainly because a version counter tells you something changed without telling you what, while checking status and expiry directly is self-documenting for anyone reading the code six months from now. And the classic read-then-write-in-application-code pattern is rejected outright everywhere in this design, because that's precisely the gap a concurrent request races through.

Where it actually breaks (and why it's still fine)

A payment succeeding but the confirmation response never reaching the client is safe, because the purchase ID is idempotent, a retry just fails its own condition since the seat is already SOLD, and the client treats that as "already succeeded" rather than an error. A DynamoDB Streams event getting redelivered can make the display-only section counters double-count briefly, but it can never touch actual seat ownership, since that's driven by client requests through conditional writes, not by the stream processing. The one thing genuinely worth stating plainly rather than glossing over: there's roughly a 200ms-to-1-second window on the sister project (Foyer's exact position resolution) where "exact" and "instant" are in real tension, and here too, being upfront about where a design's guarantees actually end is worth more than pretending everything is instantaneous.

Repository

Full design document, access pattern matrix, and an importable NoSQL Workbench model:  github.com/JoseScript7/Turnstile 
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article