Location & logistics
Geofencing - many points against many polygons
Teaches how a covering index reduces every-point-against-every-fence to a handful of tests, and why a jittery GPS makes the crossing itself the harder problem.
The first act plays here. Owned once with credits, never a subscription.
What this board gets wrong on purpose
The tension
Hysteresis is what makes the product usable and it is a deliberate, unmeasurable loss of correctness: a 50 m buffer and a 30-second dwell mean a genuine crossing is reported late, and a genuine brief visit - a courier who is inside a 60 m fence for twenty seconds - is never reported at all. The numbers are global rather than per fence, so a 10 km haulage yard and a 40 m drop-off point get the same 50 m buffer, which is negligible for one and larger than the other. The board also evaluates strictly per point rather than per segment, so a device that reports at 09:00:00 outside and 09:00:04 outside, having driven straight through a small fence in between, produces no event whatsoever - the crossing happened entirely between two samples, and nothing here interpolates the path to notice. Raising the report rate to fix that multiplies the largest cost in the system, and segment-based evaluation would fix it properly and doubles the index work, so neither is drawn here. Exactly-once delivery is claimed only up to the customer endpoint being honest about its idempotency key; a customer that ignores the key gets at-least-once and no way to tell.
Practise this as an interview
Take Geofencing - many points against many polygons as a timed system design interview on a blank board - on your own, with the requirements to check yourself against, or with a candidate you invite.
Start here
Why this design
Geofencing is two hard problems wearing one name. The first is a multiplication - every reported point against every fence - that no cluster can afford, so the question becomes how to index AREAS rather than points. The second is that GPS lies by 5 to 50 metres, so deciding whether a crossing really happened is harder than detecting one. Interviewers use it to probe spatial indexing and stateful exactly-once event delivery; logistics, ride-hailing and retail-marketing platforms run this exact pipeline.
What you will learn
- Why the naive design is 375 billion point-in-polygon tests a second, and how one S2 cell lookup reduces it to about 212,500.
- Why fences are indexed with an S2 region cover (areas, not points), and what geohash and H3 do worse here.
- Why hysteresis - a buffer distance, a dwell time and a confirmation count - is the feature, not a refinement: a parked van otherwise emits 900 events an hour.
- Why each transition is a compare-and-set on a (device, fence) state row, and why the webhook idempotency key is built from the transition, not the attempt.
- Why the whole fence definition (2.7 GB) and index (1.0 GB) fit in memory, while the 384 MB of state decides billing.
- The crossing nobody sees: a small fence driven through between two samples.
How to read this board
- Press play once: the walkthrough follows one position from the gateway to a delivered webhook.
- THE MOVING POINT, then THE FENCES: the stream in, and the index it is tested against.
- DID IT REALLY HAPPEN: hysteresis and the compare-and-set on state.
- THE OWNER: who draws the fences and receives the events.
- DATA MODEL: the fence, its cell cover, the per-device state and the event log.
- Then pages 1 to 6 in order: requirements, capacity, storage, availability, how it works, deployment.
1 · Requirements
Two million moving points, three million fences, and a customer system that will act on every event we emit. The requirements divide neatly into "make the matching possible" and "make the events trustworthy", and the second is harder.
Ask these before designing
- How many fences, and how big? Three million small fences and three thousand enormous ones are different indexes. A covering that produces 40,000 cells for one national-park-sized fence will quietly dominate the whole index.
- Is an enter event worth money? If it triggers billing, a duplicate is a refund and a missed one is a dispute, and the whole board tightens. If it turns a light on, at-least-once is fine and half this design is unnecessary.
- Does the crossing itself matter, or only the state? "Tell me when it enters" needs transition detection. "Tell me which devices are inside now" is a query and needs almost none of the hysteresis machinery.
- How long may an event be late? Two seconds means evaluating on the hot path of ingest. Two minutes permits a batch, and a batch is an order of magnitude cheaper.
- What counts as being inside? Inside the polygon, inside it for thirty seconds, or inside it with the engine off? Every product means the third and every spec says the first.
- Who owns the false positives? GPS is noisy by 5-50 m depending on sky. Somebody must choose between late events and duplicated ones, and that person is not the engineer.
Functional
- Define fences as circles or polygons, with an owner, a rule and an endpoint.
- Evaluate every incoming position against the fences that could contain it.
- Emit ENTER, EXIT and DWELL events with the position and time that caused them.
- Suppress events caused by GPS noise rather than by movement.
- Deliver each event to the owner’s endpoint with retries and an idempotency key.
- Answer "which devices are inside this fence now" without replaying anything.
Non-functional
- Event latency p95 under 3 s from the position that caused it.
- No duplicate ENTER for one crossing, enforced by a compare-and-set on the state row rather than by the evaluator being careful.
- Evaluation must be O(candidate fences), never O(all fences) - this is the requirement the whole index exists to satisfy and it should be stated as one.
- 99.95% for evaluation; delivery 99.9% with retries over 24 hours.
- Fence changes live within 60 s, including the re-covering of a redrawn polygon.
- Evaluation is idempotent per (device, fence, position timestamp), because the position stream will be replayed.
Explicitly out of scope
Producing the positions (that is the fleet-tracking board), routing and ETA, proximity search over points, indoor positioning and Bluetooth beacons - which solve the same product problem with entirely different physics.
The rest of this board
Available on Tier Slate
This page publishes the question. The answer — 5 more written pages, an 8-step narrated walkthrough and a 5-table schema — is the board itself, and it opens in Tierslate.
- 5written pages
- 8walkthrough steps
- 5tables36 columns
5 pages behind this one
- Capacity estimation
- Storage estimation
- Availability
- How it works
- Deployment plan