Rider presence service

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

Ticket

Build a Redis-backed rider presence service in backend/internal/geo, then connect it to rider online, offline and location operations. The service stores rider coordinates and tracks whether each rider is still reporting.

The package must expose these declarations:

go
func NewRiders(rdb *redis.Client) *Riders
func (r *Riders) Ping(ctx context.Context, riderID int64, lat, lng float64) error
func (r *Riders) Offline(ctx context.Context, riderID int64) error
func (r *Riders) Nearest(ctx context.Context, lat, lng, km float64, count int) ([]int64, error)

The tests in internal/geo call these declarations directly.

Assignment must not offer an order to a rider whose location is stale. The next exercise uses your nearest-rider lookup before creating a 30-second claim for a READY order.

Acceptance criteria

  • Ping stores the rider's decimal ID in riders:online and sets rider:seen:{id} to 1 with a TTL greater than zero and at most 15 seconds.
  • Offline removes the rider from riders:online and deletes the corresponding seen key. A subsequent Nearest call does not return that rider.
  • If a rider's seen key is deleted while their GEO member remains, Nearest returns an empty slice and removes the stale member from riders:online. A ZScore check for that member returns redis.Nil.
  • Nearest returns riders in closest-first order. With a 5 km radius and count 2, it returns the two closest riders in the expected order. With the same radius and count 10, it returns exactly three riders and excludes the rider approximately 55 km away.
  • A rider can call POST /v1/rider/online, POST /v1/rider/location and POST /v1/rider/offline successfully. For the location request, the body is {"lat": 28.461, "lng": 77.031}. Each operation returns HTTP 200 or 204.
  • A customer posting {"lat": 1, "lng": 1} to POST /v1/rider/location receives HTTP 403.

Constraints

  • Store rider coordinates in the Redis GEO key riders:online.
  • Use rider:seen:{id} as the presence key for each rider, with a value of 1 and a 15-second TTL.
  • Restrict POST /v1/rider/online, POST /v1/rider/offline and POST /v1/rider/location to authenticated users with the rider role. All other roles must receive HTTP 403.
  • Mount all three routes behind the existing bearer-token middleware.

Design direction

Keep Redis operations in backend/internal/geo. Let this package own GEO updates, seen-key refreshes, offline removal, nearby searches and stale-member cleanup. Put request decoding, authentication, role checks and HTTP status handling in a rider handler, then register the routes in backend/cmd/api/main.go.

Keep GEO membership separate from presence. A GEO member stores coordinates but does not expire automatically. The seen key uses Redis TTL to tell you whether those coordinates are still recent.

Use a pipeline for the paired GEO and presence updates in Ping. This keeps the two writes together without adding separate round trips. In the HTTP handlers, pass the request context through to Redis operations using Go context.

Do not treat GEO results as proof that a rider is online. Use GEOSEARCH nearest query to find nearby candidates, check their seen keys, and remove stale members. Keep HTTP concerns in the HTTP handler layer rather than mixing them into the Redis package. Any layout that meets the contract passes.

How to check your work

Run the Redis-backed tests:

bash
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B3

Then run the end-to-end tests against your running API:

bash
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 TEST_API_URL=http://localhost:8080 make test W=5B3

Read the first failing assertion as the contract that broke. Compare its expected rider IDs, TTL bound or HTTP status with the corresponding Redis key or response before changing another layer.

Finished Exercise 5.7?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Ping stores the rider in riders:online and refreshes the seen key with a TTL of at most 15 seconds.
  2. Offline removes the GEO member and deletes the seen key.
  3. Nearest returns closest online riders and honours the requested radius and count.
  4. Nearest excludes and cleans up riders whose seen keys have expired.
  5. Rider operations succeed with HTTP 200 or 204; a customer location request receives HTTP 403.
  6. Both W5B3 test commands pass with Redis and the API running.

Commands

terminal
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B3
terminal
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 TEST_API_URL=http://localhost:8080 make test W=5B3

Tests

  • TestW5B3_NearestReturnsClosestFirstAndDropsOfflineRiders in backend/internal/geo/riders_test.go
  • TestW5B3_PingGivesTheSeenKeyAShortTTL in backend/internal/geo/riders_more_test.go
  • TestW5B3_NearestDropsAndCleansUpRidersWhoWentSilent in backend/internal/geo/riders_more_test.go
  • TestW5B3_NearestHonoursTheRadiusAndTheCount in backend/internal/geo/riders_more_test.go
  • TestW5B3_RiderLocationAndOffline in backend/e2e/w5_test.go