Search code examples
maxmind

MaxMind: loading GeoIP2 mmdb into memory for fast read


I am using MaxMind's GeoIP2 to get the geo information for an IP address. In my Java web application, the

DatabaseReader reader = new DatabaseReader.Builder(new File("C:\GeoLite2-City.mmdb").withCache(new CHMCache()).build();

I am hoping to load the entire file into memory for efficient/fast read.

Is the way shown above the most efficient/fast way of using the mmdb database?


Solution

  • The code you pasted will memory-map the file and use the data cache. It should be efficient, but it will not load the whole database into memory. If you want to do that, you would need to load the database using the fileMode builder option, e.g.:

    DatabaseReader reader = new DatabaseReader
        .Builder(new File("C:\GeoLite2-City.mmdb")
        .fileMode(com.maxmind.db.Reader.FileMode.MEMORY)
        .withCache(new CHMCache())
        .build();
    

    However, in most cases, you will probably not see a performance difference between this and the memory-mapped file.