Skip to content

Talk//Programmatic DIB

Resolving a location from an IP address in Java

Turning an IP address into a country, region and city is a routine requirement for analytics, fraud checks and localisation. Doing it against a local GeoIP database rather than a hosted API keeps the lookup fast, free per request, and keeps user IP addresses inside your own infrastructure.

Published
2020-05-27
Channel
Programmatic DIB
Topics
Java · GeoIP · Backend

Why the lookup is a database problem

IP geolocation is a range lookup. Address blocks are allocated to organisations and regions, and a GeoIP database is essentially a sorted mapping from address ranges to location records. Resolving an address means finding the block that contains it.

Because it is a local lookup rather than a network call, it is fast enough to run inline on a request without thinking about it — which is not true of an API round-trip.

Why not just call a hosted API

Hosted geolocation APIs are easy to adopt and awkward to live with. They put a per-request cost on something that should be nearly free, add a network hop to a hot path, introduce a third-party dependency into your availability story, and quietly turn every user's IP address into data you are sending elsewhere.

That last point is the one that tends to matter most in review. An IP address is personal data in several jurisdictions, and shipping it to a vendor on every request is a decision worth making deliberately rather than by default.

Accuracy expectations

Country-level resolution is generally reliable. City-level is approximate and degrades badly for mobile networks, corporate egress and anyone behind a VPN. Any product decision built on top of it should be tolerant of being wrong — geolocation is a hint, not an identity.

§KKey points
  • A GeoIP lookup is a local range query, fast enough to run inline.
  • Self-hosting avoids per-request cost, a network hop and a data-sharing decision.
  • Country-level accuracy is good; city-level is approximate.
Java IP geolocationget location from IP address JavaGeoIP database lookupself hosted IP geolocation