Hashing

A row of numbered boxes, a key going in, and the same small sum choosing where it lands every time. Builds on Complexity & Big-O and Arrays & Strings. The same idea drives the hash index in Storage Engines and hash partitioning in Partitioning.
Before you start. You need arrays from Arrays & Strings: a numbered row of slots you can jump straight to. You also need O(1) and O(n) from Complexity & Big-O: work that stays flat no matter how much you store, versus work that grows with it.
A hash table finds a thing by working out where it has to be, instead of looking for it. Picture a wall of numbered post boxes where your name always works out to box 5. Nobody reads the wall from the left. They do the sum, walk to box 5, and your letter is there.
A records clerk in 1956. The new memory can jump to any address in one step, but jumping only helps if you know which address holds the record. So the clerk still searches, checking each record front to back, or halving a sorted file again and again. Either way the search gets slower as the file grows, and the files are growing.
- Work out which box a key lands in, by hand: ‘cat’ is 3941, on 8 boxes, so box 5.
- Say why two keys sharing a box is guaranteed, not a bug.
- Fetch a key back from a shared box, two ways.
- Read how full a table is and say when it should move to a bigger one.
- Choose a hash table or a tree for a job, from whether you need order.
Say it in 30 seconds
A hash table runs one small sum on the key and uses the answer as the box number, so a lookup is one jump, not a search, however much is stored. Two keys will sometimes land in one box, so each box keeps the key beside its value and has a rule for a second one: a short stack, or the next free box. Move to a bigger table before it passes about seven-tenths full and a lookup stays at about one look. What you give up is order.
- Hashes that keep secrets, a different job: Cryptography Foundations
- Lookups in order, ranges and the next largest: Trees (BST, balancing, heaps, tries)
- Spreading keys across many machines: Partitioning
- Building Redis’s own table, in Go: Redis: The Map (SET / GET / DEL)