You order biryani on Swiggy. Within seconds, a delivery partner is assigned.

At that moment, thousands of riders are moving across your city. Some are idle, some are mid-delivery, some are heading home. So how does the system pick the right one, and do it in seconds?

In this post, I'll break down how a delivery-assignment system like this works, step by step, with code you can actually run.

A note on accuracy: Some details here are confirmed by Swiggy's own engineers and published research (sources are linked at the end). Others, like specific tools such as Redis or H3, are common industry approaches and are not confirmed as Swiggy's exact stack. I'll label which is which.

1. The problem

When an order is placed, the system needs to:

  • Assign a delivery partner within seconds
  • Pick from thousands of moving riders
  • Make sure the food doesn't wait for the rider
  • Make sure the rider doesn't wait for the food
  • Keep earnings fair across riders
  • Survive lunch and dinner peaks, when demand spikes

The naive answer, "find the nearest rider," fails most of these. Let's see why.

2. Knowing where every rider is

Confirmed: Swiggy's engineering leadership has said that delivery partners' locations are pinged every few seconds in real time.

The flow looks roughly like this:

Rider App --(GPS ping every few sec)--> Location Service --> Stream (e.g. Kafka) --> Geo Store 

Some quick illustrative math:

  • 100,000 active riders
  • One ping every 5 seconds
  • = 20,000 location writes per second

That's why live locations usually live in a fast in-memory store rather than a traditional database. The database keeps the durable record (rider profile, delivery history). The in-memory store keeps only the current position, which is overwritten every few seconds.

3. Dividing the city into zones

Scanning every rider in the city for every order would be far too slow. So the city is split into smaller areas.

Confirmed: Swiggy's tech blog has published an article on logistic zones used for assignment.

Common technique (not confirmed for Swiggy): the two most popular ways to divide a map into cells are:

Technique Cell shape Known for
Geohash Rectangles Built into Redis, Elasticsearch, and others
H3 Hexagons Open-sourced by Uber

Hexagons have a useful property: all six neighbors are the same distance from the center. That makes "search around me" more uniform than with rectangles, where diagonal neighbors are farther away than side neighbors.

Here's H3 in Python:

import h3 # Restaurant location (Koramangala, Bengaluru) lat, lng = 12.9352, 77.6245 # Convert the location to an H3 cell (resolution 9 is roughly a city block) restaurant_cell = h3.latlng_to_cell(lat, lng, 9) # Get this cell plus every cell within 2 rings around it nearby_cells = h3.grid_disk(restaurant_cell, 2) print(f"Searching {len(nearby_cells)} cells instead of the whole city") # Searching 19 cells instead of the whole city 

If every rider is indexed by the cell they're currently in, finding candidates means looking up 19 cells instead of scanning the entire city.

4. Finding nearby riders fast

Common technique (not confirmed for Swiggy): Redis has geospatial commands built in. Under the hood, it stores locations as geohash-encoded scores in a sorted set, so radius searches are very fast.

Every location ping updates the rider's position:

# GEOADD key longitude latitude member GEOADD riders:live 77.6245 12.9352 rider:101 GEOADD riders:live 77.6301 12.9389 rider:102 GEOADD riders:live 77.6150 12.9280 rider:103 

When an order arrives, search around the restaurant:

GEOSEARCH riders:live FROMLONLAT 77.6245 12.9352 BYRADIUS 3 km ASC COUNT 20 WITHDIST 

This returns up to 20 riders within 3 km, sorted by distance, in milliseconds.

In Python:

import redis r = redis.Redis() def update_location(rider_id, lng, lat): r.geoadd("riders:live", (lng, lat, f"rider:{rider_id}")) def find_candidates(lng, lat, radius_km=3, limit=20): return r.geosearch( "riders:live", longitude=lng, latitude=lat, radius=radius_km, unit="km", sort="ASC", count=limit, withdist=True, ) 

One catch: this only gives you straight-line distance. A rider 1 km away across a flyover with no exit may take longer than one 2 km away on a straight road. That's why this step only produces candidates. The real decision happens next.

5. Why the nearest rider is often the wrong rider

Confirmed: Swiggy's engineering leadership has said the hard problem isn't finding a partner. It's assigning the most optimal one in a way that's cost-effective and still gives customers a good experience.

Confirmed: Swiggy has also said it balances delivery time, cost, and earnings across its rider fleet, and that AI/ML models are used in driver assignment.

Here's a simplified scoring function that shows the idea. This is my illustration, not Swiggy's actual model:

