DEV Community

Kunal Garg
Kunal Garg

Posted on

How Uber Finds You a Driver in 3 Seconds (System Design Teardown)

We take it for granted. You stand in the rain, tap "Confirm Ride" on your phone, and in under three seconds, a driver is locked in.

But if you look at this from a backend perspective, the scale is terrifying. In any major city, there are hundreds of thousands of active riders and drivers moving simultaneously.

If you built a naive loop to calculate the distance from one rider to every active driver, sort them, and return the closest—your database would crash instantly under the load of thousands of requests per second.

Here is the architectural breakdown of how Uber’s dispatch engine, DISCO, solves this massive real-time geospatial matching problem.

1. Shrinking the Search Space: Google S2 Geometry
You can't search the whole city. You have to shrink the world first.

Uber solves this by dividing the physical map of the world into tiny, mathematical cells using Google's S2 Geometry Library.

S2 projects the spherical Earth onto a cube, dividing it into a hierarchical grid. To make lookups incredibly fast, it maps these grid cells using a mathematical space-filling curve called a Hilbert Curve.

Because of how the Hilbert Curve folds, cells that are physically next to each other in the real world have numerical IDs that are close to each other.

Instead of doing complex 2D geographical math to find a driver, the Go/Java backend asks a simple, fast question: "Which driver IDs are currently sitting in cell IDs X, Y, and Z?"

2. Keeping Locations Fresh with WebSockets & Redis
A database query is only as good as its data. Since drivers are constantly moving, how does the system keep locations up-to-date without overloading a traditional disk-based database?

WebSockets over HTTP: Drivers’ apps don't constantly poll the server. Instead, they maintain a persistent, always-open WebSocket connection to stream GPS pings continuously.

In-Memory Storage (Redis): The incoming GPS data is immediately routed to an in-memory geospatial index (like Redis) rather than written to disk. This allows sub-millisecond read/write speeds, keeping driver locations current to the exact second.

3. Avoiding the Double-Booking Race Condition
What happens when two riders in the exact same block request a ride at the same millisecond, and the system matches them to the same nearby driver?

Uber's dispatch system handles this at the database layer using atomic operations.

When a driver is selected, the system sends an offer over their WebSocket with a ~10-second countdown. The instant the driver accepts, an atomic check-and-set operation locks that Driver ID to that specific trip ID.

Because this check is atomic, only one write can succeed. If a second request comes in a millisecond later, it is rejected by the lock, and the offer gracefully cascades to the next driver on the ranked list.

Want the Full Deep Dive?
If you want to see the visual diagrams of this architecture in action—including how S2 cells map to the Hilbert Curve and how the system manages dynamic surge pricing—check out the full video teardown.

💬 What do you think? If you were building a real-time matching system today, would you use S2 Geometry, or would you look at alternatives like Uber's hexagonal H3 library? Let's discuss in the comments!

Top comments (0)