garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/notes/geocoding-latency.md

raw


topic: geocoding triggers: [geo_nav.rs, nominatim, platform/http.rs, "search bar", "slow lookup"] updated: 2026-07-31

Why a Nominatim lookup is slow, and where the time actually goes

The self-hosted instance answers a warm query in 10–60 ms and a cold one in 4–10 s. 10 s is Nominatim's own NOMINATIM_QUERY_TIMEOUT, and it does not fail quietly: the endpoint returns HTTP 503 with the plain-text body Query took too long to process., which ureq raises as an error, so the search bar shows a ten-second spinner and then "Search failed: …". A city name is almost always warm; a street + city ("Beatrixlaan Rijswijk") is the shape that goes cold.

It is storage latency, not the query

Measured 2026-07-30 on garage44.eu, dd … iflag=direct, depth 1:

8 KB readssequential
/ — sda, local NVMe67 MB/s (~8,000 IOPS)807 MB/s
/data — sdb, OVH network block volume1.9 MB/s (~240 IOPS)44 MB/s

A Nominatim search bitmap-scans search_name and touches ~1,000–5,000 random 8 KB pages. The same statement, same parameters, ran 4.1 s cold and 15 ms warm with Buffers: shared hit=4919, read=0 — 4,900 random reads at ~0.8–3.4 ms each is the whole of the difference. So the fix is to keep the working set off that volume and in RAM, which is why the PGDATA is a bind mount on the root NVMe (/srv/nominatim/pgdata) and shared_buffers is 8 GB rather than the image's 2 GB. Docker's data-root is still /data/docker, so a named volume cannot land on the fast disk — it has to be a bind mount.

Things that look like the cause and are not

jit = off (also upstream's recommendation) is set via ALTER DATABASE and kept.

The client's own budget

Once the server was fixed the client became the larger half of its own latency. Warm is ~70 ms end to end, of which ~50 ms is Nominatim's Python frontend, not the database — that is the floor, and no amount of Postgres tuning moves it. Against that, geo_nav.rs was spending 120 ms of debounce plus up to 150 ms of rate-limit floor. Now 60 ms and 40 ms; the public-API branch keeps its 1 s, which is policy rather than tuning.

Two structural things mattered more than the constants:

What was not deliberate: ureq::get() builds a fresh Agent, and therefore an empty connection pool, on every call. Each lookup paid a full TCP + TLS handshake — conn=0.041 tls=0.094 ttfb=0.162 against nominatim.cartopolis.org, i.e. ~90 ms of a ~160 ms request. platform::http now holds one process-wide pooled agent. Verified with a standalone ureq 2.12 binary using the same AgentBuilder: five rapid requests then five spaced by up to 125 s idle, all reusing the connection (conn=0.000 tls=0.000), none failing. A pooled agent going stale over an idle gap was the obvious suspect when search briefly broke; it is not what happens.

Unrelated to speed, and worth knowing before "the search is broken" gets filed: Nominatim does no prefix matching at all, and it is not an autocomplete engine. The ICU tokenizer stores complete word tokens in the word table and the query analyzer looks them up by exact equality, so a half-typed name matches no rows and the endpoint returns [] — which the Search panel renders, correctly, as "No results". Leeuward, Groning and Utrech all return nothing; the full names return 2, 3 and 3 hits.

The near-misses that do answer are coincidences, not partial matching, and they are what makes this look like an intermittent bug (measured 2026-07-31 against select word_token, type, word from word …):

queryhitswhy
Amsterda1amsterda is a real token — the Portuguese exonym Amsterdã
Groninge1groninge is a real place name
Leeuwarde0no such word exists

Fixing it means a second engine, not a Nominatim setting: Photon is the purpose-built typeahead front end for this data (country extracts are published, so an NL-only index is small), and it would sit beside Nominatim rather than replace it — reverse geocoding and structured lookup stay here. Until then the honest client-side mitigation is to keep the last non-empty list on screen while a prefix returns nothing, rather than flashing "No results" between keystrokes.