π Geofence Event Processing Service
A lightweight, production-aware service for ingesting GPS events, tracking vehicle states in real time, and detecting geofence boundary transitions (enter/exit events). Built for reliability, clarity, and operational visibility.
π Overview
This is a location-based service for a taxi company. Vehicles send GPS coordinates to an HTTP endpoint, and the system determines: When a vehicle enters a geofence When a vehicle exits a geofence What zone a vehicle is currently in Whether a vehicle is moving, idle, or outside all zones
The design focuses on clean architecture, performant in-memory state management, and practical engineering choices that scale.
β¨ Key Features β Real-time GPS event ingestion- Vehicles push location events directly to /location. Each event is validated, logged, and processed instantly.
βOverlapping Zone Resolution- Zones have a priority field to correctly determine the vehicle's zone status when multiple geofences overlap at the same location.
β Geofence enter/exit detection- The service defines simple polygon-based zones. Using point-in-polygon logic, we determine whether a new coordinate crosses a boundary relative to previous state.
β Vehicle state tracking In-memory cache maintains: Last known coordinates Current geofence zone Timestamp of last update Movements across zones
β Query API /vehicle/ returns the vehicleβs latest zone, last location, and last transition.
β Lightweight, fast, and easy to deploy Zero external dependencies beyond FastAPI and standard Python libs.
π Architecture & Design Decisions
-
FastAPI for the service layer FastAPI gives: Async request handling Automatic validation via Pydantic Auto-generated Swagger docs Easy extensibility This is ideal for real-time event ingestion.
-
In-memory state store A dictionary holds vehicle states: O(1) lookups Simple and extremely fast Perfect for this challenge timeframe In a real deployment I'd use Redis β strong consistency, TTL expiry, horizontal scalability.
-
Zones defined as simple polygons Flexible for scaling beyond rectangles. Point-in-polygon is implemented with a clean, readable function.
-
Event-driven detection Each location update triggers: Zone lookup Previous vs current zone comparison Emit enter/exit events Update state store This approach minimizes unnecessary computations.
-
Operational Awareness Built-In Input validation Error responses with detailed context Logging on every critical action Clear separation of concerns
If deployed, hooking this into Prometheus or OpenTelemetry would be trivial.
π How to Run
-
Clone git clone cd geofence-service
-
Install dependencies pip install -r requirements.txt
-
Start the service uvicorn main:app --reload
-
Access API docs http://localhost:8000/docs
π‘ API Endpoints POST /location
Ingest a GPS event.
{ "vehicle_id": "TX-102", "latitude": 28.5531, "longitude": 77.2079, "timestamp": "2025-11-30T18:23:12Z" }
GET /vehicle/{vehicle_id}
Check current zone, last location, and transitions.
π§ Assumptions -GPS updates are reasonably frequent -No authentication needed for this challenge -Zones are static -In-memory store is acceptable given challenge scope -Vehicles do not send absurdly noisy GPS coordinates
π Edge Cases Handled -Missing/invalid coordinates -Vehicle sending first-ever location event -Boundary-edge coordinates (treated as inside) -Zone to no-zone transitions -No-zone to zone transitions -Rapid oscillation filtering (simple debounce could be added)
π§© Directory Structure project/ ββ src/ β ββ api/ β β ββ events.py # POST /v1/events β β ββ vehicles.py # GET /v1/vehicles/{id}/zone β ββ services/ β β ββ geofence_service.py # zone detection, transition logic,point-in-polygon checks β β ββ vehicle_service.py # state management, validation β ββ models/ β β ββ schemas.py # Pydantic request/response schemas(LocationEvent, Zone, ...) β β ββ events.py # TransitionEvent model β ββ storage/ β β ββ memory_store.py # simple in-memory vehicle state store β ββ main.py # FastAPI entrypoint + include routers ββ zones.json # stores zones information - {zoneid, name, priority, polygon} ββ README.md ββ requirements.txt
π Performance & Scalability Notes
- Current performance:
- O(1) lookup for all vehicles
- Zones check in < 0.05 ms
- To scale production-wise
- Debounce logic to prevent zone flip-flopping
- Add Kafka/EventBridge for reliable ingestion
- Add background workers for zone checks
- Configurable zones via API
π Future Improvements If I had more than 2 hours, Iβd add:
- WebSocket live tracking dashboard
- Replay engine for historical events
- Heatmap analytics for vehicle density
- Horizontal scaling via Kubernetes
- Move state to Redis
- Add Kafka/EventBridge for reliable ingestion
- Persistence layer for long-term tracking history
π Summary This codebase is designed to reflect real engineering judgment, not just algorithmic correctness. It is intentionally structured, scalable, and production-oriented β while still being lightweight enough to meet the challenge's 2-hour constraint.