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_id | INT | PRIMARY KEY, AUTO_INCREMENT | Unique identifier for each user. |
name | VARCHAR(255) | NOT NULL | Full name of the user. |
email | VARCHAR(255) | UNIQUE, NOT NULL | User email for notifications. |
phone | VARCHAR(15) | UNIQUE | Contact number of the user. |
user_type | ENUM('admin', 'customer') | NOT NULL | Defines the type of user. |
created_at | DATETIME | DEFAULT CURRENT_TIMESTAMP | Timestamp of record creation. |
2. Vehicles
Tracks vehicles entering and exiting the parking lot.
| ColumnTypeConstraintsDescription | |||
vehicle_id | INT | PRIMARY KEY, AUTO_INCREMENT | Unique identifier for vehicles. |
license_plate | VARCHAR(20) | UNIQUE, NOT NULL | License plate of the vehicle. |
vehicle_type | ENUM('motorcycle', 'car', 'truck') | NOT NULL | Type of the vehicle. |
user_id | INT | FOREIGN KEY REFERENCES Users(user_id) | Owner of the vehicle. |
created_at | DATETIME | DEFAULT CURRENT_TIMESTAMP | Timestamp of record creation. |
3. ParkingLots
Defines metadata about a parking lot.
| ColumnTypeConstraintsDescription | |||
lot_id | INT | PRIMARY KEY, AUTO_INCREMENT | Unique identifier for parking lots. |
name | VARCHAR(255) | NOT NULL | Name of the parking lot. |
location | VARCHAR(255) | NOT NULL | Physical address of the lot. |
total_spaces | INT | NOT NULL | Total capacity of the parking lot. |
created_at | DATETIME | DEFAULT CURRENT_TIMESTAMP | Timestamp of record creation. |
4. ParkingLevels
Tracks levels within a parking lot.
| ColumnTypeConstraintsDescription | |||
level_id | INT | PRIMARY KEY, AUTO_INCREMENT | Unique identifier for parking levels. |
lot_id | INT | FOREIGN KEY REFERENCES ParkingLots(lot_id) | Associated parking lot. |
level_number | INT | NOT NULL | Level number (e.g., 1, 2). |
total_spaces | INT | NOT NULL | Total spaces on this level. |
created_at | DATETIME | DEFAULT CURRENT_TIMESTAMP | Timestamp of record creation. |
5. ParkingSpaces
Tracks individual parking spaces.
| ColumnTypeConstraintsDescription | |||
space_id | INT | PRIMARY KEY, AUTO_INCREMENT | Unique identifier for parking spaces. |
level_id | INT | FOREIGN KEY REFERENCES ParkingLevels(level_id) | Associated level. |
space_number | VARCHAR(10) | NOT NULL | Unique number/identifier for the space. |
space_type | ENUM('small', 'medium', 'large') | NOT NULL | Size of the parking space. |
is_occupied | BOOLEAN | DEFAULT FALSE | Indicates if the space is occupied. |
created_at | DATETIME | DEFAULT CURRENT_TIMESTAMP | Timestamp of record creation. |
6. Reservations
Tracks reserved parking spaces for vehicles.
| ColumnTypeConstraintsDescription | |||
reservation_id | INT | PRIMARY KEY, AUTO_INCREMENT | Unique identifier for reservations. |
vehicle_id | INT | FOREIGN KEY REFERENCES Vehicles(vehicle_id) | Reserved vehicle. |
space_id | INT | FOREIGN KEY REFERENCES ParkingSpaces(space_id) | Reserved space. |
start_time | DATETIME | NOT NULL | Start of reservation. |
end_time | DATETIME | NOT NULL | End of reservation. |
status | ENUM('active', 'expired') | NOT NULL | Status of reservation. |
7. EntriesAndExits
Logs vehicle entry and exit events.
| ColumnTypeConstraintsDescription | |||
log_id | INT | PRIMARY KEY, AUTO_INCREMENT | Unique identifier for logs. |
vehicle_id | INT | FOREIGN KEY REFERENCES Vehicles(vehicle_id) | Associated vehicle. |
entry_time | DATETIME | DEFAULT CURRENT_TIMESTAMP | Time of vehicle entry. |
exit_time | DATETIME | NULLABLE | Time of vehicle exit. |
space_id | INT | FOREIGN KEY REFERENCES ParkingSpaces(space_id) | Assigned parking space. |
8. Transactions
Tracks payments for parking services.
| ColumnTypeConstraintsDescription | |||
transaction_id | INT | PRIMARY KEY, AUTO_INCREMENT | Unique identifier for transactions. |
vehicle_id | INT | FOREIGN KEY REFERENCES Vehicles(vehicle_id) | Associated vehicle. |
amount | DECIMAL(10, 2) | NOT NULL | Payment amount. |
payment_time | DATETIME | DEFAULT CURRENT_TIMESTAMP | Timestamp of payment. |
payment_method | ENUM('cash', 'credit_card', 'app') | NOT NULL | Payment method used. |
Relationships
- Users ↔ Vehicles:
- One user can own multiple vehicles.
- ParkingLots ↔ ParkingLevels ↔ ParkingSpaces:
- One parking lot can have multiple levels, and each level can have multiple spaces.
- Vehicles ↔ Reservations:
- A vehicle can make multiple reservations.
- Vehicles ↔ EntriesAndExits ↔ ParkingSpaces:
- Each vehicle's entry/exit is associated with a specific parking space.
- 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.