ArchitectAI resilience reviews are here. See what's new
Back to all articles
Low Level Design Sep 18, 2026 14 min read

Low Level Design: Ride Sharing Engine with Strategy Pattern & Lifecycle State Machine

Design a clean, extensible ride-sharing backend in 90 minutes. State machine for trip transitions, Strategy pattern for driver matching, and surge pricing calculators.

LLDDesign PatternsState PatternStrategy PatternRide Hailing
HFHLDForge Architecture TeamLead Machine Coding Specialist

1. Entities & Domain Modeling

The ride-sharing domain revolves around five core entities: `Rider`, `Driver`, `Trip`, `Location`, and `PricingStrategy`. A trip passes through strict sequential states: REQUESTED -> MATCHED -> DRIVER_ARRIVED -> IN_PROGRESS -> COMPLETED (or CANCELLED).

2. Matching Strategies: Pluggable Algorithms

We define a `DriverMatchingStrategy` interface with concrete implementations: `NearestDriverStrategy` (Euclidean distance), `HighestRatedDriverStrategy`, and `PoolMatchingStrategy`. This allows runtime swapping based on user tier or service level.

3. Concurrency & Race-Free Driver Assignment

To prevent two concurrent trips from matching the same driver, the `Driver` entity maintains an atomic availability status. Matching uses a compare-and-swap operation: a driver is offered a trip with an atomic status check (`driver.compareAndSetAvailable(true, false)`).

typescript
enum TripStatus { REQUESTED, MATCHED, IN_PROGRESS, COMPLETED, CANCELLED }

class Trip {
  public status: TripStatus = TripStatus.REQUESTED;
  constructor(
    public id: string,
    public riderId: string,
    public pickup: [number, number],
    public dropoff: [number, number]
  ) {}

  public completeTrip() {
    if (this.status !== TripStatus.IN_PROGRESS) throw new Error('Invalid state transition');
    this.status = TripStatus.COMPLETED;
  }
}
Interactive Modeling ArchitectAI

Model this architecture on the HLDForge Canvas

Turn static box diagrams into living, collaborative systems. Drag and drop microservices, Kafka clusters, and cache tiers. Run automated SPOF detection and STRIDE threat modeling in real time.

HF

Written by HLDForge Architecture Team

Lead Machine Coding Specialist

The HLDForge Architecture Team publishes practical engineering guides, real-world case studies, and high-concurrency blueprints to help engineers build scalable and fault-tolerant software.

Free forever plan

Ready to master distributed system design?

HLDForge gives you the tools, templates, and AI reviews to design systems like a Staff Engineer. Get started free.

SOC 2 Type II · ISO 27001 aligned · SSO on every paid plan