Live order tracking and rider management

6 modules · 7 lessons · 10 exercises · 17 available now

Week 5 overview

  1. Module 1 WebSocket hub
    • 5.1 Channels, select and cleanupChannels, Buffered channel, Closing a channel, select statement
    • 5.2 Managing WebSocket connectionsWebSocket, WebSocket hub, Slow consumer, WebSocket ping and heartbeat
    • 5.3 Per-user WebSocket hub
  2. Module 2 Redis event fan-out
    • 5.4 Redis pattern subscriptionsRedis pub/sub, Pub/sub fan-out across instances
    • 5.5 Per-user order event bridge
  3. Module 3 Rider management and assignment
    • 5.6 Redis GEO and SET NX PXRedis GEO, GEOSEARCH nearest query, Exclusive claim with SET NX PX
    • 5.7 Rider presence service
    • 5.8 Rider assignment worker
  4. Module 4 Live rider tracking
    • 5.9 Watching foreground locationForeground location permission, watchPositionAsync, MapView and Marker
    • 5.10 Reconnecting WebSocket hooksReconnecting WebSocket, Effect cleanup, Handler ref
    • 5.11 Order location stream
    • 5.12 Rider mode and customer tracking screens
    • 5.13 Hub broadcast and claim race tests
  5. Module 5 Live capacity planning
    • 5.14 Capacity estimationMemory per connection, File descriptor limit
  6. Module 6 Live order extensions
    • 5.15 Per-user Redis replay log (optional)
    • 5.16 Topic subscription registry (optional)
    • 5.17 Rider map geometry library (optional)

Weekly learning objectives

Week 4 gave you an app that polls for order status. This week, order events reach the phone over a WebSocket, riders report their location, the nearest rider gets an exclusive claim on a READY order, and the customer watches the rider move on a map.

  • WebSocket hub: You build a WebSocket hub that queues messages per user connection without letting a slow phone block senders.
  • Redis event fan-out: You build Redis event fan-out that carries each customer's order events from the worker to the API instance holding their WebSocket.
  • Rider management and assignment: You build rider state and assignment so the API can find nearby online riders and give one rider an exclusive claim on a READY order.
  • Live rider tracking: You build live rider tracking that streams rider positions to customers and keeps the app connected through a reconnecting WebSocket.
  • Live capacity planning: You build a capacity model for rider tracking and customer connections so you can identify the first system limit before load testing.
  • Live order extensions: You add replay, topic subscriptions and rider ETA so live order updates can recover after reconnects and customers can follow rider progress.

System architecture and changes

The worker still reads order events from Redpanda. A second consumer group publishes each event to its Redis user channel. Every API instance subscribes to those channels and forwards events to its local WebSocket connections. Redis also holds temporary rider positions, seen keys and order claims. MySQL remains the source of durable order data.

How to complete the coursework

Work through the modules in order: WebSocket hub, Redis event fan-out, rider management and assignment, live rider tracking, live capacity planning, then live order extensions. Start the services with make up. From backend/, run make test W=W5 to run every graded Week 5 test. The end-to-end tests need a running API and TEST_API_URL=http://localhost:8080. In app/, run npx tsc --noEmit and check the rider and customer screens on a phone and an emulator.

