-The parking lot system should work for vehicles of all sizes
-There should be a working gate system for timein/out
-There should be a working payment system when the driver reserves the spot. When the driver leaves we should check to see if the time was within the reservation or if we should tack on a fee
-Drivers should be able to resserve a spot beforehand. If 2 drivers try to resserve at the same time, just give the first driver the spot
-This system should be highly available
-System shouuld work for 1000s of lots accross states and countries
Each lot has approx 500 spots of which 50% get reserved per day. There are likely 5000 lots per country.
Assume 10 countries
So that is 250 * 5000 * 10 = 12500000 spots reserved a day (12.5 M/day)
12.5 M/day / 86400 = 145 writes/s = assuming 3x at peak is 435 writes/s
(16) + lot_id (8) + spot_id (8) + user_id (16) + plate (12) + start_ts (8) + end_ts (8) + status (4) + created_at (8) ≈ 128 bytes
12.M * 128 = 1.6 GB/day
1.6 GB/day → ×365 ≈ 584 GB/year → ~1.2 TB over 2 years.
I think reads will outweigh writes but not by that much. We can say reads 5x writes. 2125 reads/s at peak
Post
Get:
Ok well since this is a slightly heavier on the read side:
Lets have several servers running out Parking lot reservatin manager. Behind this we'll have a load balancer for rate limiting based on thorughout (IP level rate limiting). Then we will have a SQL data base. It will be sharded and hashed by lot id. So we can have mod N shards. One potential issye wiht this is hotter lots (more reservation traffic) could land on one shard. Any time a user reserves a spot, we will push the status to reserved in the database. In order to make reads faster we can have a Redis cache before the database that will hold the most commonly visited lots and availability. The database will be the official source of truth so when an actual payment is submitted we will query the data base to ensure the spot is actually available. When a user actually checks in, we will again modify the database directly. The cache is to speed up the reads
This will be a SQL database.
Reservation table
-reservation_id PK
-lot_id
-spot_id
-time_reserved_start
-time_reserrved_end
-created_at
-Status (checked_in bool)
Lot table
-lot_id PK
-location
Spot table
-spot_id PK
-lot_id
-vehicle_id (null if empty)
-status (Reserved, empty)
Vehicle
-vehicle_id
-plate
-type
Lets start with the availability cache. When a user actually reserves a spot, we can update the cache at the same time before returning. That means the next read will be up to date. Another option is we can have a somewhat short TTL (2 mins maybe) and we can query the database for updated records. This will be ok even if the results are a little stale because if a user decides to reserve a spot the database is still checked.
Lets also go into the status flip. I know we can use a remote procedural call to make the payment and update the status of the spot and create a reservation in our table all together. If anything fails the entire process will roll back. thnking now, we shuold keep a ledger table maybe for payments and also update that within this call. When makingt he call to update the spot to reserved we will do a Update spot SEt status = reserved where status = empty. This is so another person checking at same time will not be able to book the spot. Since one has to finish befroe the other.