Geospatial & location systems

Finding the closest driver to a rider without scanning the entire planet.

The idea

Databases are bad at 2D math (Latitude/Longitude). You can't easily ask SQL "find all drivers within 2 miles." Instead, we use systems like Geohashing or S2 Cells.

These algorithms divide the Earth's surface into a grid of squares (cells). Each cell has a string ID (like "9q8yy"). If a driver is in "9q8yy", they only update that specific cell. When a rider requests a car, the system just looks inside the rider's cell. If empty, it expands the search in a ring to the 8 neighboring cells, rapidly zooming out.

Map gridded into Geohash cells. 3 Drivers are online.

How it works (Ring Expansion)

def find_driver(rider_lat, rider_lon):
    # 1. Convert Rider Lat/Lon to a cell ID
    rider_cell = geohash.encode(rider_lat, rider_lon, precision=6)
    
    # 2. Check the rider's exact cell
    drivers = redis.get(rider_cell)
    if drivers: return get_closest(drivers)
    
    # 3. Expand search to the 8 adjacent neighboring cells
    neighbors = geohash.get_neighbors(rider_cell)
    for cell in neighbors:
        drivers.extend(redis.get(cell))
        
    if drivers: return get_closest(drivers)
    return "No drivers available nearby"