Fl4m3Ph03n1x

Fl4m3Ph03n1x

Background

We have a file that has lines with IP ranges like the following:

IP1,     IP2,      Country, Region
1.0.0.1, 1.0.0.10, pt,      l

We use this file to know where an IP belongs to. So, for example, If I am given the IP 1.0.0.9, because it is between IP1 and IP2, I know it is in Portugal, Lisbon.

As you may imagine, this file has millions of lines like this. We need an efficient way to get the location of an IP.

ETS

ETS tables are known for being fast. So one of my first reactions was: what if I put this file in an ETS table with keys / values and then use pattern matching to check if a given IP is within the range?

Problem

However one of my co-workers pointed that although this would be possible using select the complexity would be O(n) for worst case, meaning I would run the entire table until I pattern matched into the answer.

This is not acceptable, so I was wondering if there is a more efficient way of doing this using ETS tables.

Any suggestions?

Showing Posts 1 to 8

NobbZ

NobbZ

Compile into a module at runtime. Very bad load time and in general not good write times, but fast for read, constant time.

There are even packages abstracting those away for you.

idi527

idi527

There are even packages abstracting those away for you.

And there’s also Locus: Geolocation and ASN lookup of IP addresses

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This is true of select generally because the patterns can be arbitrarily complex. That doesn’t mean that a range check is necessarily O(n) worst case. I’d do some testing and see.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

We tried doing that but we ran out of atoms. When we increased the number of atoms, we ran out of RAM at compile time. We are still looking into it though…

Due to current company policies, this is not an option for us.

How would you test this exactly?

idi527

idi527

You can look into how Locus, for example, approached the problem and apply a similar solution … For compilation into a module at runtime you can look into foil or fastglobal, but I’d still go with locus’s approach instead.

NobbZ

NobbZ

How?

A single module, with a single function, this doesn’t seem to be much of atoms…

If though you want to have all IP addresses as atoms, well then there is not much we could do for you, if though you use tuple notation for them, it should be fine.

I’m sorry for you. But perhaps you might take a look at them and check how they do it? Alternatively there is the :persistent_term module in a recent enough OTP.

From all of what I’ve heard, it is meant to be used as a write once, read fast store of data.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

We are using multi-clause functions. Somehow, we ran out of atoms because of that. We are generating a module that has 10_000_000 functions. Admittedly this is something I plan on asking in another discussion, since this topic is supposed to be about ETS tables or similar alternatives.

Rather interesting but unfortunately the API doesn’t have support for range queries. Thanks for the effort though!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

As an aside, this seems like a situation where an interval tree could be useful.

Have a set of IPs or ranges you want to look up, benchmark how long it takes you to look them up on the ets table at various sizes. Plot. If it’s a line you’ve got O(n), if it’s an increasingly flat curve you’ve got O(log(n))

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews