OF Case study · 2026
GPA: a progressive geographic proximity engine in Go
A progressive proximity engine in Go: given an origin, it returns the next places to search, in distance order, in signed and stateless batches.
- Role
- Design and implementation
- Stack
- Go · Docker · Prometheus
- Status
- v0.1.0, November 2026
- License
- AGPL-3.0-or-later
Overview
GPA (Geographic Proximity API) is a proximity engine that answers one question: given an origin, which places should I search next, in order of distance? It returns candidate municipalities in batches; the consuming system searches its own data and asks for the next batch when it needs more. GPA knows nothing about the consumer's business rules.
Context
Many products need to find something "near" a place: candidates for a job, sellers in a marketplace, properties, delivery partners. The usual approach is a fixed radius, which returns too much in dense regions and nothing in sparse ones. Each team ends up re-implementing the same distance logic, usually inside the database of a specific product.
GPA covers Brazil's 5,571 municipalities, using IBGE data and an open dataset of municipal coordinates, and was designed as an independent, reusable service.
Problem
The requirements that shaped the design:
- Order results by straight-line distance, across state borders.
- Never cap the list of neighbors artificially; the consumer decides when to stop.
- Scale horizontally without session state.
- Run without a mandatory database.
- Publish no capacity number without a load test behind it.
Solution
A progressive search: the consumer starts a search with an origin (an IBGE code or coordinates) and a batch size, and receives the nearest municipalities plus a cursor. When it needs more, it sends the cursor back and receives the next batch, with no repeats. Policies such as "same state first", state filters, a maximum distance and a total limit are applied on top of the same precomputed ordering.
Architecture
GPA is a single Go binary with the geographic data embedded. Versioned JSON files, one per state, are verified against SHA-256 hashes in a manifest at startup; loading and validating the whole catalog takes about 32 ms. An immutable data snapshot can be swapped atomically, so a dataset update never serves a half-loaded state.
- httpapi: middleware, handlers and an OpenAPI 3.1 contract served with Swagger UI.
- proximity: search policies and iteration over the precomputed ordering.
- cursor: HMAC-signed, stateless cursors.
- spatial: nearest-neighbor search for arbitrary coordinates.
- datasets: catalog, per-state partitions and an on-demand cache with de-duplicated loading.
PostgreSQL is optional and only backs administrative features: API keys, consumers, usage and audit logs.
Technical decisions
Dot products instead of Haversine for ordering
Each municipality stores a precomputed 3D unit vector. Chord distance between unit vectors is monotonic with great-circle distance, so ordering by dot product (three multiplications) gives exactly the same order as Haversine (several trigonometric functions). Haversine is used only to report the distance in kilometres.
Store the full ordering, not a top-100
Storing only uint16 indices, the complete neighbor ordering for every origin in Brazil takes about 62 MB (5,570 × 5,569 × 2 bytes). That made the "no artificial cap" requirement affordable. Partitions are built per state on demand, and pointer-free structures keep the garbage collector from scanning them.
A hybrid spatial index, chosen by benchmark
The specification asked for a KD-tree. Benchmarked against brute force, the KD-tree was 11× faster for the 10 nearest neighbors (3.1 µs versus 36 µs) but slower for a full ranking (724 µs versus 558 µs). GPA uses the KD-tree for small k and a linear scan above that.
Signed cursors instead of sessions
The cursor carries the search parameters and position, signed with HMAC-SHA-256 and a key ID for rotation. Any replica can continue any search, tampering invalidates the signature, and the deterministic tie-break (distance, then IBGE code) guarantees no duplicates across pages.
Technologies
Go (standard library net/http), HMAC-SHA-256, OpenAPI 3.1, Prometheus metrics, Docker (distroless, non-root, amd64 and arm64 images), optional PostgreSQL, and an offline Python script that builds and validates the dataset.
Results
Version 0.1.0 was released in November 2026 with every phase of the original plan complete: data, proximity engine, cache, progressive search API, authentication and rate limiting, production deployment, observability and load testing.
A first load test, run on a 2-core laptop with the server and the load generator competing for the same CPU, sustained about 11,800 requests per second with a P99 of 4.4 ms without authentication and about 9,400 requests per second with a P99 of 6.2 ms with API-key verification, with zero errors in both runs. These are floor values from a constrained environment, not production capacity; measurements on production hardware haven't been made yet.
Related
- Radar IA — A platform that shows companies how AI assistants describe them as employers, by asking several LLM providers the same standardized questions.