MSD-101 · Deep dives: hard problems, solved
Fares and surge pricing
⏱ 12 min 🎮 Hands-on ☁ Azure ✓ Completed
⚡ 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
Each box is one Azure service. Tap a box (or Tab to it) to see its job and why it is there.
Create each resource. The defaults are a good design; change a setting and read why it matters. Some choices decide what breaks later.
Home › Create a resource › Azure Stream Analytics
Create Stream Analytics job: zone-counters
ⓘ Short windows make surge react quickly; long ones react after the crowd has gone.
Deployment in progress…
Microsoft.StreamAnalytics · resource group rg-surge-pricing
Home › Create a resource › Azure App Configuration
Create App Configuration: pricing rules
ⓘ A cap protects riders and your reputation; some cities require one by law.
Deployment in progress…
Microsoft.AppConfiguration · resource group rg-surge-pricing
Home › Create a resource › Azure Container Apps
Create Container App: pricing-service
ⓘ The rider pays the price they saw.
ⓘ Pricing must work without the ML model.
Deployment in progress…
Microsoft.ContainerApps · resource group rg-surge-pricing
Everything is deployed. Your architecture is lit up below. Go to Run to send a request through it.
Request: Priya asks for a fare: Stadium → Home
⚠️ Stream Analytics jobs and Azure Machine Learning endpoints bill while running. Stop them after testing.
- Send fake app-open and driver events into an Event Hub.
- Create a Stream Analytics job with a 1-minute tumbling window that writes counts per zone.
- Store the cap in App Configuration and read it from the pricing service.
- Save quotes with a TTL in Cosmos DB and book with the quote id.
- Delete the resource group when done.
* Illustrative prices to compare designs, not a quote.
What breaks, and the fix
| Failure | User sees | Why | Fix |
|---|---|---|---|
| 🎤 Concert ends: 5,000 requests in one zone | Shocking prices | No cap | Cap per city in App Configuration |
| 🔄 Price changes while Priya books | Charged more than shown | Price recalculated at booking | Book with a saved quote id, valid 2 minutes |
| 🔌 Forecast endpoint down | Cannot book at all | Hard dependency on the model | Rule-based fallback from live counters |
| 🕙 Crowd leaves after 10 minutes | High prices with no crowd | Long aggregation windows | 1-minute tumbling windows |
🎓 Education-only simulation. Nothing is created in Azure and no subscription is needed.
Stream vs command
Live data can lose a point. Commands must happen exactly once. Give them different roads.
GPS points ride the WebSocket every 4 s. One lost? The next one replaces it.
Idempotency key
Send the same ID on every retry. The server returns the first result instead of doing it again.
Book with key k7. The trip is saved… but the reply is lost in the lift.
Outbox + backoff
Save the command on the phone first, then send it. Wait longer after every failure.
The tap is saved to the local outbox. Even if the app dies now, it is not lost.
Geo index + expiry
Keep only the latest position per driver in memory, and let it expire after 30 s.
Every ping overwrites the driver’s last position and resets a 30 s timer.
Pub/sub fan-out
Publish one event to a group. Everyone in the group gets it, however many they are.
The trip service publishes once: “car moved”.
Push to wake, then re-sync
A push only says “something changed”. The app then asks the server for the truth.
The app is in the background, so its socket is closed. The server sends a push.
- 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.
- The design ideaCount fast, cap always, honour the quote.
- 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.
| Part | Azure | Why |
|---|---|---|
| Zone counts | Stream Analytics (1-min windows) | Fresh demand and supply |
| Counters store | Azure Managed Redis | Read on every quote |
| Rules and caps | Azure App Configuration | Change without a release |
| Forecast | Azure Machine Learning endpoint | Optional, with a fallback |
| Quotes | Cosmos DB with TTL | Price shown = price paid |
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.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
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
- Stream Analytics windowing (Microsoft Learn)
- App Configuration overview (Microsoft Learn)
- Cosmos DB time to live (Microsoft Learn)