paddle42380
paddle42380

Reputation: 7081

Uuids and Redis

Antirez talks about memory optimisation with Hashes at length in this article - http://redis.io/topics/memory-optimization

I have an app in production which has around 50 entities (tables) and a few million uuids, combined across various entities. I am looking to leverage Redis' Sorted Set, Lists and Hashes heavily.

What are the implications of having uuids/guids as redis keys ( and members of sets and lists), if any, from a memory and performance standpoint?

I am using postgre as another datastore and rails 3.2.9 and ruby 1.9.3.

Upvotes: 4

Views: 6193

Answers (1)

Didier Spezia
Didier Spezia

Reputation: 73266

If you use sorted sets, lists and hashes, there is no specific implication.

You can store your uuids as strings such as:

 110E8400-E29B-11D4-A716-446655440000

In that case it will take 38 bytes per value. If you do not care about storing human readable values, you may prefer to leverage Redis ability to store binary data (for both keys and values), and store the uuids as 16 bytes only.

Your short lists, sorted sets and hashes will be serialized as explained in Redis documentation. You may want to tweak the following parameters to adjust the Redis behavior to your workload:

# Hashes are encoded using a memory efficient data structure when they have a
# small number of entries, and the biggest entry does not exceed a given
# threshold. These thresholds can be configured using the following directives.
hash-max-ziplist-entries 512
hash-max-ziplist-value 64

# Similarly to hashes, small lists are also encoded in a special way in order
# to save a lot of space. The special representation is only used when
# you are under the following limits:
list-max-ziplist-entries 512
list-max-ziplist-value 64

# Sets have a special encoding in just one case: when a set is composed
# of just strings that happens to be integers in radix 10 in the range
# of 64 bit signed integers.
# The following configuration setting sets the limit in the size of the
# set in order to use this special memory saving encoding.
set-max-intset-entries 512

# Similarly to hashes and lists, sorted sets are also specially encoded in
# order to save a lot of space. This encoding is only used when the length and
# elements of a sorted set are below the following limits:
zset-max-ziplist-entries 128
zset-max-ziplist-value 64

Now if you plan to use sets (simple sets, not sorted sets), there is a specific optimization called intset that you will not benefit from if you use UUIDs as keys. An intset can be only used to encode 64 bits numbers, so 16 bytes UUIDs will not fit. If you plan to store a lot of sets, there may be a benefit to add an indirection, and use integers as primary keys instead of UUIDs. Otherwise it is pointless.

Upvotes: 7

Related Questions