Antirez在本文中详细讨论了Hashes的内存优化问题 - http://redis.io/topics/memory-optimization
我有一个生产中的应用程序,它有大约50个实体(表格)和几百万个uuids,它们组合在不同的实体中。我希望利用Redis'分类集,列表和散列很重。
从内存和性能的角度来看,将uuids / guids作为redis键(以及集合和列表的成员)(如果有的话)的含义是什么?
我使用postgre作为另一个数据存储区并使用rails 3.2.9和ruby 1.9.3。
答案 0 :(得分:5)
如果使用有序集,列表和散列,则没有具体含义。
您可以将您的uuids存储为字符串,例如:
110E8400-E29B-11D4-A716-446655440000
在这种情况下,每个值需要38个字节。如果您不关心存储人类可读值,您可能更愿意利用Redis功能存储二进制数据(对于键和值),并将uuids仅存储为16个字节。
您的短列表,有序集和哈希将按照Redis文档中的说明进行序列化。您可能需要调整以下参数以根据工作负载调整Redis行为:
# 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
现在,如果您计划使用集合(简单集合,而不是有序集合),则会有一个名为intset的特定优化,如果您将UUID用作密钥,则不会从中受益。 intset只能用于编码64位数字,因此16字节的UUID不适合。如果您计划存储大量集合,则添加间接可能有好处,并使用整数作为主键而不是UUID。否则就没有意义了。