Lesson 10 of 14
❮Prev Next❯

MSD-101 · Deep dives: hard problems, solved

Fares and surge pricing

⏱ 12 min 🎮 Hands-on ☁ Azure

10of 14

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    ⚡ The 30-second answer

    Surge pricing compares demand and supply per zone in short time windows and multiplies the base fare, capped per city. The rider gets a quote with an id that is valid for 2 minutes and books with it, so the price shown is the price paid. A forecast model helps move drivers early, but pricing must keep working without it.

    • Zone counters from a stream
    • Caps as config
    • Quote ids lock the price
    • Fallback when ML fails

    Try it

    Play with the design before you read about it. Everything runs on this page.

    No Azure subscription needed. Read the requirement, see the architecture, build it in a mock portal, send a booking through it, then break it.

    ☁ Simulate on Azure: Fares and surge pricing

    A concert ends and 5,000 people open the app in one zone. Prices must rise enough to bring drivers in, never by a shocking amount, and the price a rider sees must be the price they pay.

    It must

    • Show a fare quote before booking
    • Raise prices in a zone when demand outgrows supply
    • Charge exactly the quoted price
    • Keep working if the forecast service is down

    With these numbers

    Quote time
    p95 < 200 ms
    Surge reacts in
    < 2 minutes
    Surge cap
    ≤ 2.0×
    Quote valid
    2 minutes

    🎓 Education-only simulation. Nothing is created in Azure and no subscription is needed.

    1. The problemA concert ends. In one minute 5,000 people open the app near the stadium. Within seconds a screenshot of a 6.8× fare is trending.
    2. The design ideaCount fast, cap always, honour the quote.
    3. In real lifeSeveral cities and countries cap ride-hailing surge by regulation, and app stores and consumer law expect the shown price to be honoured. Caps-as-config and quote ids are how teams meet both.

    Start with a question

    A concert ends. In one minute 5,000 people open the app near the stadium. Within seconds a screenshot of a 6.8× fare is trending.

    Surge is an algorithm, a product promise and sometimes a legal limit, all at once.

    Explain it like I'm 10

    When a lot of kids want ice cream and only a few ice-cream vans are nearby, the price goes up a little so more vans drive over. But there is a rule: it can never cost more than double.

    The technical explanation

    Count. App opens and driver pings flow into Event Hubs. An Azure Stream Analytics job counts open requests and free drivers per zone in 1-minute tumbling windows and writes the counts to Azure Managed Redis.

    Price. The pricing service (Container Apps) reads the zone counters, applies the city’s rules from Azure App Configuration (cap, base fare, flags), and optionally a demand forecast from an Azure Machine Learning endpoint.

    Promise. It saves the quote in Cosmos DB with a 2-minute time-to-live and returns a quote id. Booking with the quote id charges exactly that price.

    Degrade. If the forecast endpoint is slow or down, fall back to rules from the live counters. Caps live in config so operations can change them without an app release.

    PartAzureWhy
    Zone countsStream Analytics (1-min windows)Fresh demand and supply
    Counters storeAzure Managed RedisRead on every quote
    Rules and capsAzure App ConfigurationChange without a release
    ForecastAzure Machine Learning endpointOptional, with a fallback
    QuotesCosmos DB with TTLPrice shown = price paid

    ☁ Simulate on Azure🧩 Patterns

    Real-world scenario

    Several cities and countries cap ride-hailing surge by regulation, and app stores and consumer law expect the shown price to be honoured. Caps-as-config and quote ids are how teams meet both.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. Why book with a quote id?
    2. 2. Where should the surge cap live?
    3. 3. The forecast model is down. What should pricing do?
    4. 4. Why 1-minute windows instead of 15-minute ones?
    5. 5. Which service counts events per zone in time windows?

    Pass the quiz to complete this lesson.

    Next: Matching the nearest driver →

    Questions people ask

    How does surge pricing work in ride apps?

    Each city is split into zones. When open requests outgrow free drivers in a zone, a capped multiplier is applied to the base fare and shown before booking. The quote is honoured if the rider books in time.

    How do I make sure the rider pays the price shown?

    Give the rider a quote with an id, saved on the server for about 2 minutes. Booking with that quote id charges exactly the price shown, even if surge changes a second later.

    What happens to surge pricing when the forecast model is down?

    Pricing keeps working. The pricing service falls back to rule-based surge from the live zone counters, and the caps stay in Azure App Configuration, so operations can change them without an app release.

    Should surge be calculated on the phone?

    No. Prices are always calculated and saved on the server; the phone only shows the quote and books with its id.

    Interview questions

    How this topic comes up in system design interviews. Answer out loud first, then open the model answer.

    intermediate Design surge pricing for a ride app.

    Stream app opens and driver pings into Event Hubs, aggregate per zone in 1-minute windows with Stream Analytics into Redis, apply rules and caps from App Configuration in a pricing service, save quotes with TTL in Cosmos DB, book by quote id, fall back to rules when the forecast fails.

    advanced How do you stop surge from flickering zone to zone?

    Smooth multipliers over a few windows, use hysteresis (rise fast, fall slowly within limits), blend neighbouring zones, and cap the change per minute.

    Quick revision

    Remember

    • Zone counters from a stream
    • Caps as config
    • Quote ids lock the price
    • Fallback when ML fails
    • Show the multiplier and the reason (“busy area”) before booking. Surprise is what makes surge feel unfair.

    One line to remember

    “Count fast, cap always, honour the quote.”

    Official documentation