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:
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
Pingstores the rider's decimal ID inriders:onlineand setsrider:seen:{id}to1with a TTL greater than zero and at most 15 seconds.Offlineremoves the rider fromriders:onlineand deletes the corresponding seen key. A subsequentNearestcall does not return that rider.- If a rider's seen key is deleted while their GEO member remains,
Nearestreturns an empty slice and removes the stale member fromriders:online. AZScorecheck for that member returnsredis.Nil. Nearestreturns riders in closest-first order. With a 5 km radius and count2, it returns the two closest riders in the expected order. With the same radius and count10, it returns exactly three riders and excludes the rider approximately 55 km away.- A rider can call
POST /v1/rider/online,POST /v1/rider/locationandPOST /v1/rider/offlinesuccessfully. For the location request, the body is{"lat": 28.461, "lng": 77.031}. Each operation returns HTTP200or204. - A customer posting
{"lat": 1, "lng": 1}toPOST /v1/rider/locationreceives HTTP403.
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 of1and a 15-second TTL. - Restrict
POST /v1/rider/online,POST /v1/rider/offlineandPOST /v1/rider/locationto authenticated users with the rider role. All other roles must receive HTTP403. - 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:
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B3Then run the end-to-end tests against your running API:
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 TEST_API_URL=http://localhost:8080 make test W=5B3Read 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.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
Pingstores the rider inriders:onlineand refreshes the seen key with a TTL of at most 15 seconds.Offlineremoves the GEO member and deletes the seen key.Nearestreturns closest online riders and honours the requested radius and count.Nearestexcludes and cleans up riders whose seen keys have expired.- Rider operations succeed with HTTP 200 or 204; a customer location request receives HTTP 403.
- Both W5B3 test commands pass with Redis and the API running.
Commands
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B3cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 TEST_API_URL=http://localhost:8080 make test W=5B3Tests
TestW5B3_NearestReturnsClosestFirstAndDropsOfflineRidersinbackend/internal/geo/riders_test.goTestW5B3_PingGivesTheSeenKeyAShortTTLinbackend/internal/geo/riders_more_test.goTestW5B3_NearestDropsAndCleansUpRidersWhoWentSilentinbackend/internal/geo/riders_more_test.goTestW5B3_NearestHonoursTheRadiusAndTheCountinbackend/internal/geo/riders_more_test.goTestW5B3_RiderLocationAndOfflineinbackend/e2e/w5_test.go