Other considerations include indoor vs. outdoor parking, additional services, valet parking, and specific location selection.
Let's assume:
Estimation:
This estimate leads to using a relational database for storage as the size is well within the limits of the a relational database and not much vertical scaling will be needed due to the low memory usage. Also strong consistency is needed to make sure there are no double bookings. The response time does not need to be super fast so that is a trade off worth taking for this scenario.
Redis or Memcached: Utilize these for caching frequently accessed data such as reservation availability and parking spot statuses.
• Benefits:
• Reduced Database Load: Offload repetitive read operations to the cache.
• Improved Response Times: Provide faster access to data, enhancing user experience during peak times.
• Implementation Strategies:
• Cache Invalidation: Ensure that caches are updated or invalidated appropriately when reservations are made, modified, or canceled.
• Data Partitioning: Segment cached data based on regions or parking lot IDs to optimize access patterns.
Public/External API (Used by users)::
Private/Internal API (Used by business/internal systems)
Reservation table:
Vehicle_type table
lot table:
lot_capacity table:
contact_info_table:
user table:
vehicle table:
transaction table:
There can be enhancements, e.g., reviews for the parking lot, and reviews for the users. But for now, I will focus on the tables above.
CDN caches static contents for high performance.
Rate Limiter protects teh service from DDOS attacks
Load Balancer distributes requests to App Servers using weighted round robing algorithm
RDB is DB
App Server uses Redis Cache for performance gain
check_capacity() is handle by Reservation Server consulting the data in the database.
reserve_spot() is handled by Res Server which creates a new entry in Res table. It will create a request for payment, which queued by Message Queue and submitted to the external Payment System
reserve_spot() amy fail if multiple users are trying to book the same spot. Return an error and askl the client to try calling reserve_spot() again.
It will also fail if the lot is full. In that case, the client should not retry. Res server may return helpful information such as estimated time spots may open up in the future.
vehicle_arrived() and vehicle_left() are handled by Trans server, which modifies the Transaction table to keep track of vehicle checkins and checkouts
If vehicle does not arrive after some number of hours, reservation should be cancelled and user would be charged for 1 day of payment.
If incosisten state happens, e.g. vehicle_left() is called before corresponding vehicle_arrived(), administrator at the lot should be notified. The service should assume the vehicle arrived around the corresponding reservations start time.
Sequence diagram depicts how messages flow for client to make a reservation and pay for it, and for the parking lot system to notify the Transaction System when the user's vehicle arrives and leaves.
It is important to find an open spot when reserve_spot() call is amde. We could chop 24 hours into 15 minutes. One day would be represented by 96 slots. Since we just need to store occupied / unoccupied, we can use on bit to represent the 15 minute slot. 1 spot requires 96 bits per day. 200,000 spots * 96 bits = 20,000,000 bits / 8 = 2,500,000 bytes = 2.5MB * 365 days =912MB / year
When reserve_spot(start_time, end_time) is called the algorithm would be:
This algorithm would find the closest parking spot that is available for the desired time slot
Because this search is bound by the number of parking slots (100s) and the granularity of reservation (15 minutes), it would be performant enough.
It would probably be possible to use interval tree data structure to optimize further.
The biggest decision was the database type and structure. SQL was chosen over NOSQL for reasons such as DB size that wouldn't require expensive vertical scaling, strict schemas, and strong consistency. New SQL could also be considered but it is more difficult to maintain/setup and SQL is likely better given this specific scenario.
When a large number of people gather in one spot for a special event liekly to reach max capacity. Might put a burder on reservation work flow. Transaction server would be fine since it is limited by the number of spots.
To prepare for events like this, key components shoudl be replicated horizontally to have enough capacity. Rate limits have to be configured to smooth out extreme level of traffic.
Database tables should be replicated multiple ways:m one within data center, copy in another data center, copy in different region. This is for fault tolerance, disaster recovery, and mitigate single point of failure.
Monitoring system must be in place to monitor the health of all the servers and components.
More api endpoints like user signup,etc.
More calculations considering variations in reservations throughout the year/days
Additional services, more vehicle and parking types (different sizes)
Optimizations and availability improvement based on geographic locations would be a good area to invest further. Using global load balancer so that clients get routed to the closes data center. Replicating databases between different geographic locations as a backup mechanism in case of a regional disaster