skip to content

--:-- IST

Medurun Real-time ambulance dispatch for patients, drivers and agencies

A patient books an ambulance, the backend finds the closest free one by live driving time, offers the ride to a few drivers and lets exactly one of them take it. I built the dispatch engine, the rider and driver apps and the dashboards.

tl;dr

problem
In an emergency the nearest ambulance on a map is often not the fastest one by road, and two drivers tapping Accept at the same moment must never both get the trip. Location updates also arrive every second from every online driver.
built
A NestJS dispatch engine that shortlists drivers with a Redis geo search, ranks them by traffic-aware driving time, offers the ride to the top few and settles Accept with one atomic Lua script. Around it sit React Native rider and driver apps, Next.js dashboards for agencies and the platform team, and a public website.
outcome
The full booking loop works end to end: request, ranked offers, single claim, live tracking in a shared trip room, PIN check at pickup, completion and payment. The product is pre-launch.
  • 6 apps in one monorepo: API, rider, driver, agency, admin and website
  • 30 NestJS feature modules in the backend
  • 8 trip states, from searching to completed or cancelled
  • 346 of the repo's 362 commits are mine

architecture

select a box to see what it does.

Rider app. React Native (Expo) app where a patient picks an ambulance type, sets pickup and drop, and watches the assigned ambulance move on a map.

key decisions

  1. 01

    Rank by driving time, not distance

    A driver 2 km away across a river can take longer than one 5 km away on a main road. Straight-line distance picks the wrong ambulance.

    Geo search is only a cheap shortlist, capped at 20 drivers. The Google Routes matrix ranks those by traffic-aware ETA, drops anyone over the max ETA, and falls back to a distance estimate if a row has no ETA.

  2. 02

    Greedy per trip, not batched matching

    Ride-hailing systems often batch riders and drivers to optimise the whole network. Ambulances are scarce and every request is urgent.

    Each trip gets the fastest available ambulance for that patient. If too few drivers are found, the search widens to a larger radius once before giving up.

  3. 03

    One Lua script decides who wins

    A ride is offered to several drivers at once, so two can accept in the same millisecond. Read then write from Node leaves a gap where both pass the check.

    Accept runs a short Lua script that checks trip status and the driver's active-trip key and writes all the bindings in one go. It runs inside a Postgres transaction that also locks the driver's document status, and the trip row is only updated if the claim wins.

  4. 04

    Buffer GPS before it hits Redis

    Every online driver sends a location every second or so, and most of those writes would be overwritten before anyone searches.

    The gateway keeps only the latest point per driver in memory and flushes changed ones every 750 ms. Drivers on a trip skip the geo index and stream to their trip room.

  5. 05

    Dispatch knobs live in platform settings

    Search radius, max ETA and how many drivers get an offer depend on the city and fleet size, and will change after launch.

    The values ship as defaults in the shared package and are read from platform settings at request time, so the ops team can tune them from the admin dashboard without a deploy.

the details

Context

Medurun is an ambulance booking platform I work on as a client project at Heizen. A patient opens the rider app, picks an ambulance type and a pickup point, and the system finds a nearby ambulance, offers it the ride and tracks it to the patient and then to the hospital. Agencies run fleets of ambulances and drivers, and some drivers work on their own.

I started the repo in June 2026 and have written most of it: 346 of its 362 commits. I designed the dispatch engine and built across the stack, from the NestJS backend to the two React Native apps and the Next.js dashboards. The product hasn’t launched yet. The public website is live at medurun.com.

The code is a Bun and Turborepo monorepo with six apps and five shared packages. The apps are the NestJS API, the rider app, the driver app, the agency dashboard, the platform admin dashboard and the marketing website. The packages hold the Prisma schema, shared types and constants, CASL permission rules, UI components and TypeScript config. Postgres holds the durable records. I put everything that changes by the second in Redis: the geo index of online drivers, trip status, who was offered what and who is busy.

What’s in it

Going online and streaming location

A driver connects to the driver Socket.IO namespace when they go on duty, not when they open the app. I kept profile, history and earnings on plain REST, since none of that needs a socket.

