Inferensys

Glossary

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 sub-linear time approximate nearest neighbor search in high-dimensional vector spaces.
Engineer reviewing vector database search results on laptop, embeddings visualization on screen, home office coding session.
PROBABILISTIC DIMENSIONALITY REDUCTION

What is Locality-Sensitive Hashing (LSH)?

Locality-Sensitive Hashing (LSH) is an algorithmic technique that hashes high-dimensional data points so that similar items map to the same buckets with high probability, enabling efficient approximate nearest neighbor search.

Locality-Sensitive Hashing (LSH) is a probabilistic indexing strategy that uses a family of hash functions specifically engineered to maximize collisions for similar vectors while minimizing collisions for dissimilar ones. Unlike traditional cryptographic hashing where collisions indicate a failure, LSH deliberately induces collisions to partition the vector space, ensuring that nearest neighbors fall into the same bucket with high probability. This transforms the computationally expensive O(n) brute-force search into a sub-linear lookup constrained to a few relevant hash buckets.

The core mechanism relies on random projection hyperplanes or banding techniques that amplify the gap between high-similarity and low-similarity pairs. By employing multiple independent hash tables, LSH boosts recall—if a true neighbor misses a collision in one table, it is likely captured in another. While memory-intensive compared to graph-based indices like HNSW, LSH offers rigorous theoretical guarantees on query time and is particularly effective for high-dimensional sparse data and specific metrics like Jaccard similarity or cosine distance.

THE HASHING APPROACH TO ANN

Key Characteristics of LSH

Locality-Sensitive Hashing (LSH) is a family of algorithms that trade perfect accuracy for sub-linear query time by using specialized hash functions that maximize collisions for similar vectors.

01

The Core Principle: Hash Collisions as Proximity

Unlike traditional cryptographic hashing where collisions are avoided, LSH maximizes the probability of collision for vectors that are close in the input space. A family of hash functions H is called (R, cR, P1, P2)-sensitive if for any two points p and q:

  • If distance(p, q) ≤ R, then Pr[h(p) = h(q)] ≥ P1
  • If distance(p, q) ≥ cR, then Pr[h(p) = h(q)] ≤ P2 This creates a locality-preserving mapping where similar items fall into the same bucket with high probability, enabling sub-linear search by only examining colliding items.
02

Amplification: AND-OR Construction

A single LSH function has limited discriminative power. To achieve practical precision and recall, LSH uses amplification through two Boolean compositions:

  • AND-construction (banding): Concatenate k independent hash functions into a single hash key. This sharpens the gap between P1 and P2, reducing false positives but also lowering collision probability.
  • OR-construction (multiple tables): Build L independent hash tables. A candidate is any vector that collides in at least one table. This boosts recall by providing multiple collision opportunities. The AND-then-OR scheme (k hash functions per table, L tables) provides the classic tunable tradeoff between precision and recall.
03

Hash Families by Distance Metric

LSH requires a hash family specifically designed for the target distance metric:

  • Cosine Similarity / Angular Distance: Use random hyperplane LSH (SimHash). A random vector r defines a hyperplane; the hash bit is the sign of the dot product with r. The probability of collision is 1 - (θ/π), where θ is the angle between vectors.
  • Euclidean Distance (L2): Use p-stable distributions (e.g., Gaussian). Project onto a random line and quantize into buckets of width w. Collision probability decreases with distance.
  • Jaccard Similarity: Use MinHash. Apply a random permutation to the set elements; the minimum element under that permutation is the hash value. Collision probability equals the Jaccard index.
  • Maximum Inner Product Search (MIPS): Requires asymmetric transformations like Simple-LSH or Range-LSH to convert inner product into a metric distance before hashing.
04

Query Execution: Candidate Generation and Reranking

