Using Cloudflare's CF-IPCountry to Reduce Latencies by 300ms
optimization
cloudflare
TL;DR
I reduced the latency of users’ first requests by 300ms (from an average of 480ms to 180ms) by replacing blocking third-party Geolocation API calls with Cloudflare’s in-built header fields.
Intro
When users make their first request to NodeFlair, we retrieve their country to localize the website for languages (I18n) and features.
To do this, we make a request to a third-party geolocation API to determine their country.
The Issue
The API’s website stated an average latency of 50ms.
However, we were experiencing latencies of approximately 300ms. This could be due to our servers being based in Singapore.
Solution
Since we are currently using Cloudflare, we can instead obtain the country code via CF-IPCountry
in the request header fields. We only fall back to using the geolocation API if the value is invalid (which is rare).
This means that for almost all requests, we no longer have to make a call to the geolocation API, thus shaving off up to 300ms from the response time.
Since requests are processed much quicker, the servers can now handle 4-10 requests every second instead of 2-3 requests, significantly increasing the application’s throughput by up to 5x.
A little about what I do at NodeFlair…
The world today runs on code written by developers that solve the world’s problems and impact lives.
Now, imagine a world where developers get to code at a place where they find purpose in their work. This meaning could translate into drive that pushes boundaries to solve more of the world’s problems.
That’s why at NodeFlair, we make it our mission to improve the world by empowering developers to code() at where they love.