For the capacity estimation we should assume for 1 lotthe following:
Keeping the above in mind if we scale to 10 countries x 100 lots across countries these numbers get larger very fast hence it brings into concern more and 800000 cars a day across continents and the application officially operating 24 hours. we are looking at a much higher load. following are some quick 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
The database that I will use is a noSql data base since the number of relations are lesser and it is a highly read write intensive data base also replication in case of the NoSQL data base is better when creating clones hence I will use Cassandra for example. for larger loads we can perform sharding to break down the database reads into smaller chunks at the routing layer using a stateless proxy instances between the service and the database
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.