🇮🇳
🇮🇳
Republic Day Special Offer!Get 20% OFF on all courses
Enroll Now
P
Prakalpana
📚Learn
Code Your Future
System Design⏱️ 24 min read📅 Jan 2

HLD: Design Uber - Ride Sharing at Scale

KR
Karthik ReddyPrincipal Engineer at Uber
📑 Contents (11 sections)

📌Problem Statement

Design a ride-sharing platform like Uber or Lyft.

📌Requirements

Functional

  • Rider requests ride with pickup/dropoff
  • Match with nearby drivers
  • Real-time tracking
  • Fare calculation with surge
  • Rating system
  • Payment processing
  • Non-Functional

  • Match in < 30 seconds
  • Location updates every 3 seconds
  • 99.99% availability
  • Support 100K concurrent rides
  • 📌High-Level Architecture

    System Architecture
    Live
    Rider App
    Driver App
    Load Balancer
    API Gateway
    Kong
    Ride Service
    Location Service
    Matching Service
    Pricing Service
    Kafka
    Events
    WebSocket Gateway
    Real-time
    Redis
    Driver Locations
    PostgreSQL
    Rides
    QuadTree
    Geo Index
    Payment Service
    Notification Service
    ⚡ High Availability🔄 Auto-scaling🛡️ Fault Tolerant

    Location Service

    public class LocationService {
    private QuadTree driverLocations;
    private RedisTemplate<String, DriverLocation> redis;
    public void updateDriverLocation(String driverId, double lat, double lng) {
    DriverLocation location = new DriverLocation(driverId, lat, lng);
    redis.opsForValue().set("driver:" + driverId, location, Duration.ofSeconds(30));
    driverLocations.update(driverId, lat, lng);
    kafkaTemplate.send("location-updates", location);
    }
    public List<String> findNearbyDrivers(double lat, double lng, double radiusKm, int limit) {
    return driverLocations.findNearby(lat, lng, radiusKm, limit);
    }
    }

    Matching Service

    public class MatchingService {
    public RideMatch matchRide(RideRequest request) {
    List<String> nearbyDrivers = locationService.findNearbyDrivers(
    request.getPickupLat(),
    request.getPickupLng(),
    5.0,
    10
    );
    List<DriverScore> scores = new ArrayList<>();
    for (String driverId : nearbyDrivers) {
    Driver driver = driverService.getDriver(driverId);
    if (!driver.isAvailable()) continue;
    double eta = etaService.calculate(driver.getLocation(), request.getPickup());
    double rating = driver.getRating();
    double score = calculateScore(eta, rating, driver.getAcceptanceRate());
    scores.add(new DriverScore(driverId, score, eta));
    }
    scores.sort(Comparator.comparing(DriverScore::getScore).reversed());
    for (DriverScore ds : scores) {
    boolean accepted = notifyDriver(ds.getDriverId(), request, 15);
    if (accepted) {
    return new RideMatch(request.getRideId(), ds.getDriverId(), ds.getEta());
    }
    }
    throw new NoDriverAvailableException();
    }
    }

    Surge Pricing

    public class SurgePricingService {
    public double getSurgeMultiplier(double lat, double lng) {
    String gridCell = getGridCell(lat, lng);
    int activeRiders = getActiveRideRequests(gridCell);
    int availableDrivers = getAvailableDrivers(gridCell);
    double ratio = (double) activeRiders / Math.max(1, availableDrivers);
    if (ratio > 2.0) return 2.0;
    if (ratio > 1.5) return 1.5;
    if (ratio > 1.2) return 1.25;
    return 1.0;
    }
    }

    ETA Service

    public class ETAService {
    public int calculateETA(Location from, Location to) {
    double distance = haversineDistance(from, to);
    TrafficData traffic = trafficService.getTraffic(from, to);
    double baseSpeed = 30;
    double adjustedSpeed = baseSpeed * traffic.getSpeedFactor();
    int etaMinutes = (int) Math.ceil((distance / adjustedSpeed) * 60);
    return etaMinutes;
    }
    }

    📌Database Design

    CREATE TABLE rides (
    ride_id UUID PRIMARY KEY,
    rider_id UUID,
    driver_id UUID,
    pickup_lat DECIMAL(10, 8),
    pickup_lng DECIMAL(11, 8),
    dropoff_lat DECIMAL(10, 8),
    dropoff_lng DECIMAL(11, 8),
    status VARCHAR(20),
    fare DECIMAL(10, 2),
    surge_multiplier DECIMAL(3, 2),
    started_at TIMESTAMP,
    completed_at TIMESTAMP
    );
    CREATE TABLE driver_locations (
    driver_id UUID PRIMARY KEY,
    lat DECIMAL(10, 8),
    lng DECIMAL(11, 8),
    updated_at TIMESTAMP
    );

    📌Key Components

  • 1QuadTree: Efficient spatial indexing for nearby search
  • 2Redis GEO: Fast geospatial queries
  • 3Kafka: Real-time location streaming
  • 4WebSocket: Live tracking updates
  • Asked at Uber, Lyft, Ola, and Grab interviews.

    KR

    Written by

    Karthik Reddy

    Principal Engineer at Uber

    🚀 Master System Design

    Join 500+ developers

    Explore Courses →
    Chat on WhatsApp