Location updates go one of two ways. If the driver has no active trip, the point goes into an in-memory buffer that keeps only the latest location per driver. Every 750 ms the buffer writes the changed drivers to a Redis geo set in a single pipeline. If the write fails, those drivers are marked dirty again and go out on the next flush. If the driver is on a trip, the point skips the geo index and goes straight to the trip room, and the last point is saved in Redis so a rider who reconnects sees the ambulance right away.

Mobile networks drop all the time, and I didn’t want a dropped socket to cost anyone a ride. When the driver app reconnects it registers again with its current state. The server puts it back in its trip room if it has one and replays any ride offers that are still open for it.

Finding the nearest ambulance

A ride request runs three steps. I wanted the cheap filters first and the paid API call last.

  • GEOSEARCH on the online-drivers set around the pickup, sorted by distance.
  • A Postgres filter that keeps drivers who are on duty, approved, of the requested ambulance type, in an active city and not blocked for expired documents. If fewer than the configured minimum are left, the search runs again with a wider radius.
  • A Google Routes matrix call for at most 20 candidates, then a sort by ETA with anyone over the max ETA removed.

The ranked list is saved on the trip and the top few drivers get an offer over the socket. Nobody waits on a driver who said no. When a driver declines, their slot is refilled from the next drivers on the list who haven’t declined and aren’t busy.

Accepting a ride

Several drivers see the same offer, so Accept is where things can go wrong. This was the part I spent the most time on. The check for “is this trip still open and is this driver free” and the writes that assign it have to happen as one step.

I made that step a Lua script of about fifteen lines. Redis doesn’t interleave other commands while a script runs, so two Accepts for the same trip can’t both pass. I looked at Redlock too, but it only gives you a lock, and you’d still have to update the trip after taking it. On one Redis, the script is simpler and does the whole job. The script reads the trip status, rejects the claim if it is no longer on offer or if the driver already has a trip, and otherwise writes the new status, the driver, the ambulance and the active-trip keys for both driver and patient. The loser gets a “ride already taken” event and the offer disappears from the other drivers’ screens.

The claim runs inside a Postgres transaction. The transaction first checks the driver and ambulance aren’t blocked for documents, holding row locks so an admin block can’t land halfway through. The trip row is updated only after the script returns success. If Redis was flushed, the trip’s claimable status is re-seeded from Postgres with SET NX before the claim.

On the trip

After Accept, the driver and the rider join one Socket.IO room per trip. A small bridge service emits to that room on both namespaces, so either side can push status changes and the rider sees the driver’s location move. Approach push notifications fire off the same stream. It’s one source of truth for where the ambulance is.

At pickup the driver asks the patient for a 4-digit PIN that was generated at Accept. The trip only moves to in progress when it matches, and wrong attempts are capped. Then the driver completes the trip and payment is collected. The PIN means a driver can’t mark the patient as picked up until they’re actually standing with them.

Dashboards, payments and the rest

The agency dashboard covers ambulances, drivers, documents, live rides on a map, ride history and revenue. The admin dashboard covers agency and driver approval, document expiry with reminders and blocks, pricing with surge hours, payouts, analytics and dispatch settings. Access is enforced with CASL, with separate rule sets for operations, finance, marketing and super admin.

Payments go through Cashfree, and the webhook handler rejects anything with a bad signature. Emails go through a BullMQ queue with three attempts and a fixed backoff, so a slow mail server never holds up a request.

Constraints

  • Urgency over network efficiency. Every trip gets the best ambulance for that patient, not the best global assignment.
  • Exactly one driver per trip, even when several accept at once.
  • GPS arrives far more often than anyone searches, so writes to Redis had to be bounded.
  • Mobile connections drop. Drivers and riders must recover their state on reconnect without losing an offer or a trip.
  • Paid map API calls had to be bounded, so the route matrix only ever sees a short list.
  • Dispatch settings had to be tunable without a deploy.

Results

  • The full loop works end to end: request, ranked offers, a single winning Accept, live tracking, PIN at pickup, completion and payment.
  • 6 apps and 5 shared packages in one monorepo, with 30 feature modules in the NestJS backend and 8 trip states (counted from the repo).
  • 346 of the repo’s 362 commits are mine, from June to October 2026 (git log).
  • The product is pre-launch, so there are no usage numbers yet. The public site is live at medurun.com.

that's the short version.