Marco
Marco

Reputation: 4395

Redis: should I convert IPs into integers?

I need to store IP addresses in a redis hash.

Will there be considerable memory savings if the IP is stored as an integer instead of a string?

I would be using Ruby's IPAddr to convert the IP to an int.

Upvotes: 1

Views: 1064

Answers (3)

nyxthulhu
nyxthulhu

Reputation: 9752

You may wish to reconsider your approach a little as well, with the advent of IPv6 storing as an integer would be a very bad idea if you ever wish to convert your application to use IPv6 (or even just support it).

These days memory/disk space is cheap so you'd be better investing in a workable future proof solution than worry about disk space if you can.

In this case a string would still be the best option since you can then utilize IPv6 and IPv4 in the same field.

Upvotes: 2

Theo
Theo

Reputation: 132882

It depends on how you do it. In Redis keys and (leaf) values are strings. If you would convert an IP address to an int and send it to Redis like the following code you wouldn't save much:

redis.hset("xyz", "ip", IPAddr.new(ip).to_i)

The IP "255.255.255.255", for example, is 15 bytes in dotted quad form, its integer representation "4294967295" is ten bytes when saved as a string, which is what the code above will do.

To get down to just four bytes stored in Redis you would have to send the raw bytes "\xFF\xFF\xFF\xFF".

In Ruby you would do it this way:

packed_ip = IPAddr.new(ip).hton
redis.hset("xyz", "ip", packed_ip)

And then when you read it back

packed_ip = redis.hget("xyz", "ip")
ip = IPAddr.ntop(packed_ip)

What IPAddr.hton and IPAddr.ntop do is this:

packed_ip = ip.split('.').map(&:to_i).pack('C4') # hton
ip = packed_ip.unpack('C4').join('.') # ntop

Then there's the whole thing about IPv6 and whatnot, but I think IPAddr has you covered there.

Upvotes: 6

Listing
Listing

Reputation: 1201

If you store and ip as an integer it will use up 4 bytes. as a string "abc.def.ghi.jkl" it will use up 16 bytes when stored as a null-terminated ascii string, so it is at least a factor of 4 or even 7+ if stored as an unicode string. Also searching for integers is much faster than for strings because your processor is optimized to compare integers...

Upvotes: 0

Related Questions