Keys, Buckets and the Art of Controlled Collision
Imagine sorting letters into numbered mailboxes. A rule sends every address to one mailbox; if two addresses share a box, the sorter must handle a collision. A hash function does the same for data keys. Because there are often more possible keys than buckets, some collisions are unavoidable. The useful question is how likely a collision is when we choose the rule at random before seeing the keys.
For a small illustration, number four keys $0,1,2,3$ and use three buckets. The rule $h(x)=x\bmod 3$ sends them to $0,1,2,0$, so keys $0$ and $3$ collide. A different rule changes which pair collides. A hash family is a set of such rules; a seed chooses one. A guarantee describes the choice of seed for fixed distinct keys, rather than promising that every rule is collision-free. This distinction links the preceding chapter's probability bounds to practical streaming sketches: collisions create error, and the randomness assumptions determine whether we can bound its expectation, variance, or tail.
Hashing also supports distributing events, estimating similarity, and selecting samples. These uses ask for different properties. A rule suitable for low expected collisions may be unsuitable for estimating Jaccard similarity or resisting an attacker who sees its seed. Reproducibility means workers can intentionally share a seed and get the same answer; independent repetitions require different, independently chosen seeds.
A randomized hash family makes a promise about a randomly chosen function on fixed keys, not about one arbitrary function on every possible key set. State who chooses the keys, when the seed is revealed, and which collision or independence property the proof actually uses.
| Term | Meaning and exact example | Boundary or non-example | Question answered |
|---|---|---|---|
| Key and universe | $x\in U$; for example, customer ID $17$ in a declared ID space | Observed keys need not exhaust $U$ | What may arrive? |
| Hash function | $h:U\to B$; $h(x)=x\bmod3$ maps integers to three buckets | An unspecified “random hash” is not a family | Where is one key sent? |
| Collision | Distinct $x,y$ have $h(x)=h(y)$ | Repeated copies of the same key are not a distinct-key collision | Which keys interfere? |
| Family and seed | Pick $h$ from $\mathcal H$ using a stored seed | Reusing one seed is not a fresh draw | What randomness is averaged over? |
| Universal collision bound | For fixed $x\ne y$, $\Pr_h(h(x)=h(y))\le1/m$ | Does not ensure every fixed $h$ spreads every input set | How large is pairwise collision risk? |
| Reproducibility | Workers using the same family, encoding, and seed hash identically | Different versioned encodings can disagree despite equal seeds | Can summaries be merged consistently? |
Keys, Buckets and the Art of Controlled Collision
Use $h(x)=x\bmod3$ on keys $0,1,2,3$. Write the bucket sequence $0,1,2,0$ and list the sole distinct-key collision $(0,3)$. Now try $g(x)=(x+1)\bmod3$: the buckets become $1,2,0,1$, so the same pair still collides. Checkpoint: simply relabeling buckets need not change collision structure. Try $q(x)=(2x+1)\bmod3$ and verify the same pair again, since $0$ and $3$ are congruent modulo $3$. Reflection: why must a hash-family analysis specify the universe and family, instead of treating every change to a formula as fresh randomness?
The Independence Ladder
Fix a finite universe $U$ and bucket set $B$ of size $m$. Choose a random function $h$ from a family $\mathcal H$ by sampling a seed. Uniform marginals mean each fixed key's output is uniform on $B$. Pairwise independent outputs mean for every two distinct fixed keys $x,y$ and every $a,b\in B$, $\Pr(h(x)=a, h(y)=b)=1/m^2$. This implies uniform marginals and collision probability $1/m$. A universal family only needs $\Pr(h(x)=h(y))\le1/m$ for each distinct fixed pair; that weaker statement need not imply independent outputs. For $k$-wise independence, the corresponding joint uniformity holds for every set of up to $k$ distinct fixed keys. Full independence requires it for every finite set of keys, which can need far more stored randomness.
A concrete pairwise-independent family works over a prime field $\mathbb F_p$: choose $a,b$ independently and uniformly from $\mathbb F_p$, then set $h_{a,b}(x)=ax+b\pmod p$ for $x\in\mathbb F_p$. For distinct $x,y$, the map from $(a,b)$ to $(h(x),h(y))$ is bijective because $x-y\ne0$ has a multiplicative inverse modulo $p$. Thus each ordered pair of outputs has probability $1/p^2$. The seed selects one of $p^2$ functions and can be stored in $2\lceil\log_2p\rceil$ bits. That is a representation upper bound, not a claim about an arbitrary $m$-bucket reduction. Reducing these outputs modulo a nondivisor $m$ may spoil exact uniformity and the stated pairwise guarantee.
Other goals occupy different branches. Exact min-wise independence asks that for every nonempty fixed set $S$ and each $x\in S$, $\Pr(h(x)=\min_{y\in S}h(y))=1/|S|$, assuming ties are resolved by a specified rule; a uniformly random permutation has this property. Approximate min-wise families allow a quantified error, often with much shorter seeds. Tabulation hashing splits a key into characters and combines seeded table lookups; its exact independence and other useful properties depend on the construction, not its name alone. A pseudorandom generator expands a short seed into a longer deterministic sequence. Statistical limited independence is an information-theoretic property of selected outputs; computational pseudorandomness instead concerns what a bounded adversary can distinguish. Neither is automatically implied by the other.
flowchart TD
F["Choose a function from a family"] --> U["Uniform output per fixed key"]
F --> C["Universal: bounded pair collision"]
P["Pairwise independent outputs"] --> U
P --> C
K["k-wise independent outputs"] --> P
R["Fully random function"] --> K
F --> M["Min-wise property: ranking a set"]
F --> G["PRG: computational claim"]
classDef navy fill:#17365D,color:#fff,stroke:#17365D;
classDef teal fill:#147D84,color:#fff,stroke:#147D84;
classDef crimson fill:#C6284D,color:#fff,stroke:#C6284D;
classDef purple fill:#6C3BAA,color:#fff,stroke:#6C3BAA;
class F,R navy; class P,K,U,C teal; class M purple; class G crimson;
| Property | Exact scope | What it supports | What it does not imply |
|---|---|---|---|
| Uniform marginal | $\Pr(h(x)=a)=1/m$ for each fixed $x,a$ | Each fixed key has balanced output | Two keys need not be independent |
| Universal collision | $\Pr(h(x)=h(y))\le1/m$ for fixed $x\ne y$ | Expected interference bounds | Exact joint uniformity |
| Pairwise independent | $\Pr(h(x)=a,h(y)=b)=1/m^2$ for fixed distinct $x,y$ | Collision probability $1/m$ and some variance calculations | Three outputs are independent |
| $k$-wise independent | Any up to $k$ fixed distinct keys have uniform joint outputs | Arguments using at most $k$ output variables | Full independence on larger key sets |
| Exact min-wise permutation | For fixed nonempty $S$, each member equally likely to rank first | Idealized minimum-hash similarity analysis | Collision control alone suffices |
| Computational PRG | A specified efficient observer has small distinguishing advantage | Efficiency under a computational threat model | Information-theoretic $k$-wise independence |
Match the property to the proof step: pairwise output independence controls any fixed pair, while a concentration argument involving many keys may require higher independence or a separate theorem. In the affine prime-field family, distinct $x,y$ give a bijection from seeds $(a,b)$ to output pairs, which proves the pairwise claim precisely.
Collision Probabilities, Seeds and Adversaries
For a fixed set of $n$ distinct keys, let $C=\sum_{i\lt j}I_{\{h(x_i)=h(x_j)\}}$ count colliding pairs. If a family is universal into $m$ buckets, linearity of expectation gives $\mathbb E[C]\le\binom n2/m$. No independence between the indicators is needed for this first-moment claim. Markov then gives $\Pr(C\ge t)\le\mathbb E[C]/t$ for $t\gt 0$; at $t=1$ this is the familiar union-bound-style estimate for any collision. A variance calculation requires covariances of pair indicators: pairwise independence of key outputs alone does not automatically make all those indicators independent when pairs overlap.
For $p=7$ and fixed keys $1,2,3,4$, affine hashing has six distinct-key pairs, each colliding with probability $1/7$, so $\mathbb E[C]=6/7$. One sampled seed may yield $C=0$; the expectation is over all $49$ equally likely seeds. In fact $a=0$ makes all four keys collide, while any $a\ne0$ is a permutation of $\mathbb F_7$ and makes none collide. Therefore $C=6$ with probability $1/7$ and $C=0$ with probability $6/7$: its mean is indeed $6/7$. This striking example shows why an expected collision bound is not a strong tail bound. It also shows that forbidding $a=0$ changes the family: it removes those collisions but no longer gives uniform independent outputs for a fixed pair.
The randomness budget is part of the analysis. A full random function from $U$ to $m$ buckets can be specified with about $|U|\log_2m$ bits; the affine family over $\mathbb F_p$ needs two field elements. If independent rows or trials are required, each needs fresh independent seed material. Reusing one seed across trials creates correlated failures and defeats a median-amplification proof. A seeded PRG may reduce stored randomness, but then the guarantee must explicitly state whether it is computational or whether a construction preserves the limited independence the proof needs.
Finally, fix the order of choices. The universal guarantee is over a random seed for a fixed pair selected independently of that seed. After a seed is published, a party able to choose keys can search for colliders; a seed-dependent pair is outside the original quantifiers. For untrusted keys, use a defense justified under an explicit adaptive or computational threat model and test operational limits; a simple universal-family calculation is not a denial-of-service guarantee.
flowchart TD
A["Fix keys before sampling seed"] --> B["Universal pair collision bound"]
B --> C["Sum indicators by linearity"]
C --> D["Expected collision pairs"]
D --> E["Markov for a weak tail"]
D --> F{"Need a sharper tail?"}
F -->|Yes| G["Prove enough independence or structure"]
A --> H{"Are keys chosen after seed?"}
H -->|Yes| I["Original guarantee does not apply"]
classDef navy fill:#17365D,color:#fff,stroke:#17365D;
classDef teal fill:#147D84,color:#fff,stroke:#147D84;
classDef crimson fill:#C6284D,color:#fff,stroke:#C6284D;
classDef purple fill:#6C3BAA,color:#fff,stroke:#6C3BAA;
class A,B,C navy; class D,E,G teal; class I crimson; class F,H purple;
Collision Probabilities, Seeds and Adversaries
Given: $p=7$, fixed keys $S=\{1,2,3,4\}$, and $h_{a,b}(x)=ax+b\pmod7$ with $a,b$ independent uniform field elements. Checkpoint 1: there are $\binom42=6$ unordered distinct-key pairs; each pair collides with probability $1/7$. Checkpoint 2: by linearity, $\mathbb E[C]=6/7$. Trace: when $a=0$ all keys map to $b$, producing $C=6$; for any $a\ne0$ the map is injective, so $C=0$. Since seven values of $b$ accompany each $a$, $\Pr(a=0)=1/7$ and the trace verifies the expectation. Variation: let a client observe $a,b$ and then choose two congruent bucket targets or search a larger universe for colliders. Identify which “fixed keys before seed” assumption no longer holds. Conclusion: do not promote a collision expectation into a per-seed or adaptive guarantee.
| Analytical viewpoint | What it establishes | Premise and precision | Failure example |
|---|---|---|---|
| Collision indicator | $\mathbb E[I_{xy}]\le1/m$ | Fixed distinct $x,y$; universal family | Pair chosen after viewing seed |
| Linearity over pairs | $\mathbb E[C]\le\binom n2/m$ | Fixed distinct-key set; no indicator independence required | Mistaking repeated occurrences for distinct pairs |
| Markov | $\Pr(C\ge t)\le\mathbb E[C]/t$ | $C\ge0$, $t\gt 0$; may be loose | Reading the mean as a hard maximum |
| Pairwise output independence | Uniform joint distribution of two fixed hash values | Enough for pair properties, not generic high-order tails | Correlated three-way outputs |
| Independent repetitions | Median-style risk reduction under a per-run bound | Fresh independent seeds and same fixed target | Reuse one seed in every row |
| Public-seed threat model | No fixed-input collision promise against seed-aware chosen keys | Requires an adaptive or computational defense | Attacker floods one known bucket |
Hashing Across Distributed Event Pipelines
Approachable routing. A school assigns numbered lockers to four parcel IDs. A family drawn from $\mathbb F_7$ can spread four fixed IDs over seven lockers; a chosen seed $a=2,b=1$ maps IDs $1,2,3,4$ to $3,5,0,2$ with no collisions. The random-family statement concerns the probability before the assignment is chosen. After it is chosen, the clerk can inspect the actual load exactly.
Production telemetry. Suppose four regional workers consume $200{,}000$ events per second, keep separate compact summaries, and merge them hourly. If they hash the same user ID using different seeds or differently normalized bytes, their bucket indices do not refer to the same coordinates; adding counters produces a meaningless merged sketch. Specify key encoding, family/version, seed, bucket count, and hash independence required by the downstream estimator. If workers use the same seed, the results are reproducible and mergeable under an estimator whose merge algebra permits it; this sharing is intentional and must not be misdescribed as four independent trials. Measure throughput, seed storage, hot-bucket rates, and whether seed rotation requires rebuilding summaries.
Misleading and adversarial setting. A public API accepts user-chosen IDs, and an adversary can send many requests after learning the routing seed or observing hot partitions. A fixed-key universal bound gives no assurance about those chosen IDs. The operator needs an explicitly justified adversarial strategy, potentially a secret keyed hashing primitive, access controls, monitoring, and rotation policy. Likewise, using a fast noncryptographic family for deduplication does not make accidental hash collisions proof that two payloads are equal; a confirmatory comparison is needed if false merges are costly. The formal abstraction ignores data encoding, malicious selection, and operational costs until those are written into the contract.
Hashing Across Distributed Event Pipelines: Hashing, Independence & Pseudorandomness
Inputs: four keys $1,2,3,4$ and two regional workers. Worker A uses $h_{2,1}(x)=2x+1\pmod7$, producing bucket indices $(3,5,0,2)$. Worker B initially uses $h_{2,2}$, producing $(4,6,1,3)$. Model-selection step: if both workers count the same key identities in per-bucket arrays, ask whether coordinatewise addition preserves per-key meaning. Expected observation: no, because bucket $3$ refers to key $1$ in A and key $4$ in B for these inputs. Change B to the same family, seed, and encoding as A; now identical keys align. Success criterion: compute both arrays for event lists A: $(1,2,1)$ and B: $(1,4)$, explain the mismatch, then show that the aligned merged buckets have counts $3$ at bucket $3$, $1$ at bucket $5$, and $1$ at bucket $2$. State that collisions may still exist on other keys.
Hashing Across Distributed Event Pipelines: Hashing, Independence & Pseudorandomness
Scenario: four workers, $200{,}000$ events/s aggregate, hourly summary merges, externally supplied IDs, and a memory limit that makes a full random function impossible. Decision record: (1) Write the exact query and its proof's required property: fixed-key collision, pairwise variance, higher-order tail, or approximate min-wise ranking. (2) Record universe/encoding, family, seed length, bucket count, and serialization version; test cross-language bit-for-bit vectors. (3) Separate shared seeds used for merge compatibility from independently sampled seeds used for confidence amplification. (4) Threat-model whether an adversary can see outputs, infer seeds, or choose keys adaptively; do not claim security from universal hashing alone. (5) Measure throughput, collision-heavy workloads, memory including seed tables, rotation/replay costs, and offline error against ground truth. Acceptance: a written choice explains which theorem applies, how a worker verifies compatibility at merge time, and what happens when the adversarial premise fails.
Choose a Hash Family and Defend the Choice
An appropriate family is the smallest one whose proven property covers the required step of the streaming algorithm under the actual input model. It must also fit the seed, update-time, and merge budgets. The following exercises test whether the choice and its quantifiers can be defended independently of a particular implementation.
Practice: Choose a Hash Family and Defend the Choice
Tier 1 — Define. For a family with $m=8$ buckets, state the universal bound for two fixed distinct keys and the exact pairwise-independent joint probability of two specified outputs. Expected: at most $1/8$ for collision; $1/64$ for a specified ordered pair of outputs. Explain why the first claim alone does not prove the second.
Tier 2 — Derive. For five fixed distinct keys and a universal family into eight buckets, compute $\mathbb E[C]\le\binom52/8=10/8$. Hint: use an indicator for each unordered pair. Expected: the bound $1.25$ on expected colliding pairs, not on the number of occupied buckets or a per-seed maximum. Describe why no indicator independence was required.
Tier 3 — Verify a construction. Over $\mathbb F_7$, choose $a=2,b=1$ and hash keys $1,2,3,4$. Expected: $3,5,0,2$. Then solve $h(1)=3$ and $h(2)=5$ to recover $a=2,b=1$. Proof checkpoint: for arbitrary distinct $x,y$, subtraction gives $a=(h(x)-h(y))(x-y)^{-1}\pmod7$, and then $b=h(x)-ax$; the unique seed proves pairwise independence when seeds are uniformly drawn.
Tier 4 — Counterexample. Let $X,Y$ be independent fair bits and $Z=X\oplus Y$. Expected: every pair is independent, but $X\oplus Y\oplus Z=0$ always, so the triple is not independent. Identify the missing premise in a proof that multiplies three probabilities.
Tier 5 — Professional decision. Choose a family for (a) fixed-key bucket collision accounting, (b) minimum-hash Jaccard estimation, and (c) public untrusted keys. Artifacts: a three-row contract listing exact property, seed exposure, adversary, seed bytes, and evidence to collect. Rubric (10 points): definitions 2, collision derivation 2, construction proof 2, counterexample 2, threat and operations decision 2. Edge cases: seed reuse across “independent” trials, modulo reduction from $p$ to $m$, mixed encodings, and merging sketches with different seed versions. Stretch: calculate how a stronger tail requirement changes independence or replication costs.
Before committing to a hash family, name the fixed inputs and adversary, the exact collision or independence property the proof uses, the seed and bucket representation, and whether workers must share or separate seeds. This contract connects the preceding concentration tools to upcoming approximate counters, filters, and distinct-element sketches, each of which asks hashing to justify a different error claim.
Next in the Series
In Approximate Counting & Stream Sampling, we'll explore Count and sample streams with sublinear state..