← Create New Paste

How to Design a Geofencing Alert Service in a System Design Interview

15 views
"Design a service that notifies users when they enter or leave a geographic area" shows up in interviews at delivery, ride-hailing, retail and IoT companies. Think store arrival notifications, alerts when a delivery van leaves a depot, or a fleet manager's "truck left the allowed zone" warning. It differs from a nearby-search service: the question is not "what is close to me?" but "did this moving device just cross a boundary?" 1. Clarify requirements Functional: - Clients (businesses) create, update and delete geofences: circles or polygons. - Devices send location updates. - The system emits ENTER, EXIT and optionally DWELL events to subscribers (push notification, webhook). Non-functional questions to ask: How many devices and how often do they report (say 10 million devices, every 30 seconds = about 330k updates per second)? How many fences (say 50 million)? Acceptable delay (a few seconds)? Is missing an event worse than sending a duplicate? Usually duplicates are tolerable if consumers are idempotent. 2. High-level flow Device -> location ingestion API -> message queue (Kafka, partitioned by device ID) -> geofence evaluation workers -> event store + notification/webhook dispatcher. Partitioning by device ID matters: all updates for one device land on one worker in order, so the worker can compare the new position with the last known state without distributed locking. 3. Finding candidate fences quickly Testing every point against 50 million polygons is impossible. Index fences spatially: - Cover each fence with geohash or S2/H3 cells at a chosen resolution, and store a map cell -> list of fence IDs. - For each update, compute the device's cell, fetch candidate fences, then run an exact point-in-polygon (ray casting) or distance test for circles. Discuss choosing the resolution: coarse cells mean many false candidates; fine cells mean huge fences explode into thousands of cells. A common answer is a multi-level covering (S2 does this well). 4. State and transitions Per device keep a small state record: set of fence IDs it is currently inside, plus timestamps. ENTER = in new set but not old; EXIT = in old set but not new. Store it in Redis or a key-value store keyed by device, cached in the worker. DWELL can be a timer: if still inside after N minutes, emit once. 5. Handling messy GPS Real-world data jitters. Without care, a device on the edge fires ENTER/EXIT every few seconds. Mitigations: hysteresis (require a point to be X meters inside to enter and X meters outside to exit), requiring two consecutive readings, and ignoring fixes with poor accuracy values. Interviewers love this detail because it shows production thinking. 6. Delivering events Write each event to a durable log with an ID, then deliver through a webhook dispatcher with retries and exponential backoff. Give consumers an event ID so they can deduplicate. Push notifications go through APNs/FCM. 7. Scaling and trade-offs - Fence updates: publish changes to workers so their local index stays fresh. - Hot spots: a stadium with many devices and fences may overload one cell; split partitions by device, not by region, to spread load. - Battery: on mobile, OS-level region monitoring can do a coarse check on the device and the server confirms. 8. Wrapping up Close by summarising the bottleneck (spatial lookup and per-device state), the trade-offs you chose and what you would monitor: event latency, false-transition rate, and dispatcher retry backlog. System design rounds reward structure and calm communication more than perfect answers. If you want an extra hand when it counts, Linkjob AI (https://linkjob.ai) offers real-time help during job interviews and technical interviews, helping you keep your design discussion organised as follow-up questions arrive.
Created Nov 18, 2026 00:32 Expires Nov 18, 2027 00:32