EBOOK – REDIS IN ACTION

This book covers the use of Redis, an in-memory database/data structure server.

open all | close all

5.3 IP-to-city and -country lookup

While we’ve been collecting statistics and logs in Redis, we’ve been gathering information
about visitor behavior in our system. But we’ve been ignoring one of the most
important parts of visitor behavior—where the visitor is coming from. In this section,
we’ll build a set of functions that we can use to parse an IP-to-location database, and
we’ll write a function to look up IP addresses to determine the visitor’s city, region
(state), and country. Let’s look at an example.

As visitors to Fake Game Company’s game have multiplied, players have been coming
from all over the world to visit and play. Though tools like Google Analytics have
helped Fake Game Company to understand which major countries their users are
from, they want to know cities and states to better understand their users. It’s our job
to use one of the IP address-to-city databases and combine it with Redis to discover the
locations of players.

We use Redis instead of a typical relational database because Redis will generally be
faster for this (and other) use cases. And we use Redis over local lookup tables
because the amount of information necessary to locate users is large enough to make
loading tables on application startup a relatively expensive operation. To start using
our lookup tables, we first need to load the tables into Redis.