0
0
LLDsystem_design~25 mins

Why booking tests availability and concurrency in LLD - Design It to Understand It

Choose your learning style9 modes available
Design: Booking Tests Availability and Concurrency System
Design focuses on booking test slot availability and concurrency handling. Out of scope are payment processing, user authentication, and test result management.
Functional Requirements
FR1: Allow users to view available test slots in real-time
FR2: Enable users to book test slots without double booking
FR3: Handle multiple users trying to book the same slot concurrently
FR4: Provide immediate feedback if a slot is no longer available
FR5: Support cancellation and rescheduling of booked tests
FR6: Ensure data consistency and integrity during concurrent bookings
Non-Functional Requirements
NFR1: Support up to 10,000 concurrent users booking tests
NFR2: API response time p99 under 200ms for availability checks
NFR3: System availability of 99.9% uptime
NFR4: Prevent race conditions and double bookings
NFR5: Data consistency must be maintained even under high concurrency
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
❓ Question 6
Key Components
API Gateway for client requests
Availability service to check and show free slots
Booking service to handle reservations
Database with transactional support
Cache layer for fast availability queries
Locking or concurrency control mechanisms
Design Patterns
Optimistic and pessimistic locking
Eventual consistency vs strong consistency
Cache aside pattern for availability data
Queue-based booking to serialize requests
Idempotency for booking requests
Reference Architecture
Client
  |
  v
API Gateway
  |
  v
+----------------+       +----------------+
| Availability   |<----->| Cache (Redis)  |
| Service        |       +----------------+
+----------------+
  |
  v
+----------------+
| Booking Service |
+----------------+
  |
  v
+----------------+
| Database       |
+----------------+
Components
API Gateway
Nginx or AWS API Gateway
Receives client requests and routes them to appropriate services
Availability Service
Node.js or Python microservice
Checks and returns available test slots using cache and database
Cache
Redis
Stores frequently accessed availability data for fast reads
Booking Service
Java Spring Boot or similar
Handles booking requests with concurrency control and updates database
Database
PostgreSQL with transactions
Stores test slots, bookings, and user data with strong consistency
Request Flow
1. Client requests available test slots via API Gateway.
2. API Gateway forwards request to Availability Service.
3. Availability Service checks Redis cache for slot availability.
4. If cache miss, Availability Service queries Database and updates cache.
5. Client selects a slot and sends booking request via API Gateway.
6. API Gateway forwards booking request to Booking Service.
7. Booking Service starts a database transaction.
8. Booking Service checks slot availability in Database.
9. If available, Booking Service reserves the slot and commits transaction.
10. Booking Service updates cache to reflect new availability.
11. Booking Service responds to client with booking confirmation or failure.
12. Client receives immediate feedback on booking status.
Database Schema
Entities: - TestSlot(id, start_time, end_time, capacity) - Booking(id, user_id, test_slot_id, status, created_at) Relationships: - One TestSlot can have many Bookings - Booking references one TestSlot Constraints: - Unique constraint on (test_slot_id, user_id) to prevent duplicate bookings - Capacity enforced by counting active bookings per TestSlot
Scaling Discussion
Bottlenecks
Database write contention when many users book the same slot simultaneously
Cache inconsistency leading to stale availability data
API Gateway overload under high concurrent requests
Latency increase due to synchronous database transactions
Solutions
Use optimistic locking or row-level locking in database to prevent double booking
Implement cache invalidation or write-through cache to keep availability data fresh
Scale API Gateway horizontally with load balancing
Introduce booking request queue to serialize high-contention bookings
Use read replicas for database to offload read queries
Implement eventual consistency for availability display with strong consistency for booking confirmation
Interview Tips
Time: Spend 10 minutes clarifying requirements and constraints, 15 minutes designing components and data flow, 10 minutes discussing scaling and bottlenecks, and 10 minutes answering questions.
Explain importance of concurrency control to avoid double bookings
Discuss trade-offs between strong and eventual consistency
Highlight use of caching to improve availability check latency
Describe database schema supporting booking constraints
Address scaling challenges and solutions for high concurrency
Show understanding of user experience with immediate booking feedback