A scalable, fault-tolerant ticket reservation system built with Spring Boot, Redis, and PostgreSQL. This system is designed to handle concurrent ticket reservations, ensuring each ticket can only be reserved once, even under high load.
- Distributed Locking: Uses Redis and Redisson for distributed locking to prevent race conditions during ticket reservations
- Scalable Architecture: Horizontally scalable with load balancing via Nginx
- Database Persistence: PostgreSQL for reliable data storage
- Containerized: Fully dockerized for easy deployment and scaling
- Monitoring: Includes metrics endpoints with Prometheus integration
- Transaction Management: ACID-compliant transactions for data integrity
The system is built with a microservices architecture:
- Multiple Application Instances: Horizontally scalable application nodes
- Nginx Load Balancer: Distributes traffic across application instances
- Redis: Provides distributed locking mechanism
- PostgreSQL: Stores ticket and reservation data
- Backend: Java 17, Spring Boot 3.1.3
- Database: PostgreSQL 14
- Caching & Locking: Redis with Redisson
- Load Balancing: Nginx
- Containerization: Docker & Docker Compose
- Monitoring: Micrometer with Prometheus integration
- Docker and Docker Compose
- Java 17 (for development)
- Gradle (or use the included wrapper)
-
Clone the repository:
git clone https://github.com/yourusername/ticket-reservation-system.git cd ticket-reservation-system -
Start the application using Docker Compose:
docker-compose up -d
-
The application will be accessible at:
- API Endpoint: http://localhost:80
- Individual instances: http://localhost:8081 and http://localhost:8082
- PostgreSQL: localhost:5432
- Redis: localhost:6379
POST /api/tickets/{ticketId}/reserve- Reserve a ticketPOST /api/tickets/{ticketId}/release- Release a ticket reservationGET /actuator/health- Health checkGET /actuator/metrics- Metrics informationGET /actuator/prometheus- Prometheus metrics
Docker Compose is used to manage and connect the different services. See docker-compose.yml for details.
Run unit tests with:
./gradlew testIntegration tests use TestContainers to spin up real PostgreSQL and Redis instances:
./gradlew integrationTestA concurrent test script is included to simulate high loads:
./test-concurrent.shThe core of this system is the distributed locking mechanism implemented with Redisson. This ensures that:
- Each ticket can only be reserved once
- Race conditions are prevented when multiple users try to reserve the same ticket
- Locks are released properly even if a service instance fails
The application exposes various metrics via Spring Boot Actuator and Prometheus integration. You can:
- Check the health endpoint:
/actuator/health - View metrics:
/actuator/metrics - Scrape Prometheus metrics:
/actuator/prometheus
-
Redis Connection Failure:
- Ensure Redis is running and accessible from application containers
- Check
REDIS_HOSTandREDIS_PORTenvironment variables
-
Database Connection Issues:
- Verify PostgreSQL is running
- Check database credentials in environment variables
-
Load Balancer Issues:
- Ensure Nginx configuration is correctly pointing to application instances
- Verify both application instances are healthy
Contributions are welcome! Please feel free to submit a Pull Request.