from dataclasses import dataclass @dataclass class Rider: id: str eta_to_restaurant_min: float # from a routing engine, not straight-line distance current_orders: int # orders already being carried same_direction: bool # existing order is heading the same way earnings_today: float def score(rider: Rider, food_ready_in_min: float, avg_earnings: float) -> float: # 1. Timing: how far off is the rider's arrival from when the food is ready? timing_gap = abs(rider.eta_to_restaurant_min - food_ready_in_min) # Arriving late is worse than arriving early (the food goes cold) if rider.eta_to_restaurant_min > food_ready_in_min: timing_penalty = timing_gap * 2.0 else: timing_penalty = timing_gap * 1.0 # 2. Batching: an already-busy rider is fine only if heading the same way if rider.current_orders > 0 and not rider.same_direction: batching_penalty = 15 else: batching_penalty = 0 # 3. Fairness: slightly prefer riders who have earned less today fairness_bonus = (avg_earnings - rider.earnings_today) / 100 # Lower score = better match return timing_penalty + batching_penalty - fairness_bonus def pick_best(riders, food_ready_in_min, avg_earnings): return min(riders, key=lambda r: score(r, food_ready_in_min, avg_earnings)) 

The weights here are made up. In a real system, values like these are learned from historical data rather than hand-tuned.

6. The key idea: Just-In-Time assignment

This is the most interesting part of the whole system.

Confirmed: Swiggy uses an approach called Just-In-Time (JIT) assignment for most orders. Research on food delivery describes the goal of JIT as minimizing the time food sits waiting.

Think of delivery time like this (a simplified model):

Delivery time = max(Assignment delay + Rider travel to restaurant, Food prep time) + Restaurant to customer travel 

The rider's journey to the restaurant and the food preparation happen in parallel. So:

  • If the rider arrives too early, they stand around waiting. That's wasted rider time and lost earnings.
  • If the rider arrives too late, the food sits and gets cold.

An example:

Rider A Rider B
Distance to restaurant 500 m 2 km
Time to reach restaurant 3 min 11 min
Food ready in 12 min 12 min
Result Waits 9 min Arrives right on time

Rider A is closer, but Rider B is the better choice. Rider A stays free for an order that's ready now.

So the goal isn't to find the closest rider. It's to find the rider who reaches the restaurant right when the food is ready.

7. Batching: one rider, multiple orders

Confirmed: Published research has evaluated batching and matching strategies using real Swiggy datasets.

If two orders come from nearby restaurants and are going to nearby customers, one rider can carry both. That means:

  • Fewer riders needed during peaks
  • Lower cost per delivery
  • Higher earnings per trip for the rider

The trade-off is that the second customer's food may take a little longer. Batching only works if the extra delay stays within acceptable limits.

8. Things that go wrong (and how to handle them)

A design that only works on the happy path will fail at 8 PM on a Saturday. Here's what a real system has to handle:

The rider declines or doesn't respond
Offer the order with a timeout (say, 15–30 seconds). If there's no response, move to the next-best candidate automatically.

No riders nearby
Widen the search radius step by step. If there's still nobody, show the customer a longer ETA instead of failing the order.

Peak load
Queue assignment requests instead of processing them all at once. Batch more aggressively when demand is high.

Stale or wrong GPS data
If a rider hasn't pinged in the last 30–60 seconds, treat them as unavailable. GPS in dense areas can also jump around, so smoothing location data helps.

Two orders grabbing the same rider at the same time
Use a lock or an atomic operation when assigning, so one rider can't be double-booked by two parallel requests.

9. Putting it all together

 +-------------------+ Rider pings ---> | Location Service | ---> Geo Store (in-memory) +-------------------+ | | New order -----> +-------------------+ | | Assignment Service| <------------+ 1. Find nearby candidates +-------------------+ | 2. Get ETAs from routing engine | 3. Score candidates (ML model) | 4. Offer to best rider (with timeout) v +-------------------+ | Notification | ---> Rider app: "New order" +-------------------+ 

Key takeaways

  1. Live locations come in every few seconds and live in a fast in-memory store.
  2. Spatial indexing (zones, geohash, H3) turns "search the whole city" into "search a few cells."
  3. Straight-line distance is only for finding candidates. Real decisions use actual travel time.
  4. The nearest rider is often the wrong rider. Timing, batching, and fairness all matter.
  5. Just-In-Time assignment aims for the rider to arrive exactly when the food is ready.
  6. Failure handling (declines, timeouts, peaks) is what separates a demo from a production system.

Your turn

If you were designing this system, what would you optimize first: faster delivery or fair rider earnings? Let me know in the comments.

Sources