How do you make a booking retry safe across API instances?
After this you can answer: How do you make a booking retry safe across API instances?
Say it in 30 seconds
Section titled “Say it in 30 seconds”The deeper answer
Section titled “The deeper answer”A pre-check alone is insufficient: two requests can both observe no previous booking. The unique database constraint settles that race. For two events using the same user and key, one transaction can lose its insert after decrementing seats; the entire losing transaction rolls back, then the query reads the winner’s booking.
The service compares the stored event and quantity with the new input. Matching parameters replay the public booking; different parameters yield a conflict. Existing successes remain replayable even after the event sells out or starts.
This implementation stores successful bookings rather than every HTTP response. A rejected booking writes no success row, so the same key may try again later. I would document that contract explicitly for clients. Keys do not expire in this schema, which simplifies correctness at the cost of retaining them with every booking.
Counter-questions
Section titled “Counter-questions”Why scope the key to the user?
What if the two requests arrive simultaneously?
Would a Redis SET NX be enough?
Where in MiniTickets
Section titled “Where in MiniTickets”Read Idempotency, then inspect the rds-postgres piece →.
apps/api/src/services/bookings.ts → bookTicketRead this book online: https://book.theversehub.in/