A high-concurrency event ticketing backend built with Django, focused on data integrity and specialized concurrency control.
The primary challenge of this system is managing high-volume, simultaneous booking requests for limited inventory. This project implements a robust solution to the race condition problem using database-level locking and asynchronous processing.
- Row-Level Locking: Uses PostgreSQL
SELECT FOR UPDATEwithin atomic transactions to prevent double-booking. This ensures that only one request can modify a specific seat status at any given millisecond. - Idempotency: Implementation of unique idempotency keys to prevent duplicate transactions caused by network retries or client-side double-submits.
- Database Constraints: Utilization of
UniqueConstraintandCheckConstraintat the schema level as a final line of defense for data consistency.
- Task Offloading: Heavy operations such as ticket PDF generation and email notifications are handled by Celery workers via a Redis broker.
- Inventory Management: Automated cleanup of expired "Pending" reservations through periodic Celery tasks, ensuring seats return to the available pool if payment is not confirmed within the TTL (Time-To-Live).
- Backend: Python 3.12 / Django 5.0 / Django REST Framework
- Database: PostgreSQL 16
- Task Queue: Redis 7 / Celery 5.4
- Infrastructure: Docker / Docker Compose
A load test was conducted simulating 10 concurrent requests targeting a single available seat.
| Metric | Value |
|---|---|
| Concurrent Requests | 10 |
| Available Inventory | 1 |
| Success Rate | 10% (1/10) |
| Collision Rate | 0% |
| Average Latency | 125ms |
-
Environment Setup:
docker-compose up -d
-
Initialize Database:
docker-compose exec web python manage.py makemigrations docker-compose exec web python manage.py migrate docker-compose exec web python manage.py seed_data
-
Verify Concurrency Logic: A simulation script is provided to demonstrate the locking mechanism under load:
python3 demo_race.py
-
API Documentation: The OpenAPI schema is available at
/api/docs/(Swagger UI) or/api/redoc/once the server is running.
This project serves as a demonstration of handling:
- Race conditions in distributed systems.
- Pessimistic concurrency control.
- Background job processing and task scheduling.
- Containerized multi-service architecture.