Skip to main content

Module crc

Module crc 

Source
Expand description

CRC16 for slot placement, CRC32C for integrity, CRC64 for the file format.

Three different polynomials for three different jobs. CRC16 is Redis’s XMODEM variant and it exists here because slot = crc16(key) & 0x3FFF is how a key reaches a shard (04 section 1). Getting it wrong does not corrupt anything, it just makes us incompatible with every Redis cluster client, so the hash tag rules are implemented here too.

CRC32C is Castagnoli, the same polynomial SSE4.2 and the ARM CRC extension implement in hardware, and it is what guards pages and superblocks (07).

CRC64 is the Jones polynomial and it is here for one reason only: it is the eight bytes on the end of an RDB payload, so DUMP cannot produce something a real Redis will accept and RESTORE cannot reject a corrupt payload without it. Nothing else in the engine uses it, and nothing else should, because CRC32C has hardware behind it and this does not.

Constants§

SLOT_COUNT
The number of slots, which is Redis’s 16384 and is not configurable.

Functions§

crc16
Redis’s CRC16, the XMODEM variant.
crc64
CRC64 over data, continuing from crc. Start with 0.
crc32c
CRC32C over data, continuing from crc. Start with 0.
hash_tag
The part of a key that decides its slot.
slot_of
The cluster slot a key belongs to, hash tags included.