Modules

  1. Module 1: WebSocket hub

    3 items

    You build a WebSocket hub that queues messages per user connection without letting a slow phone block senders.

    • 5.1Channels, select and cleanupLesson · 48 min

      You coordinate goroutines with buffered channels and select, then close channels safely when a connection ends.

      Open lesson →
    • 5.2Managing WebSocket connectionsLesson · 30 min

      You trace how coder/websocket handles slow consumers and heartbeats before building a hub that manages each user's connection.

      Open lesson →
    • 5.3Per-user WebSocket hubExercise · 120 min

      You build a per-user WebSocket hub that authenticates /v1/ws connections and queues messages for connected users. A full send buffer drops messages without blocking Send.

      Open exercise →
  2. Module 2: Redis event fan-out

    2 items

    You build Redis event fan-out that carries each customer's order events from the worker to the API instance holding their WebSocket.

    • 5.4Redis pattern subscriptionsLesson · 18 min

      You route messages from user:* to the matching local hub connection so each API instance can receive events published by the worker.

      Open lesson →
    • 5.5Per-user order event bridgeExercise · 90 min

      You build a per-user order event bridge that publishes worker events to user:{id} and forwards them to the customer's local WebSocket. The customer socket must receive the matching ACCEPTED event within 15 seconds.

      Open exercise →
  3. Module 3: Rider management and assignment

    3 items

    You build rider state and assignment so the API can find nearby online riders and give one rider an exclusive claim on a READY order.

    • 5.6Redis GEO and SET NX PXLesson · 18 min

      You can store rider coordinates with Redis GEO, search by distance, and reserve an order with a short-lived claim before implementing rider assignment.

      Open lesson →
    • 5.7Rider presence serviceExercise · 72 min

      You build a Redis-backed rider presence service with online, offline and location operations; nearby searches return only active riders in closest-first order.

      Open exercise →
    • 5.8Rider assignment workerExercise · 132 min

      You build the rider assignment worker to offer READY orders to nearby riders and accept an order only for its current claim holder. A 30-second claim ensures that only one rider can win an order.

      Open exercise →
  4. Module 4: Live rider tracking

    5 items

    You build live rider tracking that streams rider positions to customers and keeps the app connected through a reconnecting WebSocket.

    • 5.9Watching foreground locationLesson · 36 min

      You can request foreground location permission and render watched coordinates as a map marker, providing the position data the rider screen needs to report.

      Open lesson →
    • 5.10Reconnecting WebSocket hooksLesson · 18 min

      You can reconnect a mobile WebSocket with backoff and clean up its handlers so the customer map keeps receiving updates after network interruptions.

      Open lesson →
    • 5.11Order location streamExercise · 48 min

      You build the order location stream, accepting rider coordinates through a per-rider 2000 ms Redis limit and publishing them on an order channel. The API forwards accepted payloads to the customer's WebSocket connection while the order is PICKED_UP.

      Open exercise →
    • 5.12Rider mode and customer tracking screensExercise · 150 min

      You build rider mode and the customer tracking screen, connecting rider offers and location updates to a live order map and status timeline.

      Open exercise →
    • 5.13Hub broadcast and claim race testsExercise · 72 min

      You build concurrency tests for the WebSocket hub and Redis rider claims, proving that broadcasts arrive in order and exactly one rider wins each order claim.

      Open exercise →
  5. Module 5: Live capacity planning

    1 items

    You build a capacity model for rider tracking and customer connections so you can identify the first system limit before load testing.

    • 5.14Capacity estimationLesson · 35 min

      Estimate connection, Redis and network load for a target rider fleet, then take those assumptions into the Week 6 load tests.

      Open lesson →
  6. Module 6: Live order extensions

    3 items

    You add replay, topic subscriptions and rider ETA so live order updates can recover after reconnects and customers can follow rider progress.

    • 5.15Per-user Redis replay logExercise · 45 min · Optional

      You build a Redis replay log that accepts user-scoped payloads and returns increasing IDs plus retained entries after a supplied ID. Concurrent appends must receive distinct IDs.

      Open exercise →
    • 5.16Topic subscription registryExercise · 40 min · Optional

      You build a Go topic registry that tracks user subscriptions and publishes messages through a send callback. Publish returns the number of accepted sends.

      Open exercise →
    • 5.17Rider map geometry libraryExercise · 40 min · Optional

      You build app/lib/eta.ts, a pure TypeScript library that takes geographic positions and travel-speed inputs and returns distances, ETA estimates, interpolated positions and compass bearings.

      Open exercise →

Completion checklist

What should be true at the end of the week.

  • From backend/, run make test W=W5 and confirm every graded Week 5 test passes.
  • With the API running, execute cd backend && TEST_API_URL=http://localhost:8080 go test ./e2e -run W5B4_TwoRiders and confirm one rider gets 200 while the other gets 409.
  • From app/, run npx tsc --noEmit and confirm there are no TypeScript errors.
  • On a phone as the rider and an emulator as the customer, go online, send a location update, and confirm the customer sees the marker move.
  • Drop the customer's WebSocket connection and confirm the app reconnects without an app restart.

Week requirements

Rules every piece of code this week must follow.

  • GET /v1/ws?token=<access token> must reject missing or invalid tokens with 401 before upgrading, and register a valid connection to the user identified by the token.
  • A new connection for the same user must replace the old connection. Closing or replacing a connection must remove it and release its goroutines. Concurrent sends and disconnects must remain safe.
  • Hub.Send must never block. It must return false when the user has no connection or a message is dropped. Broadcasts must reach every connected user in order.
  • The worker must publish events to user:{id}. API instances must forward each valid user channel message only to that user's local connection and ignore malformed channel names.
  • Rider endpoints must reject customer requests with 403. Online riders must be tracked in riders:online with a rider:seen:{id} key that expires after 15 seconds. Nearest must respect the requested radius and count, return closest riders first, and remove riders whose seen key has expired.
  • An order claim must use SET NX PX on order:{id}:claim for 30 seconds. Only one rider may hold a claim, only the holder may release it, and an expired claim must be available to another rider. The accept endpoint must return 200 to the claim holder and 409 to other riders. Location updates must be limited to one every 2 seconds per rider and publish {lat, lng} on order:{id}:loc.
  • Replay logs must assign increasing unique IDs, retain only the newest entries, expire their keys, and return only entries newer than the requested ID in order. Topic subscriptions must be race-free, deliver once per subscriber, count only accepted sends, and stop delivery after unsubscribe or user removal.
Live order tracking and rider managementWeek 5 · 17 items available
Start Module 1: WebSocket hub
Week 5: Live order tracking and rider management · Learn the Zomato tech stack from scratch