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:

go
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. ClaimedBy returns the first rider's ID, which is 11 in 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 ClaimedBy returns 6.
  • A rider other than the claim holder cannot release a claim. Releasing rider 5's claim as rider 6 returns false, nil and preserves the original claim. Releasing it as rider 5 returns true, nil, after which ClaimedBy returns 0.
  • When an order has no claim, ClaimedBy returns 0 without an error.
  • After a READY order is processed by the worker, two online riders calling POST /v1/orders/{id}/accept produce exactly one HTTP 200 response and at least one HTTP 409 response. Only one rider can successfully accept the order.

Constraints

  • Use order:{id}:claim as 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:

bash
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 go test -race ./internal/geo/assign/ -run W5B4

Run the complete exercise test suite:

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

Run the end-to-end test against your running API and worker:

bash
cd backend && TEST_API_URL=http://localhost:8080 go test ./e2e -run W5B4_TwoRiders

Read 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.

Finished Exercise 5.8?

Rider management and assignment

3 items

  • 5.6Redis GEO and SET NX PXnot completed
  • 5.7Rider presence servicenot completed
  • 5.8Rider assignment workernot completed

Live rider tracking

Up next · Module 4 · 5 items · about 5 h 30 min

Start module 4

Implementation

Run locally

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

  1. Claim allows exactly one rider to acquire a live claim with a 30-second expiry.
  2. ClaimedBy returns the current rider ID or 0 when no claim exists.
  3. Expired claims can be acquired by another rider.
  4. Release allows only the claim holder to delete the claim.
  5. The worker assigns READY orders through rider claims.
  6. Two riders competing for acceptance produce one HTTP 200 and at least one HTTP 409.

Commands

terminal
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 go test -race ./internal/geo/assign/ -run W5B4
terminal
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B4
terminal
cd backend && TEST_API_URL=http://localhost:8080 go test ./e2e -run W5B4_TwoRiders

Tests

  • TestW5B4_ClaimGoesToExactlyOneRider in backend/internal/geo/assign/claim_test.go
  • TestW5B4_ClaimExpiresAfterThirtySeconds in backend/internal/geo/assign/claim_test.go
  • TestW5B4_ExpiredClaimCanBeTakenByAnotherRider in backend/internal/geo/assign/claim_test.go
  • TestW5B4_OnlyTheHolderCanReleaseAClaim in backend/internal/geo/assign/claim_test.go
  • TestW5B4_ClaimedByIsZeroWhenNobodyHoldsIt in backend/internal/geo/assign/claim_test.go
  • TestW5B4_TwoRidersOneClaim in backend/e2e/w5_test.go