← Lessons

quiz vs the machine

Gold1410

System Design

Geofencing

Firing events when a moving object enters or leaves a defined region.

5 min read · core · beat Gold to climb

What a geofence is

A geofence is a defined region, often a circle or polygon, that triggers an enter or exit event when a tracked object crosses its boundary. Examples include alerting when a driver arrives at a pickup zone or when a phone leaves a warehouse.

The matching problem

For each position update you must answer which fences contain this point. Checking every fence per update does not scale, so fences are stored in a spatial index like an R tree or cell index. A position lookup retrieves only the candidate fences whose bounding region overlaps, then a precise point in polygon test confirms membership.

Detecting transitions

A fence cares about changes, not just current state. The system remembers the previous inside or outside flag per object and fence pair, and emits an event only when that flag flips. This avoids repeated alerts while a device sits inside.

Key idea

Geofencing narrows candidate regions with a spatial index, confirms with a precise test, and emits events only when inside or outside state flips.

Check yourself

Answer to earn rating on the learn ladder.

1. Why store geofences in a spatial index?

2. When should an enter event be emitted?