LSH querying follows a two-phase pipeline:

  1. Bucket Retrieval: Hash the query vector using the same k × L functions. Retrieve all vectors from the matching buckets across all L hash tables. This candidate set is typically a tiny fraction of the total dataset.
  2. Reranking: Compute the exact distance between the query and each candidate vector. Sort by true distance and return the top-K. The candidate set size is the critical runtime factor. Too few candidates (high k, low L) hurts recall; too many (low k, high L) approaches brute-force cost. Parameter tuning is data-dependent.
05

Advantages and Limitations

Strengths:

  • Theoretically grounded: Provides probabilistic guarantees on recall given parameter choices.
  • Data-agnostic: No training phase required; hash functions are randomly generated independent of data distribution.
  • Incremental updates: Inserting new vectors requires only hashing and appending to buckets; no global index rebuild.
  • Distributable: Hash tables naturally partition across machines by bucket ID.

Limitations:

  • Memory overhead: Storing L complete hash tables multiplies storage requirements.
  • Curse of dimensionality: As dimensionality grows, the gap between P1 and P2 shrinks, requiring exponentially more tables to maintain recall.
  • Parameter sensitivity: Choosing k and L requires careful tuning; poor choices lead to either missed neighbors or excessive candidate sets.
  • Outperformed by graph methods: On many real-world benchmarks, HNSW and similar graph-based indices achieve better recall-speed tradeoffs.
06

LSH in Modern Vector Databases

While pure LSH has been largely superseded by graph-based indices (HNSW) and clustering methods (IVF) in modern vector databases, its principles remain influential:

  • FAISS includes IndexLSH for cosine similarity using random rotation and scalar quantization.
  • Annoy (Spotify) uses random projections to build a forest of binary trees, conceptually similar to LSH's random hyperplane approach.
  • Datasketch libraries use MinHash LSH for large-scale document deduplication and near-duplicate detection.
  • Hybrid systems sometimes use LSH as a coarse filter before applying a more expensive graph search, combining the data-agnostic nature of LSH with the precision of graph traversal. LSH remains valuable for streaming data, decentralized settings, and applications where index build time must be near-zero.
ALGORITHM COMPARISON

LSH vs. Other ANN Algorithms

Comparing Locality-Sensitive Hashing against graph-based, clustering-based, and quantization-based approximate nearest neighbor search methods across key operational dimensions.

FeatureLSHHNSWIVF-PQScaNN

Index Structure

Hash tables

Multi-layer proximity graph

Voronoi cells + codebooks

Anisotropic vector quantization

Query Time Complexity

Sub-linear O(1) lookups

Logarithmic O(log N)

Sub-linear O(√N)

Sub-linear O(√N)

Memory Overhead

High (multiple hash tables)

High (graph edges + vectors)

Low (compressed codes)

Low (quantized vectors)

Recall@10 (768-d)

85-92%

95-99%

90-95%

93-97%

Index Build Time

Fast (minutes)

Slow (hours)

Medium (tens of minutes)

Medium (tens of minutes)

Incremental Insertion

Filtered Search Support

Theoretical Guarantee

Probability of collision proportional to similarity

LOCALITY-SENSITIVE HASHING

Frequently Asked Questions

Clear, technical answers to the most common questions about Locality-Sensitive Hashing (LSH), its mechanisms, and its role in modern approximate nearest neighbor search pipelines.

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 hash functions that aim to minimize collisions, LSH functions are specifically designed to maximize collisions for vectors that are close together under a chosen distance metric. The core mechanism involves projecting data points onto random vectors or hyperplanes and discretizing the results. For cosine similarity, a random hyperplane is generated, and the hash value is determined by which side of the hyperplane the vector falls on (0 or 1). By concatenating multiple such hash functions into a single hash table, the probability that two distant points collide becomes exponentially small, while nearby points remain likely to share a bucket. During query time, the query vector is hashed using the same functions, and only vectors in the colliding bucket are examined, drastically reducing the search space from the entire dataset to a tiny, high-probability candidate set.

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.