Ingest millions of position updates
Every driver sends a location every few seconds.
Say it like this
Millions of drivers report position every few seconds, so I put the updates on a Kafka topic partitioned by region, keep only the latest point per driver in the geo index, and let the client slow its reporting when it is idle.
Basic solution
Send updates to a partitioned stream, keep only the latest position per entity in memory, and reduce frequency when idle.
Adaptive reporting into a region-partitioned stream
Drivers report at a rate set by their state. The gateway validates and publishes by region; one consumer keeps only the latest point in the geo index, with a sweep that drops drivers silent for 60 s, while others push to riders and archive history. The matcher reads only the index, never the stream.
How it works
- The driver app reads GPS and sends a small record (driver_id, lat, lng, heading, ts) to the driver gateway. It sends every 4 seconds while moving or on a trip, every 30 seconds while parked and available, and nothing at all when offline. The app decides the interval itself from its own speed and state.
- The gateway checks the driver's token, rejects implausible records (a jump that would need 500 km/h, or a timestamp far from server time), and publishes the record to the locations topic. The partition key is the region, so every update for one city lands on that city's partitions.
- A consumer group per region reads those partitions. For each update it compares the timestamp with the one already stored for that driver and keeps whichever is newer (last-writer-wins), then writes the position into the geo index with GEOADD, which stores the driver under the cell its coordinates fall in.
- Alongside the geo index the consumer records each driver's last update time in a sorted set scored by timestamp, and a sweeper runs every few seconds removing any driver not updated in 60 seconds from both structures. Redis GEO members have no per-member expiry, so this sweep is what a TTL would otherwise do. A driver who stops reporting, because the app closed or the phone died, drops out of the index within a minute and stops being offered trips.
- Two more consumers read the same topic without affecting the first: a trip tracker that pushes the matched driver's position to the rider's app every few seconds, and an archive sink that writes every record to object storage as hourly files for ETA models and fare disputes.
- The matcher reads only the geo index. It never reads the stream, so a burst of location updates cannot slow down a match.
Why a stream in between
- Driver traffic is bursty and the index is a single hot store per region. The log absorbs bursts so the index sees a steady rate, lets a consumer restart and catch up without losing updates, and lets three consumers read the same data that was published once.
Worked example
Driver positions for ride matching in one large city
- 200K drivers online at peak: 120K moving or on trips reporting every 4 seconds, 80K stationary reporting every 30 seconds.
- Update rate: 120K / 4 + 80K / 30 = 30K + 2.7K = 32.7K updates per second. With a fixed 4 second interval it would be 50K; adaptive intervals cut it by a third.
- Payload: about 60 bytes each, so 2 MB per second into Kafka; 8 partitions for the region give 4K updates per second each.
- Geo index writes: 32.7K GEOADD per second plus a ZADD of the last-seen timestamp into one Redis node for the region, about 65K ops per second, under its 100K ops per second; the index holds 200K entries, about 20 MB.
- Archive: 32.7K x 60 bytes x 86,400 = 170 GB per day of raw positions for one city, written as hourly Parquet files; keep 90 days, about 15 TB, in object storage.
- Rider-facing pushes: 50K active trips each receiving the driver position every 4 seconds, 12.5K pushes per second through the connection servers.
Variants and when each wins
Stream, then index (Kafka -> consumer -> Redis GEO)
- Updates go to a partitioned log and a consumer writes the latest position per driver into the geo index. It wins once you have more than a few thousand drivers, because the log absorbs bursts, can be replayed, and feeds several consumers from one publish.
Direct write to the index
- The gateway runs GEOADD against Redis itself for every update. It wins for a small fleet because it is the simplest thing that works; there is no replay and no history, and a Redis stall backs up straight into the gateway.
In-memory index per region service
- Each region's service holds driver positions in its own process memory and matches from there. It wins on latency, since there is no network hop to Redis; the service becomes stateful, and a restart loses the index until drivers report again.
Client-side batching
- The app buffers several points and sends them every 10 seconds. It wins for the archive, where fewer requests matter; it loses for matching, which needs the freshest point.
Recommendation: adaptive reporting on the client, a region-partitioned topic, consumers writing the latest point into Redis GEO with a 60-second last-seen sweep, and the archive as a second consumer.
Failure modes
Stale positions after a consumer lag
- Riders get matched with drivers who drove away a minute ago. It happens because the consumer fell behind the topic, so the index holds positions that are no longer true. Alert on consumer lag measured in seconds, scale consumers to one per partition, and have the matcher ignore any index entry whose timestamp is older than 15 seconds.
Out-of-order updates
- A driver's dot on the map jumps backwards. It happens because a retried request from the app arrived after a newer update had already been written. Compare timestamps before writing (last-writer-wins on the device timestamp) and drop any update older than the one already stored.
Index pinned by one region
- One Redis node runs at 100 percent while the others idle. It happens because a mega-city maps to a single partition and a single node, and the region key alone is too coarse for the biggest cities. Split large regions into sub-cells and spread those across nodes.
Ghost drivers
- The matcher offers trips to drivers who closed the app long ago. It happens because index entries never expire, so a driver's last position stays forever. Record a last-seen timestamp that every update refreshes and sweep out drivers older than 60 seconds, and have the gateway delete the entry when a driver explicitly goes offline.
Battery drain and dropped reports
- Drivers complain about battery life, and reports become sparse as phones throttle the app. It happens because the app reports at a fixed high frequency regardless of whether the car is moving. Use an adaptive interval based on speed and state, and tolerate short gaps by interpolating the position along the last heading for a few seconds.
Interviewer follow-ups
Answer out loud before you open each one.
Why partition the topic by region rather than by driver id?
Because the consumers and the index are per region: one consumer group owns a city and writes to that city’s Redis node, so all of a region’s updates must land on its partitions. Ordering per driver still holds within a partition because a driver stays in one region for the duration.
How do you choose the reporting interval?
By state: every 4 seconds while on a trip or moving, since the rider sees the car move and the matcher needs freshness; every 30 seconds while stationary and available; never while offline. The client decides from its own speed, which cuts the update rate by a third or more at no cost to matching.
What happens to matching when the stream is down?
The index keeps serving its last known positions until the last-seen sweep removes them, so matching degrades over about a minute rather than stopping instantly. After that the matcher should fall back to a direct-write path from the gateway or refuse matches rather than offer trips to stale drivers.
Where does the position history go and why keep it?
A second consumer sinks every update into hourly files in object storage, partitioned by region and day. It feeds the ETA model with real travel times, settles fare disputes, and lets you replay the stream into a rebuilt index after an outage.
Connections
- Built on
- Ring buffer · Geohash and hierarchical cells (H3, S2)
- Pairs with
- Find things near a point · Keep events as a replayable log · Shard by region · Expire data automatically · Slow the producer when the consumer is behind
- Pulls against
- Default to a relational database
- Used in
- Ride Matching (Uber / Lyft)
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon API Gateway | Driver gateway | API Gateway, Apigee | API Management | Kong, Tyk |
| Amazon MSK (Kafka) | locations topic | Managed Service for Apache Kafka | Event Hubs (Kafka API) | Confluent Cloud, Redpanda |
| Amazon EC2 Auto Scaling | Location consumer, Trip tracker, Matcher | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |
| Amazon ElastiCache | Geo index (Redis) | Memorystore | Azure Managed Redis | Redis, Valkey, Memcached |
| Amazon S3 | Archive (S3) | Cloud Storage | Blob Storage | Cloudflare R2, MinIO, Ceph |