System requirements


Functional:

Vehicle Entry & Exit:

  • Vehicles should be allocated parking spaces dynamically upon entry.
  • Support payments (e.g., hourly, flat fee, subscription).
  • Track entry and exit times for billing purposes.

Vehicle Types:

  • Allocate spaces based on vehicle size (e.g., small for motorcycles, large for trucks).

Space Management:

  • Handle real-time updates for available spaces.
  • Optimize space usage by considering vehicle types and space sizes.

User Notifications:

  • Notify users of available spaces or reservations via an app or screens.

Accessibility:

  • Reserve designated spaces for accessibility (e.g., disabled parking).

Security:

  • Monitor vehicles using surveillance (e.g., cameras, RFID tags).




Non-Functional:

Scalability:

  • Handle high-volume scenarios (e.g., stadium events).

Reliability:

  • Ensure system uptime with minimal disruptions.

Performance:

  • Quick allocation of parking spaces to reduce wait times.

Security:

  • Secure payment processing and user data.




Capacity estimation

50 cities in U.S, 100 parking lots per city, 200 slots per lot. 1 million slots per day. if avg parking time is 4 hours, 6 million QPS per day.





API design

1. Vehicle Management

  • Register a vehicle:
http Copy code POST /api/v1/vehicles { "licensePlate": "ABC123", "type": "car" // Options: motorcycle, car, truck }
  • Response: 201 Created
json Copy code { "vehicleId": "1234", "licensePlate": "ABC123", "type": "car" }
  • Get details of a vehicle:
http Copy code GET /api/v1/vehicles/{vehicleId}
  • Response: 200 OK
json Copy code { "vehicleId": "1234", "licensePlate": "ABC123", "type": "car", "status": "parked" }

2. Parking Space Management

  • Get available parking spaces:
http Copy code GET /api/v1/parking-spaces?type=car&level=1
  • Response: 200 OK
json Copy code { "availableSpaces": [ { "spaceId": "A101", "level": 1, "type": "car" }, { "spaceId": "A102", "level": 1, "type": "car" } ] }
  • Reserve a parking space:
http Copy code POST /api/v1/parking-spaces/reserve { "vehicleId": "1234", "spaceId": "A101" }
  • Response: 200 OK
json Copy code { "reservationId": "5678", "vehicleId": "1234", "spaceId": "A101", "status": "reserved" }

3. Entry and Exit

  • Log vehicle entry:
http Copy code POST /api/v1/entry { "vehicleId": "1234" }
  • Response: 200 OK
json Copy code { "entryId": "7890", "vehicleId": "1234", "entryTime": "2024-11-23T10:00:00Z" }
  • Log vehicle exit and calculate fee:
http Copy code POST /api/v1/exit { "vehicleId": "1234" }
  • Response: 200 OK
json Copy code { "exitId": "4567", "vehicleId": "1234", "exitTime": "2024-11-23T14:00:00Z", "parkingFee": 20.00 }

4. Payments

  • Process payment:
http Copy code POST /api/v1/payments { "vehicleId": "1234", "amount": 20.00, "paymentMethod": "credit_card" }
  • Response: 200 OK
json Copy code { "paymentId": "9988", "status": "success", "amount": 20.00 }

5. Administrative APIs

  • Get parking lot stats:
http Copy code GET /api/v1/admin/stats
  • Response: 200 OK
json Copy code { "totalSpaces": 500, "occupiedSpaces": 300, "availableSpaces": 200, "revenue": 1500.00 }





Database design

1. Users

Stores information about customers and admins.

ColumnTypeConstraintsDescription
user_idINTPRIMARY KEY, AUTO_INCREMENTUnique identifier for each user.
nameVARCHAR(255)NOT NULLFull name of the user.
emailVARCHAR(255)UNIQUE, NOT NULLUser email for notifications.
phoneVARCHAR(15)UNIQUEContact number of the user.
user_typeENUM('admin', 'customer')NOT NULLDefines the type of user.
created_atDATETIMEDEFAULT CURRENT_TIMESTAMPTimestamp of record creation.

2. Vehicles

Tracks vehicles entering and exiting the parking lot.

ColumnTypeConstraintsDescription
vehicle_idINTPRIMARY KEY, AUTO_INCREMENTUnique identifier for vehicles.
license_plateVARCHAR(20)UNIQUE, NOT NULLLicense plate of the vehicle.
vehicle_typeENUM('motorcycle', 'car', 'truck')NOT NULLType of the vehicle.
user_idINTFOREIGN KEY REFERENCES Users(user_id)Owner of the vehicle.
created_atDATETIMEDEFAULT CURRENT_TIMESTAMPTimestamp of record creation.

3. ParkingLots

Defines metadata about a parking lot.

ColumnTypeConstraintsDescription
lot_idINTPRIMARY KEY, AUTO_INCREMENTUnique identifier for parking lots.
nameVARCHAR(255)NOT NULLName of the parking lot.
locationVARCHAR(255)NOT NULLPhysical address of the lot.
total_spacesINTNOT NULLTotal capacity of the parking lot.
created_atDATETIMEDEFAULT CURRENT_TIMESTAMPTimestamp of record creation.

4. ParkingLevels

Tracks levels within a parking lot.

ColumnTypeConstraintsDescription
level_idINTPRIMARY KEY, AUTO_INCREMENTUnique identifier for parking levels.
lot_idINTFOREIGN KEY REFERENCES ParkingLots(lot_id)Associated parking lot.
level_numberINTNOT NULLLevel number (e.g., 1, 2).
total_spacesINTNOT NULLTotal spaces on this level.
created_atDATETIMEDEFAULT CURRENT_TIMESTAMPTimestamp of record creation.

5. ParkingSpaces

Tracks individual parking spaces.

ColumnTypeConstraintsDescription
space_idINTPRIMARY KEY, AUTO_INCREMENTUnique identifier for parking spaces.
level_idINTFOREIGN KEY REFERENCES ParkingLevels(level_id)Associated level.
space_numberVARCHAR(10)NOT NULLUnique number/identifier for the space.
space_typeENUM('small', 'medium', 'large')NOT NULLSize of the parking space.
is_occupiedBOOLEANDEFAULT FALSEIndicates if the space is occupied.
created_atDATETIMEDEFAULT CURRENT_TIMESTAMPTimestamp of record creation.

6. Reservations

Tracks reserved parking spaces for vehicles.

ColumnTypeConstraintsDescription
reservation_idINTPRIMARY KEY, AUTO_INCREMENTUnique identifier for reservations.
vehicle_idINTFOREIGN KEY REFERENCES Vehicles(vehicle_id)Reserved vehicle.
space_idINTFOREIGN KEY REFERENCES ParkingSpaces(space_id)Reserved space.
start_timeDATETIMENOT NULLStart of reservation.
end_timeDATETIMENOT NULLEnd of reservation.
statusENUM('active', 'expired')NOT NULLStatus of reservation.

7. EntriesAndExits

Logs vehicle entry and exit events.

ColumnTypeConstraintsDescription
log_idINTPRIMARY KEY, AUTO_INCREMENTUnique identifier for logs.
vehicle_idINTFOREIGN KEY REFERENCES Vehicles(vehicle_id)Associated vehicle.
entry_timeDATETIMEDEFAULT CURRENT_TIMESTAMPTime of vehicle entry.
exit_timeDATETIMENULLABLETime of vehicle exit.
space_idINTFOREIGN KEY REFERENCES ParkingSpaces(space_id)Assigned parking space.

8. Transactions

Tracks payments for parking services.

ColumnTypeConstraintsDescription
transaction_idINTPRIMARY KEY, AUTO_INCREMENTUnique identifier for transactions.
vehicle_idINTFOREIGN KEY REFERENCES Vehicles(vehicle_id)Associated vehicle.
amountDECIMAL(10, 2)NOT NULLPayment amount.
payment_timeDATETIMEDEFAULT CURRENT_TIMESTAMPTimestamp of payment.
payment_methodENUM('cash', 'credit_card', 'app')NOT NULLPayment method used.

Relationships

  1. Users ↔ Vehicles:
    • One user can own multiple vehicles.
  2. ParkingLots ↔ ParkingLevels ↔ ParkingSpaces:
    • One parking lot can have multiple levels, and each level can have multiple spaces.
  3. Vehicles ↔ Reservations:
    • A vehicle can make multiple reservations.
  4. Vehicles ↔ EntriesAndExits ↔ ParkingSpaces:
    • Each vehicle's entry/exit is associated with a specific parking space.
  5. Vehicles ↔ Transactions:
    • A vehicle can have multiple transactions.






High-level design

User calls api gateway to get authenticated and then the request will be forwarded to parking service if the user is authenticated. the reservation, entry, exit and payment will be saved to DB to make sure the state are permanently stored.






Request flows

Entry Process:

  • Vehicle approaches gate → System scans license plate/RFID or issues a ticket → Allocates space → Opens gate.

Parking:

  • User follows instructions to the allocated space → Sensors confirm occupancy.

Exit Process:

  • System calculates fee → Processes payment via app/kiosk → Opens exit gate.




Detailed component design

Database will be SQL DB with replicas to avoid single point failure, replica can serve as master DB when there is fail over happens.

Load balancing to route to the machine with light load or faster response.





Trade offs/Tech choices


Database Choice:

  • Start with PostgreSQL for relational consistency (e.g., reservations, transactions).
  • Move to a NoSQL database (e.g., MongoDB) for storing real-time occupancy data if scaling becomes an issue.

Space Allocation Algorithm:

  • Use a simple first-available algorithm initially.
  • Upgrade to an optimized allocation algorithm (based on proximity or user preferences) as the system grows.

IoT Sensors:

  • Start with cost-effective ultrasonic sensors for smaller lots.
  • Scale to vision-based systems for larger, more complex parking lots.

Real-Time Updates:

  • Use Redis for lightweight caching and pub/sub for updates.
  • Transition to Kafka for handling higher throughput as the system scales.





Failure scenarios/bottlenecks

avoid single point of failure in all components

distribute the parking spaces to different levels or physical areas to avoid traffic congestion inside the parking lot.




Future improvements

  • Integrate with public transportation for last-mile connectivity.
  • Use ML models to predict peak times and optimize space usage.