I mean, you might spend 20 minutes more to set it up, but you are safe from having to rely on 3rd party service.
Anyway, kudos to coderholic for creating this and sharing the story.
I mean, you might spend 20 minutes more to set it up, but you are safe from having to rely on 3rd party service.
Anyway, kudos to coderholic for creating this and sharing the story.
- Database needs to be distributed to your servers
- Database can become out of date easily
- Database lookup requires going to local disk and having a relatively fast access path/cache for lookups
- In general, a local database requires a large amount of effort compared to just running a curl in your PHP code.
If you are actually going to use a database, the proper solution does not look like "put it on your webservers" anyway, it looks like "put it on a separate service with a fast caching layer" etc etc. So in other words, the proper solution to decouple yourself from a 3rd party API is to... build a 1st party API.
In other words, not a 20 minute job. For small shops, a quick curl during the page load is a 20 minute job.
docker run -p 8080:8080 -d fiorix/freegeoip curl localhost:8080/json/1.2.3.4
Connected to 35.165.108.15:443
HTTP/1.1 200 OK
Server: nginx/1.8.1
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Date: Sun, 02 Jul 2017 08:54:40 GMT
X-Content-Type-Options: nosniff
Connection: keep-alive
Body discarded
DNS Lookup TCP Connection TLS Handshake Server Processing Content Transfer
[ 4ms | 244ms | 518ms | 525ms | 0ms ]
| | | | |
namelookup:4ms | | | |
connect:248ms | | |
pretransfer:767ms | |
starttransfer:1292ms |
total:1292msIt’s great for checking something quickly, otherwise you have the same thing in browser’s dev tools.
(Not to mention with very minimal effort you can usually avoid the majority of the specific tax of latency you mention, by doing things like parallelizing the request with other work or doing it asynchronously to the user's interface.)
Running a Docker container in production is not a 20 minute job.
Was just an extra step in our build pipeline.
In this case the geoIP database is like a 1MB CSV file, provided for free to the entire world by maxmind (a major network provider).
You can put give that file to many servers like nginx/apache out of the box, they will start adding a header with the country and the city of the client.
What this service is doing is effectively looking up the ip block in that csv file and returning the result as JSON.
[1] https://www.google.com/search?q=geoip+download
[2] https://dev.maxmind.com/geoip/legacy/geolite/
[3] https://dev.maxmind.com/geoip/legacy/downloadable/