A simple IP address lookup API, with high performance in mind. It uses pre-collected data and calculates hostname on the fly.
It requires Node.js v16, MongoDB v6, and Redis v6 or higher.
- Run
npm install
- Grab and configure environment variable from
.env
file - Start mongo db
- Start
redis-server
for data caching - Start
redis-server --port 6380
for stats collection - Run
node src/loader.js
to seed data from csv file npm start
to launch
Enter a testing IPv4 address in the {ipaddress}
placeholder to get information.
curl --location --request GET 'http://localhost:3001/api/lookup/{ipaddress}'
The above request gets a response like
{
"city": "Washington",
"region": "Washington, D.C.",
"country": "US",
"latitude": 28.89511,
"longitude": -70.03137,
"postal_code": "20004",
"timezone": "America/New_York",
"hostname": "example.net"
}
Get usage stats with the following endpoint
curl --location --request GET 'http://localhost:3001/api/stats'
The above request gets a response like
{
"usage": 21330
}
src/loader.js
is a utility that seeds data from user provided csv file, to the database in batches.
src/loader.js
currently performs inserts only, it can be modified to perform upsert operations during seeding process so that older data can be updated. At this time a hash of start and end IP address can be calculated and indexed, later when performing upsert, existing rows can be identified using their hash, and their values can be updated, it will seamlessly update the data while the requests are being served by caching layer.
There are two levels of caching, first one using Redis that can be shared across multiple instances of the API, and the other caching happens within the server memory to reduce latency under high stress. Server-side cache lives about 180s, and Redis cache for about 300s, so a key invalidates in 480s, and fresh data can be pulled from the database.