IP geolocation detection works through well-known databases that map IP ranges to countries. For example:

  • an IP range of 5.83.240.0 to 5.83.255.255, means that it came from Ireland.
  • an IP range of 46.23.112.0 to 46.23.127.255 means that it came from Jordan.
  • an IP range of 86.36.0.0 to 86.37.255.255  means that it came from Qatar.

and so on.

Major and credible IP mapping databases are MaxMindip2c.org, and IP2Location.

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.

  1. 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.com/plans

 

  1. 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/en/geoip2-precision-services
    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 (MaxMindip2c.org, or IP2Location) to build your own detection. However, a high-performance setup is required here to handle high traffic. Also 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:

  1. 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.
  2. Setup a Node.js Server with MaxMind Database: You can install a pure JavaScript module for Geo IP lookup using MaxMind binary databases. This provides very fast look up using Node.js. Remember you'll need to maintain this setup, and keep updating the database manually.

 

Recommendation

If you're looking for faster time to market, and less maintenance overheads, go with one of the third-party implementations. You'll get more reliable and accurate service.

The homegrown solution will provide cheaper solution, but will be on the expense of maintaining it. That is of course of you have the ability to perform this setup.