Estimate the scale of the system. Consider daily active users, read/write ratio, storage requirements, bandwidth, and any relevant QPS calculations...
The API's will be designed as follows
GET v1/lots/getavailablelots - returns a list of available lots which can be taken
POST v1/lots/booklot/{lotid} - this API will book a lot for a user who is entering the parking lot
POST v1/lots/reserverlot/{lotid} - this API will reserve a lot of when a user is booking for future/ the request object will consist of the user id if it is a registered user or the vehicle number for when the user will be arriving it should also contain set the hold time as per default threshold or a user given threshold when up to 30 mins not more than that if the user provides something more than that then it will default back to 30 mins post which the parking lot will be relinquished to the pool.
From a high level design there are 2 inputs we will have to consider one which is from the user who is entering the parking lot but also from the vehicle parking spaces, since the spaces are segregated between the types of the vehicles it should be updated when it is occupied, hence certain types of vehicles can be stopped at the entrance. The backend services will be different micro services that will be distributed between API gateway that will redirect to Booking service, payment service and availability service. the data base will also have a master and slave model so all the reads can go to the slaves and the write requests can go to the master. But we will also have to add a read after write so that the data bases stay synced.So the user journey will be like the following :
User Entry --> API gateway --> load Balancer --> Booking service --> availability Service --> if available then book and return location number --> Payment service return payment link --> user pays and the slot is booked --> DB for updating record
User exit --> API gateway --> Load balancer --> Booking service --> udpate DB with open slot
User reserve slot --> API gateway --> load balancer --> Booking service --> availability service--> if available then book the slot --> Payment service return payment link --> if payment successful then book slot --> DB update
Define the data model. Identify the main entities, their attributes, and relationships. Consider the choice of database type (SQL vs NoSQL) and justify your decision based on access patterns...
ok lets talk about the reservation service or booking service. since this will be handling many cases of double booking and payment confirmation and other aspects as well. this is how we will be handing the different scenarios :
SELECT ... FOR UPDATE locks the spot row, the transaction verifies no overlapping active hold exists, then inserts the reservation and commits. The first request wins the row lock and commits; the second blocks, re-reads after the lock releases, sees the conflict, and aborts with 409. The client re-checks availability and retries.