We Stopped Reverse-Geocoding Every Point: The Sampling Trick That Made Multi-Region Routing Affordable
Performance work is often less about clever algorithms and more about refusing to do expensive work you do not actually need.
In a geo-aware routing gateway, region resolution can become one of those deceptively expensive steps. Every route request may contain multiple coordinates. Every coordinate can trigger a reverse-geocode lookup. If you are not careful, the system spends a meaningful part of its time figuring out where a request belongs before it even starts routing.
The optimization in this codebase is simple enough to miss and strong enough to matter.
The naive approach
The naive design would inspect every coordinate in a request, reverse-geocode each one through Photon, map every country code to a deployment region, and then decide whether the route is single-region or cross-region.
That is easy to explain and expensive to run.
It is especially wasteful for shapes with repeated coordinates, dense paths, or endpoints like /matrix where not every point contributes equally to the routing decision.
The actual design
src/services/gatewayService.js uses coordinatesForRegionLookup() to reduce the lookup set before any region call is made.
The code does three smart things:
- deduplicates coordinates with
uniqueCoordinates() - treats
/matrixspecially by only sampling the first and last relevant points - for larger routes, samples endpoints plus the bounding extremes returned by
boundingExtremeCoordinates()
This is a great example of practical optimization. Instead of aiming for theoretical completeness, it asks a better question: what is the minimum coordinate set that is likely to preserve the region decision?
In many real routes, that answer is much smaller than the full payload.
Why this works well in production
This optimization compounds with caching. Region lookups in src/services/regionService.js are cached under coordinate keys, so reducing the number of lookups lowers both immediate latency and long-term cache churn. That matters because reverse geocoding is not just slower than an in-memory check. It is also an external dependency with its own failure modes.
The design also keeps the optimization local to the orchestration layer. Routes do not need special-case logic. Clients do not need to change what they send. The gateway becomes faster without making the API more complicated.
What the code is implicitly saying
This design choice tells me the author understands something important about performance engineering: the best optimizations often happen before the expensive call, not inside it.
There is no heroic concurrency trick here. No exotic index. No queue. Just a decision to avoid unnecessary reverse lookups while preserving enough signal to make the right routing call most of the time.
That kind of optimization tends to age well because it simplifies the workload rather than merely accelerating it.
The tradeoff
Sampling is a heuristic. It is not mathematically perfect region classification.
If a path snakes across a border in a way that endpoints and bounding extremes do not capture cleanly, the lookup set may miss meaningful transitions. The system has a second line of defense for route-like endpoints in crossRegionService.js, where it samples points between coordinate pairs to detect boundary changes during cross-region composition. But the gateway still relies on heuristics to stay efficient.
That is the right call for this stage of the product. Perfect geographic reasoning is expensive. The real question is whether the cheaper method is good enough for the requests you actually serve.
What I would improve
I would make this heuristic observable. If you want to trust sampling in production, you should measure where it is ambiguous.
Specifically, I would add:
- counts for how many coordinates were extracted versus how many were sampled
- cache hit rates for region lookups
- error rates or fallback frequency correlated with route size and shape
- maybe a shadow mode that occasionally evaluates a fuller lookup set for comparison
That last one is especially useful because heuristics get dangerous when they are silently wrong. You want evidence that you are buying latency without giving away correctness.
The lesson
There is a mature kind of optimization that feels almost boring when you first read it. It does not involve new infrastructure or advanced math. It just cuts waste from the decision path.
That is what is happening here. The gateway stopped asking Photon questions it did not need answered. In systems that sit on top of external services, that kind of restraint is often the cheapest performance win you will ever ship.