Atomic Dispatch Engine with DynamoDB & SQS
FairDispatch is a domain-agnostic gig-economy engine that guarantees exactly-once resource assignment and algorithmic fairness under burst traffic. It uses DynamoDB conditional writes and SQS delay queues to completely eliminate race conditions and double-booking without requiring distributed locks.
If you’ve ever used a food delivery app during a heavy downpour, or tried to book a cab on New Year’s Eve, you’ve likely experienced the ghost-driver phenomenon. The app assigns you a driver, you wait five minutes, and suddenly the driver vanishes, or your app crashes and resets.
Underneath the UI, a classic backend failure is occurring: a race condition. Hundreds of concurrent requests are fighting for a handful of available drivers. Naive dispatch engines will either crash under the load or accidentally double-assign the same driver to multiple users.
For the AWS x WeMakeDevs "First Commit" hackathon, we set out to solve this exact problem. We built FairDispatch, a generic gig-economy dispatch engine that guarantees exactly-once assignments under concurrent load.
The best part? We didn't use Redis. We didn't build a complex distributed locking service (like Redlock). We solved it using just two native AWS primitives: DynamoDB and SQS.
Here is how we did it.
1) The Core Guarantee: DynamoDB Conditional Writes
When a burst of dispatch requests comes in, our Go-based worker process pulls them and queries DynamoDB to find available drivers in the user's geographic zone.
If multiple users are in the same zone, the Go workers might fetch the exact same
cab-123 record for multiple requests simultaneously. If they all send a standard UpdateItem command to mark the driver as HELD , the last write wins, and the driver is double-booked.To prevent this, we used DynamoDB Conditional Writes.
When a worker attempts to claim a resource, it attaches a strict
ConditionExpression :// Go SDK v2 implementation
_, err := client.UpdateItem(ctx, &dynamodb.UpdateItemInput{
TableName: aws.String("resources"),
Key: map[string]types.AttributeValue{
"id": &types.AttributeValueMemberS{Value: resourceID},
},
UpdateExpression: aws.String("SET #s = :held, assigned_request = :reqId"),
// Critical correctness check:
ConditionExpression: aws.String("#s = :available"),
ExpressionAttributeNames: map[string]string{
"#s": "status",
},
ExpressionAttributeValues: map[string]types.AttributeValue{
":held": &types.AttributeValueMemberS{Value: "HELD"},
":reqId": &types.AttributeValueMemberS{Value: requestID},
":available": &types.AttributeValueMemberS{Value: "AVAILABLE"},
},
})
Because DynamoDB evaluates this condition atomically on the server side, only the very first request to reach the database will succeed. The exact millisecond
cab-123 changes to HELD , any other concurrent requests evaluating #s = :available will be rejected with a ConditionalCheckFailedException .Those workers instantly catch the error and move on to the next best driver. Zero double-booking, guaranteed by the database itself.
2) Event-Driven TTL with SQS Delay Queues
Once a driver is
HELD , the platform waits for the driver to accept the gig on their phone. If they don't respond within 30 seconds, the driver needs to be released back into the AVAILABLE pool.The naive approach is to write a cron job that runs every minute, runs a heavy
Scan on the database to find stale holds, and updates them. This introduces latency (a driver might be held for 59 seconds before the cron job frees them) and wastes database read capacity.Instead, we used SQS Delay Queues to build an event-driven TTL (Time-To-Live) mechanism.
The exact millisecond a worker successfully claims a driver in DynamoDB, it pushes a tiny message to an SQS
ttl-cleanup queue with DelaySeconds: 30 .For 30 seconds, that message is invisible. At exactly the 30-second mark, the message materializes in the queue. Our worker pulls it and executes an O(1) targeted check against DynamoDB: Is
cab-123 STILL held by req-456 ?If yes, it releases the driver. If the driver already confirmed the ride (moving to
BUSY ), the worker just discards the message.This gives us accurate expiration without a single heavy database scan.
3) Zero-Cost Development with LocalStack
Building highly concurrent architectures usually requires racking up AWS bills just for testing.
We built and load-tested this entire architecture, hammering the API with concurrent requests against a pool of simulated drivers, without spending a single cent. We ran the entire AWS stack (DynamoDB and SQS) locally using LocalStack.
By simply pointing our AWS SDK
EndpointResolver to localhost 4566 , we had a full emulation of the AWS cloud running inside Docker Compose. It allowed us to rapidly iterate on our schema and test the limits of our conditional writes purely on local hardware.My Journey: From Concept to First AWS Build
I built Fair-Dispatch as a solo developer for this hackathon. Taking on this project meant I had to understand the deep tradeoffs of how massive real-world systems (like Uber or Swiggy) actually operate under the hood.
I started by researching the dispatch problem: why systems crash under load, why race conditions occur, and why naive polling doesn't scale. Translating those concepts into a robust architecture taught me an incredible amount about system design, decoupling services, and state machines.
Incredibly, this project was my very first time using AWS. Diving straight into DynamoDB conditional updates, SQS Delay Queues, and the AWS Go SDK was intimidating at first. However, realizing how powerful these native cloud primitives are when combined logically was a massive "aha" moment for me. AWS didn't just provide the infrastructure; it fundamentally shaped how I designed the solution.
Conclusion
By treating DynamoDB as an atomic state machine and SQS as an event-timer, we built a bulletproof dispatch primitive. No distributed locks, no polling cron jobs, just raw AWS services doing exactly what they are designed to do.
If you are building logistics platforms or any application where scarce resources are fought over concurrently, lean into conditional writes. The database is smarter than you think!
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article