High-Level Design
Describe the overall system architecture. Identify the main components needed to solve the problem end-to-end. Use the diagramming tool to create a block diagram.
The main class of this is the ParkingSlot
ParkingSlot
{
car_park_id: long,
slot_id: long,
is_booked : boolean,
booking_start_time: DateTime,
booking_end_time: DateTime,
is_occupied: boolean,
category: Vehicle_Size,
latitude: float,
longitude: float,
in_time: DateTime
}
Vehicle_Size Enum {
SUV , SEDAN, COUPE, HATCH_BACK, MOTOR_CYCLE }
Following are the main components to solve the problem end to end
- Load Balancer
- We will use Nginx to distribute requests from clients to the app servers that host the UI components.
- Application server
- UI
- We will start with 3 UI servers. Nginx will distribute the requests to individual server based on load
- Backend API
- Slot management service
- Reservation service
- Payment service
- Cache
- Cache will be used to store the most commonly searched park's slots
- This will be an LRU cache
- Cache will be invalidated any time the park's slots are updated
- Cache will also come with a TTL of 30 mins
- DB
- We will be using an RDBMS based DB like MySQL as we have more structured data and no joins as such
- We need to write and read in equal measures and hence we will use MySQL
- We will be sharding the database into 3 shards and we will split the data according to the park's id. this will ensure that the data is split evenly
- API Services
- Slot Management service
- This service is used by the UI to collect information about all the slots of a particular parking lot
- Slot management service will first query the cache with the given parking id. If it's available, it serves that data.
- If cache miss, it fetches the data from DB and updates in cache and then returns the list of all SlotInfo for that parking lot
- Slot management service will also provide information about an individual slot
- this is done by looking up the cache. If miss, we retrieve from DB and update in cache
- Slot management service will also handle check in and checkout of the car
- Check in
- At the check in gate, the user shows his booking id QR code and the check in API is called
- check in service will mark the check in time of the car in the database
- Check out
- At the check out gate, the user shows his booking id QR code and the check out API is called
- Check out service will ensure that the total time spent ( check out time - check in time ) is within the reserved time and then allows to exit. The Check in and check out fields are cleared and the slot is marked available
- If any extra time is spent, we need to direct to payment service and allow to exit only after payment is successful.
- Reservation service
- This service is used to reserve an available slot
- Service will directly check in the database and lock the slot.
- Session is started for 5 mins. The transaction needs to be closed within that time . Each slot will correspond to one row in the table and it will be locked for the duration of the session. This will ensure that if two people try to reserve at the same time only one will be successful and the other will get an error message saying slot is unavailable and ask to try another slot
- Reservation service will call the payment service to complete the payment.
- Payment service
- The Payment service is called by the reservation and the check out services to complete payment
- Reservation payment
- The payment service is called by reservation service along with the payment details
- The payment service will then call the Payment gateway ( Stripe / Visa / UPI ) along with the payment details and an idempotent key.
- The payment service will also provide a call back endpoint to the payment gateway. The gateway will call the endpoint after the transaction is complete
- Once the callback API is hit, we update the calling service -> reservation
- Reservation service then updates the SlotInfo in DB and the session is closed successfully and lock on DB is also released
- Checkout service
- The payment service is called by reservation service along with the payment details
- The payment service will then call the Payment gateway ( Stripe / Visa / UPI ) along with the payment details and an idempotent key.
- The payment service will also provide a call back endpoint to the payment gateway. The gateway will call the endpoint after the transaction is complete
- Once the callback API is hit, we update the calling service -> checkout
- Checkout service will then update the gates to open if payment is successful