Skip to content
Back to all projects

Backend

TicketRush

A ticket booking backend that never sells more seats than it has, however many requests arrive at once. The stock decision is one Lua script in Redis; the order itself is written to PostgreSQL afterwards through a RabbitMQ queue. In the public demo every visitor gets a private stock of tickets.

Demo

LIVEOpen the live site

The deployed project opens in a new tab.

May be asleep

Problem

The classic ticketing bug is check-then-update: the code reads the stock, sees one ticket left, then decrements it. If two requests read at the same moment, both see one ticket and both get a sale. Locking the database row fixes that, but under load every request queues on the same row and the bottleneck simply moves into the database. I tried the opposite arrangement here. The decision is made in memory, in front of the database; anything beyond the stock is rejected immediately, and the accepted purchases are written to the database one after another at the database's own pace.

Highlights

  • Checking and decrementing the stock happens in one Redis Lua script that returns 1 after a DECR when the value is above zero and 0 otherwise. Redis runs commands one at a time, so no other request can slip in between.
  • Once the stock is gone, a request is rejected with a 400 before it touches the database or the queue (TicketSoldOutException).
  • An accepted purchase is dropped onto the durable ticket.queue in RabbitMQ as JSON. The consumer saves the order to PostgreSQL and lowers the database stock too.
  • The result reaches the UI over WebSocket with STOMP (/ws-ticket), so nobody has to refresh the page.
  • On every start the Redis counter is rebuilt from the real stock in the database, so a restart cannot resell a ticket.
  • load-test.js sends 100 requests at an event with 10 tickets. The output recorded in the README shows 10 sales, 90 sold-out answers and 0 server errors.

Architecture and notes

A request arrives at TicketController and TicketService runs the Redis script first. If the script succeeds, a TicketRequest carrying the user and the event is put on the queue and the API answers 200 right away. Writing the order to the database happens after that, at the speed of the queue. If the database slows down the queue grows, but the number of sales cannot pass the counter in Redis. The event and 2000 test users are created at startup when the database is empty.

The public demo runs on a free 512 MB server, and there is no RabbitMQ there. Under the demo profile the producer and consumer split stays as it is, but the queue is a LinkedBlockingQueue with one worker thread. The demo also gives each visitor a private stock through the X-Visitor-Id header, so one person buying everything does not leave the next visitor with an empty system. Those counters live in Redis with a 24 hour TTL, and a cleanup job that runs every half hour deletes the order and stock rows of expired visitors. While the server wakes up, /api/health answers 503 and the UI shows that as a waiting screen.

The weak spot: the consumer catches and logs its errors but does not compensate. If the database write fails after a ticket was already taken in Redis, the two sides drift apart; a dead-letter queue and retries would be the answer. The automated tests only check that the application starts, and for now the concurrency behaviour can only be tried by hand with the load test script (the README has a sample output).

Live from GitHub

Stars
1
Forks
0
Last updated

Languages

  • Java57.8%
  • TypeScript21.8%
  • JavaScript15.6%
  • CSS2.3%
  • Dockerfile1.4%
  • Other1.2%

Fetched from the public GitHub API and refreshed hourly.

From the README

TicketRush, popüler konserler ve etkinlikler gibi yüksek talep gören senaryolarda race condition sorunlarını çözen, event-driven ve tam dağıtık bir bilet satış sistemidir.

Amaç, binlerce kullanıcının aynı anda “Satın Al” butonuna bastığı durumda bile:

  • Veri tutarlılığını sağlamak
  • Overselling riskini ortadan kaldırmak
  • Sistemi kilitlemeden, ölçeklenebilir bir mimari ile süreci yönetmektir.

Teknoloji Yığını

Aşağıdaki teknoloji yığını, yüksek trafikli sistemlerin ihtiyaçları olan ölçeklenebilirlik, tutarlılık ve performans kriterlerine göre seçildi.

…

Read the full README on GitHub (opens in a new tab)

Technologies

  • Java 21
  • Spring Boot 3
  • React
  • TypeScript
  • PostgreSQL
  • Redis
  • RabbitMQ
  • WebSocket
  • Docker

Request flow

Hover or focus a component to see what it is.

A generic request path built from the project's tech list. It illustrates the stack; it is not a measured trace.