Real-time backend

RideWave

I built a ride-matching service that finds drivers near a pickup with Redis and sends each one an offer over Socket.IO.

By Krrish Kumar

Match a passenger to nearby drivers the moment a ride is requested, push the offer to those drivers instantly, and withdraw it from everyone else once one of them accepts, without polling the database.

Request path

  1. PassengerPOST /booking · JWT
  2. Express APIfare · booking saved as pending
  3. Redis GEOGEORADIUS → nearby drivers
  4. Socket.IOnewBooking to specific sockets
  5. Driver acceptsoffer withdrawn from the rest

One Express process hosts REST and Socket.IO. Redis answers the two hot-path questions: who is near, and which socket reaches them.

Why it's built this way

  • Redis GEO for proximity

    Nearest-driver lookup is a radius query on a geo set, so match cost tracks the number of nearby drivers rather than the size of the drivers collection.

  • Addressable sockets, not broadcasts

    Mapping driver ID to socket ID in Redis means an offer goes to specific drivers. No fan-out to every connected client, and the mapping survives across handlers.

  • Monolith on purpose

    One process keeps REST and socket state together. At this scale a service split adds hops without removing a bottleneck.

Outcome

A working real-time matching loop: request, proximity search, targeted offer, confirm, and withdrawal from the drivers who lost the race.

Known limit

Confirmation is a read-then-write. Making it an atomic findOneAndUpdate on status: 'pending' closes the window where two drivers could accept the same ride.

Full request path, step by step
  1. 01A driver connects over Socket.IO and registers; Redis stores driver:{id} → socketId. Location updates land in a Redis geo set via GEOADD.
  2. 02A passenger posts a booking. The API computes fare from haversine distance and saves the booking as pending in MongoDB.
  3. 03GEORADIUS returns drivers within the search radius. The API resolves each driver's socket from Redis and emits newBooking to it.
  4. 04The offered driver IDs are saved as a Redis set per booking, so the offer can be addressed to exactly those drivers later.
  5. 05When a driver confirms, the booking flips to confirmed; that driver gets rideConfirmed and every other notified driver gets removeBooking.
Node.jsExpressSocket.IORedis GEOMongoDBMongooseJWT
Krrish Kumar