Solution and explanation
What is different from food delivery
Delivery in 10–15 minutes is possible only because items come from small dark stores (micro-warehouses) within 2–3 km, and the hard problem is real-time inventory: the app must never sell an item that is not on the shelf of the store that will deliver it.
Functional requirements
- Map the customer's location to the serving dark store; show only that store's in-stock items.
- Cart, checkout, payment; picking and packing in the store; rider assignment; live tracking.
- Inventory: receiving stock, cycle counts, replenishment from mother warehouses, expiry tracking.
Non-functional requirements
- End-to-end 10 minutes: ~2 min picking/packing, ~1 min assignment, ~5–7 min riding.
- Inventory accuracy: overselling causes cancellations; underselling loses revenue.
- Peaks: evenings, weekends, festivals, rain (demand jumps and riders are fewer).
Architecture
App ─▶ Serviceability Service (location ─▶ dark store polygon)
─▶ Catalog Service (store-specific assortment + price) ─▶ cache
─▶ Inventory Service (per store, per SKU: on_hand, reserved) ─▶ Redis + DB
─▶ Order Service ─▶ Payments
─▶ Store Ops (picker app: optimised pick path, packing)
─▶ Rider Dispatch (riders waiting at the store, FIFO / batching)
Events ─▶ Kafka ─▶ Replenishment & demand forecasting · Analytics
Inventory reservation (the core)
- Available to sell = on_hand − reserved − safety buffer.
- When the customer places the order, reserve each item atomically:
UPDATE inventory SET reserved = reserved + qty WHERE store_id = ? AND sku = ? AND on_hand - reserved >= qty (or a Redis Lua script). If any item fails, release the others and tell the user. - Reservation has a timeout (e.g. 10 min) in case payment is abandoned.
- When picked and dispatched, decrease on_hand and reserved together.
Fast fulfilment
- Picking: SKUs stored by velocity (fast movers near the packing desk); the picker app gives an optimal route.
- Riders wait at the store, so assignment is a queue rather than a city-wide search; orders going the same direction can be batched.
- Demand forecasting per store per hour decides replenishment from the mother warehouse (often several times a day).
Interview follow-ups
- *How do you show stock instantly to lakhs of users?* Cache availability per store/SKU, updated by inventory events; final truth is checked at order time by the reservation.
- *What if the shelf count is wrong?* Picker marks the item missing → partial refund or substitute; frequent cycle counts correct the count.
- *How is a location mapped to a store?* Point-in-polygon lookup on store service areas, with fallback to another store when one is overloaded or closed.