Inferensys

Glossary

Locality-Sensitive Hashing (LSH)

An algorithmic technique that hashes similar input items into the same buckets with high probability, enabling efficient approximate nearest neighbor search in high-dimensional spaces.
Developer reviewing semantic search engine results on laptop, relevance scores visible, technical search demo.
APPROXIMATE NEAREST NEIGHBOR SEARCH

What is Locality-Sensitive Hashing (LSH)?

Locality-Sensitive Hashing (LSH) is an algorithmic technique that hashes similar input items into the same buckets with high probability, enabling efficient approximate nearest neighbor search in high-dimensional spaces.

Locality-Sensitive Hashing (LSH) is a probabilistic dimensionality reduction method that uses a family of hash functions to map high-dimensional data points into compact binary codes, or buckets. The core property is that the probability of collision is high for points that are close in the original space and low for points that are far apart. This contrasts with conventional cryptographic hashing, where a tiny input change produces a radically different hash. LSH trades perfect accuracy for dramatic speed gains, reducing the complexity of a nearest neighbor search from linear to sub-linear time.

Common LSH families are tailored to specific distance metrics. For Jaccard similarity between sets, the MinHash scheme is used. For cosine similarity between vectors, a random hyperplane projection generates a binary bit. For Euclidean distance, projections onto random lines with a fixed bucket width are employed. These generated hashes are typically stored in multiple hash tables to boost recall. LSH is a foundational component in scalable vector database indexing, powering deduplication pipelines, recommendation systems, and the efficient retrieval phase of Retrieval-Augmented Generation (RAG) architectures.

CORE MECHANISMS

Key Features of LSH

Locality-Sensitive Hashing transforms high-dimensional data into compact signatures where similarity is preserved. These core features define how LSH enables efficient approximate nearest neighbor search at scale.

01

Hash Collision Probability

The defining property of LSH: similar items collide in the same bucket with high probability, while dissimilar items are mapped to different buckets. This is formalized through a distance-sensitive family of hash functions where the collision probability Pr[h(x) = h(y)] = sim(x, y). For cosine similarity, random hyperplane projections achieve this; for Jaccard similarity, MinHash provides the guarantee. The probability curve creates a sharp threshold—items above a similarity cutoff almost certainly collide, while those below almost never do.

02

Amplification via AND-OR Construction

LSH controls precision and recall through banding techniques that combine multiple hash functions:

  • AND construction (r rows): Concatenating r hash values into a single signature reduces false positives by requiring agreement across all r functions
  • OR construction (b bands): Using b independent hash tables reduces false negatives by allowing a match in any band
  • The S-curve effect: The AND-OR composition creates a steep sigmoid probability curve, giving near-certain collision above a tunable similarity threshold and near-zero collision below it
03

Sublinear Query Time

LSH achieves O(n^ρ) query time where ρ < 1, dramatically outperforming brute-force O(n) linear scans. For a dataset of 1 billion vectors, an LSH index with ρ = 0.5 reduces comparisons from 1 billion to approximately 31,600. The parameter ρ is determined by the hash family's sensitivity and the desired similarity threshold. This sublinear scaling is what makes LSH viable for billion-scale nearest neighbor search in production systems like image retrieval and recommendation engines.

04

Distance Metric Specialization

LSH families are designed for specific distance metrics, each with distinct hash constructions:

  • Cosine similarity: Random projection using a random hyperplane; the sign of the dot product determines the hash bit
  • Jaccard similarity: MinHash uses the minimum hash value of set elements under random permutations
  • Euclidean distance (L2): p-stable distributions (e.g., Gaussian) project points onto random lines, then quantize into buckets
  • Hamming distance: Bit sampling directly selects random bit positions
  • Earth Mover's Distance: Specialized embeddings for distributional similarity
05

Locality-Sensitive Families

A hash family H is (d₁, d₂, p₁, p₂)-sensitive if for any two points:

  • If d(x, y) ≤ d₁, then Pr[h(x) = h(y)] ≥ p₁
  • If d(x, y) ≥ d₂, then Pr[h(x) = h(y)] ≤ p₂ Where p₁ > p₂ and d₁ < d₂. This formal guarantee ensures a gap between the collision probability of close and far points. The amplification gap (p₁ vs p₂) determines how effectively AND-OR construction can separate similar from dissimilar pairs, with larger gaps enabling more efficient indexes.
06

Candidate Pair Filtering

LSH functions as a pre-filter for exact distance computation, not a replacement. The workflow:

  1. Hash the query point into all LSH tables
  2. Retrieve all items that share at least one bucket with the query (candidate pairs)
  3. Compute exact distances only on candidates, typically reducing comparisons by 99% or more
  4. Return the k-nearest verified neighbors This two-phase approach guarantees exact results on the filtered set while avoiding the prohibitive cost of exhaustive comparison across the entire dataset.
LOCALITY-SENSITIVE HASHING

Frequently Asked Questions

Clear, technically precise answers to the most common questions about locality-sensitive hashing, its mechanisms, and its role in modern approximate nearest neighbor search.

Locality-sensitive hashing (LSH) is an algorithmic technique that hashes similar input items into the same buckets with high probability, enabling efficient approximate nearest neighbor search in high-dimensional spaces. Unlike traditional cryptographic hashes where a single bit change produces a completely different output, LSH functions are designed so that the collision probability between two points increases with their similarity. The core mechanism involves projecting data points through multiple randomized hash functions that partition the space. Points that are close in the original space will likely fall on the same side of each random partition, generating identical hash codes. By using multiple hash tables (an amplification technique), LSH reduces false negatives while maintaining sub-linear query time. Common LSH families include MinHash for Jaccard similarity on sets, SimHash for cosine similarity on vectors, and p-stable distributions for Euclidean distance. This makes LSH a foundational technique for deduplication, near-duplicate detection, and large-scale similarity search in systems like FAISS and content fingerprinting platforms.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.