Skip to content
cezar.garcia
All projects

Personal project · 2026

Lotou

Concert ticketing that survives the on-sale rush without overselling.

JavaSpring BootNestJSFastAPIAngularKafkaRedisPostgreSQL
78,621
reservation attempts answered in 40 s (19,415 with a Postgres lock)
2.98 s
p99 latency at on-sale (20.5 s on Postgres)
0
oversold tickets or seats sold twice
35
architecture decision records (ADRs)

Overview

Lotou is a ticketing platform for concerts. The producer creates and publishes the show and follows sales. The buyer picks a section or a seat, pays, gets a ticket with a QR code and walks in through the gate.

I built it in phases to show, with evidence, how I design a distributed system: every piece has a reason, every decision has an ADR and every promise has a test or a measurement behind it.

A show page in the Lotou store, with the poster, the sections and the purchase panel
The show page: every show is a poster in the artist's colour.

The problem

Ticketing is the textbook case of a concurrency spike. When a big show goes on sale, tens of thousands of people compete for a few thousand tickets in the same minute.

At that moment the system has three duties: never oversell, never sell the same seat twice and stay up. The domain also has two kinds of stock, each with its own reservation strategy: general admission (a quantity) and numbered seats (a unique item).

Architecture

Eight services in three languages sit behind a gateway. Java goes where there is concurrency and transactions, Node handles the catalogue, the gate and the mock payment provider, and Python handles data, real time and notifications.

The user path is synchronous HTTP. Everything else is events and commands on Kafka, written to an outbox in the same transaction as the change. The only synchronous call between services reads the reservation when the order is created, because the buyer needs to know right away whether it still holds.

  • api-gateway (Java, Spring Cloud Gateway): routing, JWT validation and rate limiting on checkout.
  • inventory-service and order-service (Java, Spring Boot): reservations, virtual queue, orders, coupons and the purchase and refund sagas.
  • event-service, checkin-service and payment-mock (Node, NestJS): cached catalogue, gate check-in and a mock payment provider.
  • analytics-service and notification-service (Python, FastAPI): live dashboard, financial report, ticket PDFs and emails.
  • One database per service: Postgres, MongoDB and two Redis instances with opposite memory policies (cache and stock).
Lotou architecture diagram: browser, gateway, services, Kafka, RabbitMQ, databases and observability
The browser talks to the gateway; services exchange facts and commands over Kafka.

Atomic reservation under contention

The first version reserved with a Postgres lock (SELECT ... FOR UPDATE). It is correct, but the lot row becomes a queue and every waiter holds a pool connection. I kept that engine in the code as the baseline.

The current version is a Lua script in Redis, which runs as a whole with no other command interleaved: it checks idempotency, the queue token, the per-CPF quota and the stock, holds the seats (all or none) and writes the result to a stream that feeds Postgres.

Both engines go through the same tests and the same k6 load test, which checks in the database at the end that nothing was sold beyond capacity. The numbers come from a single 4-CPU machine, so the comparison is what matters.

  • On-sale, 2,000 users for 40 s: 78,621 attempts answered with Redis and Lua, against 19,415 with the Postgres lock.
  • p99 latency of 2.98 s against 20.5 s (with 18 timeouts on Postgres).
  • 1,000 buyers after the same 50 seats: p99 of 283 ms against 1.95 s.
  • For high-demand shows, a virtual queue draws positions at opening time and admits a fixed number of people per second.
Lotou checkout with the numbered seat map
The seat map updates by itself when someone else reserves (SSE).

Payment as a saga and the live dashboard

The buyer only waits for the reservation. Payment is a saga orchestrated by the order-service: order, payment and reservation confirmation exchange commands and events over Kafka, with a transactional outbox. A declined payment or an expired reservation is compensated without leaving a ticket stuck.

After payment, RabbitMQ workers render the PDF with the QR code and send the email, with retry, priority and a DLQ. The producer follows it all on a WebSocket dashboard: a sale shows up there less than half a second after the click on pay.

  • 20 simultaneous clicks on buy for the same reservation become a single order, charged once.
  • With inventory down in the middle of checkout, the order stays pending and completes later, with no second charge.
  • Cancellation with a full refund: the ticket goes back on sale and so does the coupon use.
  • A cancelled show refunds every order at 20 per second, so the payment provider is not overloaded.
Trace of the purchase saga in Grafana Tempo, going through api-gateway, order-service, payment-mock, inventory-service and analytics-service
From the click on pay to the confirmed order: 5 services and 67 spans in 250 ms.
The producer's live dashboard, with tickets sold, revenue, sales by section and an activity feed
The producer dashboard receives sales and revenue over WebSocket.

Tickets and gate check-in offline

At the door of a sold-out show, the signal disappears. That is why the store is a PWA: tickets for upcoming shows are stored on the phone, QR code included, and open with no connection.

The gate works offline too. The QR code is signed with Ed25519: when the show is opened, the scanner downloads the ticket list and the public key, validates on the phone itself and uploads the scans when the network is back. A double entry across offline devices becomes a recorded conflict with an alert.

The checkin-service keeps its own copy of the tickets, fed by Kafka, and keeps validating even when the order service is down.

Four mobile screens of the store: the storefront, the show page, the ticket with its QR code and the saved tickets opening offline
The store on a phone, from the poster to the ticket saved on the device.
Four mobile screens of the gate app: gate selection, entry granted offline, a scan waiting for the network and a double entry detected
The gate validates on the phone itself and syncs when the network is back.

Observability, load and chaos

Every request produces a trace that starts in the browser and crosses the services, Kafka, the database and Redis. The OTel Collector masks CPF numbers and emails before forwarding to Tempo, Prometheus and Loki.

Grafana comes up with six dashboards and thirteen alerts: invariants queried straight from the databases (overselling, double entry, stuck saga), checkout SLOs with burn-rate alerts, and messaging. The alerts caught a real incident during development, with 626 orders stuck, and the cause was fixed.

  • Integration tests against real databases with Testcontainers; end to end with Playwright.
  • Gate under load: 30,000 entries at 150 scans per second, 1,500 repeats blocked, no double entry, p99 of 145 ms.
  • A 100-use coupon fought over by a thousand buyers: exactly 100 orders with the coupon.
  • Eight chaos experiments, including Redis restarted in the middle of on-sale: the state came back and no sale went over capacity.
  • 35 ADRs, each written at the time of the decision.
Messaging dashboard in Grafana during a load test, with zero consumer lag and no messages in the DLQs
Messaging during a load test: zero lag and empty dead-letter queues.

Full stack

Back end
Java 21Spring Boot 4Spring Cloud GatewayNode.jsNestJS 12Python 3.13FastAPI
Front end
Angular 22PWASSEWebSocket
Data and messaging
PostgreSQLMongoDBRedis (Lua, Streams)KafkaRabbitMQ
Security
KeycloakOAuth 2.0JWTEd25519
Observability
OpenTelemetryGrafanaPrometheusLokiTempo
Testing and delivery
TestcontainersPlaywrightk6ToxiproxyDocker ComposeGitHub Actions