IP geolocation detection works through well-known databases that map IP ranges to countries. For example:
- an IP range of 220.127.116.11 to 18.104.22.168, means that it came from Ireland.
- an IP range of 22.214.171.124 to 126.96.36.199 means that it came from Jordan.
- an IP range of 188.8.131.52 to 184.108.40.206 means that it came from Qatar.
and so on.
The problem with using a database lies in performance. Each request to view your website will generate a request to the IP database, to determine the country.
If you get 5 requests per second, this means 5 queries to your IP database, to determine the country.
Therefore, you will need to rely on a very fast, highly optimized, database engine to carry the load of IP-to-country queries.
This database must not be your website's database.
And therefore, for faster performance, and to not overwhelm the site's servers with IP-to-countries queries for each page view, we can have the following implementation options:
Through a Third-party
Having the detection from a third-party is easier to setup, and requires you to pay for the service of the third-party only. No extra setup or maintenance is required from you.
- CloudFlare IP Geolocation: CloudFlare applies a layer between the visitor and the servers of your website. CloudFlare supports GeoIP location detection and is embedded in that layer without any extra effort. Your website must be using CloudFlare CDN and "IP Geolocation" option must be enabled at your CloudFlare settings.
CloudFlare's Pro subscription requires your DNS zone file to be managed from CloudFlare which is not a good option for some organizations.
See pricing here: https://www.cloudflare.
- MaxMind GeoIP2 Precision Web Services: MaxMind provides reliable GeoIP detection. By using the paid service (not the database), the load is managed by MaxMind's servers and not your site. A user ID and license key is required here. You will need to buy one of their services and they will provide you the login details. You can view your user ID and license key inside your MaxMind account.
See pricing here: https://www.maxmind.com/
If you opt-in for Country-level detection (costs $0.0001 per query), and your site gets 200,000 visits per month, you'll need to pay $20/month.
Through Homegrown Solution (Using Databases)
You can use the available databases (MaxMind, ip2c.org, or IP2Location) to build your own detection. However, a high-performance setup is required here to handle high traffic. Al, o you should consider the maintenance costs, and manual database updating (which usually needs to be updated every week).
Assuming that you have access to your servers (a.k.a hosting with a VPS or Dedicated Servers), this setup can be done through:
- Using Same Servers or Reverse Proxy: You can install MaxMind DB Apache Module, which allows for IP-to-location queries to MaxMind database from Apache, thus getting the information faster from querying your site's database or application.
If you're looking for faster time to market, and fewer maintenance overheads, go with one of the third-party implementations. You'll get more reliable and accurate service.
The homegrown solution will provide a cheaper solution but will be at the expense of maintaining it. That is of course if you have the ability to perform this setup.