Urgent.News

What's breaking now, across hundreds of outlets.

Tech

How to Prevent Overselling & Double Charges in High-Load Ticket Booking (Java 21 & Spring Boot 3.4)

Selling concert tickets during a flash sale is one of the toughest concurrency problems in backend engineering. Thousands of users hit the "Book Seat" endpoint at the exact same millisecond. If you rely on a standard SELECT -> CHECK -> UPDATE flow, two threads will read the seat as AVAILABLE at the exact same time—resulting in overselling (two different buyers paying for the exact same seat).…

During a flash sale, thousands of users attempt to book concert tickets simultaneously. Using a standard SELECT - CHECK - UPDATE flow to check seat availability and update status can cause overselling - two different buyers paying for the same seat. To prevent this, the article recommends using PESSIMISTIC_WRITE with SKIP LOCKED in PostgreSQL.

This means selecting rows for update with a "SELECT * FROM seats WHERE id = :seatId AND status = 'AVAILABLE' FOR UPDATE;" query. This serializes access cleanly at the database level. For background workers scanning for available seats, adding SKIP LOCKED ensures threads don't block each other waiting on locked rows. To prevent double charges from retried requests that could create duplicate orders, the article suggests requiring a unique Idempotency-Key header from the client.

This key is atomically checked and stored in Redis with a TTL (time-to-live). If the key is new, it's locked as "IN_PROGRESS" and the booking proceeds. Duplicate requests with the same key return the cached response without touching the database. For guaranteed event delivery, the article advises against publishing directly to Kafka or RabbitMQ inside the booking transaction.

Instead, it recommends writing the event into an outbox_events table inside the same PostgreSQL transaction as the seat reservation. A background relay worker then polls the outbox_events table using FOR UPDATE SKIP LOCKED and reliably publishes the events. The full architecture, request flow diagram, and a production-ready Java 21 + Spring Boot 3.4 starter kit are available on GitHub.

The starter kit includes Docker Compose, Swagger UI, multi-threaded test cases for race conditions, and a guide for senior Java / system design interviews.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

More from Dev.to on urgent.news

Get the news by email. Yesterday's most-covered stories, every morning.

We track opens and clicks to improve the newsletter.

See today's edition · More options

More in Tech

The Sunday Papers

Sundays are for chopping up and responsibly disposing of the company credit card that you’ve had for two years but never actually used, because some of us are independent spirits who can pay for our…

More from Sunday, 6 December 2026 →