Delivery Tracking System

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-11-30

Problem Statement

Design a Delivery Tracking System that enables customers, delivery partners, and merchants to track the real-time location and status of deliveries — such as packages, food, or courier shipments.

The system should support real-time location updates, delivery status transitions, and notifications for users.


🎯 Objective


Build a scalable, fault-tolerant, and real-time delivery tracking platform that allows:


•Customers to track delivery status and live location
•Delivery partners to update their position and status
•Merchants/admins to monitor delivery operations efficiently


✅ Functional Requirements


1. User Roles


•Customer: Can track active deliveries, view ETA, and get notifications.
•Delivery Partner: Shares live location and updates delivery status (picked up, on the way, delivered).
•Merchant/Admin: Can view delivery progress, partner availability, and performance analytics.


2. Delivery Creation


•A new delivery is created when an order is placed.
•Each delivery has:
•Delivery ID
•Pickup location
•Drop-off location
•Assigned delivery partner
•Current status (CREATED, PICKED_UP, IN_TRANSIT, DELIVERED, CANCELLED)


3. Real-time Tracking


•Delivery partner’s mobile app sends GPS coordinates periodically (e.g., every 5–10 seconds).
•The system ingests and updates partner location in near real-time.
•Customers can view current location, route, and ETA on the map.


4. Status Updates


•Partner can update delivery status via app.
•System automatically updates timeline (e.g., “Order Picked Up at 1:30 PM”, “Arriving in 5 minutes”).
•Status updates are broadcasted to customer and merchant dashboards.


5. Notifications


•Send push/SMS notifications when:
•Delivery is assigned
•Package is out for delivery
•ETA changes significantly
•Order is delivered or cancelled


6. ETA Calculation


•Compute ETA dynamically based on:
•Current location
•Traffic conditions (using external map APIs)
•Route distance and speed patterns


7. Delivery History


•Allow customers and merchants to view past deliveries and timelines.
•Support analytics like delivery time, delays, and success rate.


⚙️ Non-Functional Requirements


| Requirement | Description |

| ----------------- | -------------------------------------------------------------------- |

| **Scalability** | Handle millions of concurrent deliveries and GPS updates. |

| **Low Latency** | Real-time updates (< 2 seconds lag). |

| **Availability** | 99.9% uptime for tracking services. |

| **Reliability** | Ensure delivery events are never lost. |

| **Security** | Authenticate delivery partners and customers; encrypt location data. |

| **Extensibility** | Support multiple logistics partners and geographies. |

| **Durability** | Persist location & status history for audits. |


🧠 Key Components to Design


1. Delivery Management Service

•Creates, updates, and tracks deliveries.
•Stores delivery metadata, status, timestamps, and assigned driver.

2. Location Service

•Handles ingestion of GPS data from delivery apps.
•Updates live location in cache and persists periodically in database.
•Supports APIs for fetching current location and delivery route.

3. Notification Service

•Sends alerts for delivery events (via push, SMS, email).
•Uses message queues (Kafka/SNS/SQS) for asynchronous notifications.

4. ETA Service

•Computes ETA using route APIs (Google Maps, Mapbox, etc.).
•Continuously updates ETA as driver moves.

5. Tracking API

•Exposes REST/WebSocket endpoints for customers to view live delivery updates.
•Supports real-time streaming via WebSockets or MQTT.

6. Analytics Service

•Aggregates delivery times, partner performance, and delay patterns.
•Feeds data into dashboards for operations.

7. Storage

•SQL DB: Deliveries, drivers, and order metadata.
•NoSQL / Time-Series DB: Location logs and status updates.
•Redis: Real-time location cache.


🧱 High-Level Architecture

1 2 ┌───────────────────────┐ 3 │ Customer / Merchant │ 4 │ Web / Mobile │ 5 └──────────┬────────────┘ 6 │ 7 ┌─────┴───────────┐ 8 │ API Gateway │ 9 └─────┬───────────┘ 10 │ 11 ┌─────────────────┼────────────────────────────┐ 12 │ │ │ 13 │ Delivery Mgmt Service Tracking Service 14 │ │ │ 15 │ Notification Service ETA & Route Service 16 │ │ │ 17 │ Analytics Service User/Driver Service 18 └─────────────────┼────────────────────────────┘ 19 │ 20 ┌────────┴─────────┐ 21 │ Message Queue │ 22 │ (Kafka/SQS) │ 23 └────────┬─────────┘ 24 │ 25 ┌────────┴─────────┐ 26 │ Databases Layer │ 27 │ SQL + Redis + TSDB│ 28 └───────────────────┘


💾 Data Model Example


Delivery Table


| Field | Type | Description |

| ----------- | --------- | ----------------------------------------- |

| delivery_id | UUID | Unique delivery ID |

| order_id | UUID | Associated order |

| partner_id | UUID | Assigned delivery partner |

| pickup_lat | Float | Pickup location latitude |

| pickup_lng | Float | Pickup location longitude |

| drop_lat | Float | Drop-off location latitude |

| drop_lng | Float | Drop-off location longitude |

| status | Enum | CREATED, IN_TRANSIT, DELIVERED, CANCELLED |

| eta | Timestamp | Estimated arrival time |

| created_at | Timestamp | Creation time |


Location Table


| Field | Type | Description |

| ----------- | --------- | ------------------- |

| partner_id | UUID | Driver reference |

| delivery_id | UUID | Associated delivery |

| latitude | Float | Current latitude |

| longitude | Float | Current longitude |

| timestamp | Timestamp | Time of update |


⚡ Scalability Considerations


•Use Kafka or Kinesis to ingest millions of GPS updates.
•Cache live location in Redis (GeoSpatial Indexes) for quick lookup.
•Periodically persist to a time-series database (like InfluxDB or Cassandra).
•Use WebSockets or gRPC streaming for real-time location updates to clients.
•Apply rate limiting and batching on location updates to reduce load.


🔐 Security Considerations


•JWT-based authentication for delivery partners and customers.
•HTTPS and TLS encryption for all APIs.
•Mask sensitive GPS data when not needed (e.g., for merchants).


⚠️ Failure Handling


| Failure | Mitigation |

| ------------------------ | --------------------------------------------- |

| Partner loses connection | Cache last known location; resume sync later |

| GPS data loss | Use last known location + ETA prediction |

| Queue delay | Retry via message queue and idempotent writes |

| System overload | Autoscale ingestion and tracking services |


✅ Expected Outcome

A real-time, scalable, and reliable Delivery Tracking System that can:

•Handle thousands of concurrent deliveries
•Provide <2s update latency
•Offer visibility and control to all stakeholders (customers, merchants, drivers)


Solution

Comments