Rider assignment worker
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.
Ticket
Build the rider assignment component by adding exclusive claims to the Riders service in backend/internal/geo, connecting READY order events to the worker, and implementing rider acceptance.
The package must expose these methods:
func (r *Riders) Claim(ctx context.Context, orderID, riderID int64) (bool, error)
func (r *Riders) ClaimedBy(ctx context.Context, orderID int64) (int64, error)
func (r *Riders) Release(ctx context.Context, orderID, riderID int64) (bool, error)The tests in internal/geo/assign call these methods directly.
When an order becomes READY, the worker finds nearby online riders and offers the order to one rider at a time. It publishes the offer to the rider's user channel user:{rider_id} through the fan-out from the previous module, so the rider's app hears about it. If a claim expires without acceptance, the worker offers the order to the next nearest rider.
The rider-only POST /v1/orders/{id}/accept endpoint must accept the order only for the claim holder and prevent another rider from winning the same order. The existing order state machine must allow PICK_UP only for the assigned rider.
Acceptance criteria
- The first rider to claim an order succeeds. A second rider attempting the same live claim receives
false, nil.ClaimedByreturns the first rider's ID, which is11in the tested case. - A successful claim has a remaining TTL greater than 25 seconds and at most 30 seconds when checked immediately after creation.
- After a claim expires, another rider can acquire it. The tested claim expires after its TTL is shortened to 30 milliseconds and an 80-millisecond delay; the next rider acquires it successfully, and
ClaimedByreturns6. - A rider other than the claim holder cannot release a claim. Releasing rider
5's claim as rider6returnsfalse, niland preserves the original claim. Releasing it as rider5returnstrue, nil, after whichClaimedByreturns0. - When an order has no claim,
ClaimedByreturns0without an error. - After a READY order is processed by the worker, two online riders calling
POST /v1/orders/{id}/acceptproduce exactly one HTTP200response and at least one HTTP409response. Only one rider can successfully accept the order.
Constraints
- Use
order:{id}:claimas the Redis claim key. - Store the rider ID as a decimal string.
- Claim expiry is exactly 30 seconds, configured with Redis
PX 30000. - Claim acquisition must use Redis
SET NX PX. - Release must compare the stored rider ID before deleting the claim.
- The worker searches within 5 km and requests at most five riders from
Nearest.
Design direction
Keep the claim methods on the existing Riders type in backend/internal/geo. Use Redis for claim ownership so separate worker processes cannot independently reserve the same order.
Keep assignment in the existing worker architecture. When a READY event arrives, use Nearest to find up to five riders within 5 km. Try them in closest-first order. Publish an offer to user:{rider_id} only after a rider successfully acquires the claim. Include kind, order_id and expires_at in the JSON offer. Express expires_at as Unix milliseconds.
Do not let an abandoned offer block an order indefinitely. Let the 30-second claim expire, then retry with the next rider using a small ticker goroutine or delayed retry.
Keep acceptance in the API handler. Check ClaimedBy against the authenticated rider before touching the order. Use a conditional database update so two accepts cannot assign the same order. Return HTTP 200 to the successful claim holder and HTTP 409 to a competing accept. Restrict PICK_UP through the existing order state machine so only the assigned rider can perform that transition.
Any layout that meets the contract passes.
How to check your work
Run the claim unit tests with the race detector:
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 go test -race ./internal/geo/assign/ -run W5B4Run the complete exercise test suite:
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B4Run the end-to-end test against your running API and worker:
cd backend && TEST_API_URL=http://localhost:8080 go test ./e2e -run W5B4_TwoRidersRead the first failing assertion to identify whether claim ownership, expiry, release or acceptance is broken. Check the Redis claim key and its TTL before changing the worker or API handler.
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.
Rider management and assignment
3 items
- 5.6Redis GEO and SET NX PXnot completed
- 5.7Rider presence servicenot completed
- 5.8Rider assignment workernot completed
Up next · Module 4 · 5 items · about 5 h 30 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
Claimallows exactly one rider to acquire a live claim with a 30-second expiry.ClaimedByreturns the current rider ID or0when no claim exists.- Expired claims can be acquired by another rider.
Releaseallows only the claim holder to delete the claim.- The worker assigns READY orders through rider claims.
- Two riders competing for acceptance produce one HTTP 200 and at least one HTTP 409.
Commands
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 go test -race ./internal/geo/assign/ -run W5B4cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B4cd backend && TEST_API_URL=http://localhost:8080 go test ./e2e -run W5B4_TwoRidersTests
TestW5B4_ClaimGoesToExactlyOneRiderinbackend/internal/geo/assign/claim_test.goTestW5B4_ClaimExpiresAfterThirtySecondsinbackend/internal/geo/assign/claim_test.goTestW5B4_ExpiredClaimCanBeTakenByAnotherRiderinbackend/internal/geo/assign/claim_test.goTestW5B4_OnlyTheHolderCanReleaseAClaiminbackend/internal/geo/assign/claim_test.goTestW5B4_ClaimedByIsZeroWhenNobodyHoldsItinbackend/internal/geo/assign/claim_test.goTestW5B4_TwoRidersOneClaiminbackend/e2e/w5_test.go