data-structures - Redis Hyperloglog - PFCOUNT 副作用

标签 data-structures redis hyperloglog

Redis 最近发布了名为 HyperLogLog 的新数据结构。它允许我们保留唯一对象的数量,并且只占用 12k 字节的大小。我不明白的是Redis的PFCOUNT命令在技术上说是写命令。为什么会这样?

Note: as a side effect of calling this function, it is possible that the HyperLogLog is modified, since the last 8 bytes encode the latest computed cardinality for caching purposes. So PFCOUNT is technically a write command.

最佳答案

HyperLogLog对象的头部如下:

struct hllhdr {
    char magic[4];      /* "HYLL" */
    uint8_t encoding;   /* HLL_DENSE or HLL_SPARSE. */
    uint8_t notused[3]; /* Reserved for future use, must be zero. */
    uint8_t card[8];    /* Cached cardinality, little endian. */
    uint8_t registers[]; /* Data bytes. */
};

注意 card 字段:它包含算法评估的最后一个基数。计算基数的估计值是一项昂贵的操作,因此 Redis 会缓存该值并将其保存在该字段中。

当 PFADD 被调用时,HyperLogLog 对象可能会被更新,也可能不会被更新(很有可能不会)。如果未更新,调用 PFCOUNT 将重用缓存值(卡字段)。如果更新了,则card字段失效,所以下一个PFCOUNT会执行计数算法,将新值写入card字段。

这就是 PFCOUNT 可以改变 HyperLogLog 对象的原因。

关于data-structures - Redis Hyperloglog - PFCOUNT 副作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23164374/

相关文章:

c++ - 可以realloc Array,那为什么要用指针呢?

redis - Webflux,使用 Websocket 如何防止订阅两次 reactive redis 消息传递操作

python - 无法检查 celery 队列

cassandra - 如何存储唯一的 "Likes"或 "Views"或按比例设置?

algorithm - 是否有可能对 hyperloglog 进行重复数据删除,以便添加和删除元素会产生相对正确的唯一计数?

algorithm - 序列平均值的高效数据结构

kotlin - 根据属性对象将列表的属性合并到另一个列表

data-structures - 绳子有没有平衡条件?

php - 为 Laravel 7 安装和配置 PhpRedis