Every consistent hash ring starts with a hash function that turns a key into a number, the same number every time. Today you build that function - a small, fully specified FNV-1a hash - so that every position on the ring later in the project is a value you can pin down exactly.
Turn a string key into a deterministic 32-bit number with FNV-1a.
Consistent hashing is built on one simple guarantee: a key always lands in the same place. That guarantee comes entirely from the hash function, so we pin it down first. We use FNV-1a, a tiny non-cryptographic hash that needs no library: start from a fixed offset basis, and for each byte of the key, XOR the byte into the running value and multiply by a fixed prime. The multiply is 32-bit and wraps around on overflow, which is what mixes the bits.
Real systems reach for SHA-1 or MD5 here, and the ring you build works with any of
them - it only cares that the hash is deterministic and well spread. We pick FNV-1a
because it is short enough to hold in your head and gives numbers you can reproduce by
hand, which matters when every later lesson asserts an exact position. The empty-string
case is the cleanest check that you started from the right basis: with no bytes to fold
in, Hash("") is the offset basis itself.
// FNV-1a: start from the basis, fold in one byte at a time.func Hash(s string) uint32 {h := uint32(2166136261)for i := 0; i < len(s); i++ {h ^= uint32(s[i]) // XOR the byte inh *= 16777619 // then multiply by the prime (wraps at 2^32)}return h}