Uuids 和 Redis

4
Antirez在这篇文章中详细介绍了使用哈希表进行内存优化的方法- http://redis.io/topics/memory-optimization
我正在运行一个应用程序,其中包含大约50个实体(表),以及分布在各种实体之间的数百万个uuid。我计划大量利用Redis的有序集合、列表和哈希表。
从内存和性能的角度来看,将uuids/guids作为Redis键(以及集合和列表的成员)有什么影响吗?
我正在使用postgre作为另一种数据存储方式,以及rails 3.2.9和ruby 1.9.3。
1个回答

7

如果您使用排序集、列表和哈希,没有具体的影响。

您可以将uuid作为字符串存储,例如:

 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。否则没有意义。


感谢 Didier。来自 Redis 文档(http://redis.io/commands/object)-“集合可以编码为 intset 或 hashtable。intset 是一种用于仅由整数组成的 小型 集合的特殊编码”。 - 你有没有大概了解“小型”意味着什么? - paddle42380
"small" 的定义由 set-max-intset-entries 参数确定。在默认配置下,少于 512 个项目的集合被视为小集合,并因此存储为 intsets。 - Didier Spezia
需要注意的是,只有集合中的元素需要是64位整数才能适应紧凑的集合。用于集合本身的主键可以是任何东西,包括UUID的字节或字符串表示形式。 - Mani Gandham

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接