Skip to content

Instantly share code, notes, and snippets.

@maciej
Last active March 30, 2023 19:03
Show Gist options
  • Save maciej/ecec311a6665fa5e2da13dc44a46edfb to your computer and use it in GitHub Desktop.
Save maciej/ecec311a6665fa5e2da13dc44a46edfb to your computer and use it in GitHub Desktop.
Redis HLL size

Redis HLL has a 16 bytes header:

  • 8 bytes are used to cache the set cardinality
  • 3 are not used
  • 4 are used to store the magic "HYLL"
  • 1 is used to represent the type of the set (sparse or dense)
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. */
};

https://github.com/antirez/redis/blob/unstable/src/hyperloglog.c#L182-L188

A set with 1 element takes 21 bytes of memory:

127.0.0.1:6379> PFADD foo bar
(integer) 1
127.0.0.1:6379> STRLEN foo
(integer) 21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment