Inferensys

Glossary

Locality-Sensitive Hashing (LSH)

Locality-Sensitive Hashing (LSH) is a family of hashing algorithms that map similar high-dimensional data points to the same hash buckets with high probability, enabling fast approximate similarity search.
Developer reviewing semantic search engine results on laptop, relevance scores visible, technical search demo.
RETRIEVAL LATENCY OPTIMIZATION

What is Locality-Sensitive Hashing (LSH)?

A core algorithmic technique for accelerating approximate nearest neighbor search in high-dimensional spaces.

Locality-Sensitive Hashing (LSH) is a probabilistic hashing technique designed so that similar input items (like high-dimensional vectors) map to the same hash bucket with high probability, while dissimilar items map to different buckets. This property enables approximate nearest neighbor (ANN) search by drastically reducing the candidate set—instead of comparing a query to every vector in a database, the system only computes distances for vectors within the same hash bucket. Unlike cryptographic hashing, which maximizes output differences, LSH functions are engineered to preserve similarity in the hash space, making them fundamental to low-latency retrieval in systems like RAG and recommendation engines.

The technique operates by using families of hash functions (e.g., based on random projections or stable distributions) and amplifying their power through AND- and OR-constructions. An AND-construction increases precision by requiring matches across multiple functions, while an OR-construction increases recall by checking multiple hash tables. This creates a tunable recall-latency trade-off. LSH is particularly effective for cosine similarity and Euclidean distance metrics. While it reduces search time from linear to sub-linear complexity, it is an approximate method, meaning results are probabilistic and not guaranteed to be the exact nearest neighbors, a calculated trade-off for performance.

RETRIEVAL LATENCY OPTIMIZATION

Key Characteristics of LSH

Locality-Sensitive Hashing (LSH) is defined by its probabilistic approach to similarity search, trading perfect accuracy for dramatic speed improvements in high-dimensional spaces. These core characteristics explain how and why it works.

01

Probabilistic Guarantee

LSH provides a probabilistic guarantee, not a deterministic one. It is designed so that the probability of collision (hashing to the same bucket) is high for similar items and low for dissimilar ones. This is formalized by the LSH family condition: for a similarity function sim and parameters p1, p2 where p1 > p2, if sim(x, y) ≥ p1 then Pr[h(x) = h(y)] ≥ p1, and if sim(x, y) ≤ p2 then Pr[h(x) = h(y)] ≤ p2. The gap between p1 and p2 determines the quality of the hash family.

02

Amplification via AND/OR Construction

Basic LSH functions are amplified to sharpen the probability curve. This is done through two constructions:

  • AND Construction (Concatenation): Combine k hash functions; vectors hash to the same bucket only if all k functions agree. This reduces false positives (dissimilar vectors colliding) but increases false negatives.
  • OR Construction (Multiple Tables): Create L independent hash tables, each with its own AND-composed function. A query is searched in all L tables. This increases recall by reducing false negatives. Tuning k and L controls the trade-off between precision, recall, and query time.
03

Sub-Linear Query Time

The primary performance benefit of LSH is achieving sub-linear query time relative to the dataset size N. Instead of comparing the query to all N vectors (O(N) time), LSH only compares vectors within the same hash bucket(s). The expected query time is often O(N^ρ) where ρ is a parameter less than 1, dependent on the LSH family. For cosine similarity with SimHash, ρ ≈ 1/(1+cos(θ)) for angle θ. In practice, this enables searching billion-scale datasets with latency measured in milliseconds.

04

Hash Families for Common Metrics

Different similarity/distance metrics require specific, mathematically derived hash functions:

  • Cosine Similarity: SimHash (signed random projection) uses a random hyperplane. h(x) = sign(r · x) where r is a random Gaussian vector.
  • Euclidean Distance (L2): p-stable LSH (e.g., E2LSH) uses distributions like Gaussian or Cauchy. h(x) = floor((r · x + b) / w), where b is a uniform random offset and w is a bucket width.
  • Jaccard Similarity: MinHash represents sets as binary vectors. The hash of a set is the minimum element after a random permutation. Each family preserves the locality-sensitive property for its specific metric.
05

Memory vs. Accuracy Trade-off

LSH manages a direct trade-off between memory footprint and retrieval accuracy (recall).

  • Memory: Storing L hash tables increases memory usage linearly with L. Each table stores pointers to vectors.
  • Accuracy: Higher L and careful tuning of k increase recall but also query time and memory. The failure probability (missing a true nearest neighbor) decays exponentially with L, but so does efficiency. This makes LSH suitable for scenarios where an approximate answer is acceptable, and resources are constrained. It is often paired with a multi-stage retrieval pipeline, where LSH is the fast, first-stage retriever.
06

Comparison to Graph-Based ANN

LSH differs fundamentally from graph-based Approximate Nearest Neighbor (ANN) methods like HNSW.

  • Indexing: LSH is data-independent in its basic form (random projections); hash functions are defined before seeing data. HNSW is data-dependent, constructing a graph that adapts to data distribution.
  • Search Pattern: LSH performs hash lookups (O(1) bucket access). HNSW performs greedy graph traversal.
  • Performance Profile: LSH often has faster index build time but may require more memory for multiple tables to achieve high recall. HNSW typically achieves higher recall at low latency for a given memory budget but has a slower build phase. LSH is often favored for extremely high-dimensional data or where the index must be frequently rebuilt.
COMPARISON

LSH vs. Other ANN Methods

A technical comparison of Locality-Sensitive Hashing against other prominent approximate nearest neighbor (ANN) algorithms, focusing on trade-offs relevant to retrieval latency optimization in RAG systems.

Feature / MetricLocality-Sensitive Hashing (LSH)Hierarchical Navigable Small World (HNSW)Inverted File Index + Product Quantization (IVFPQ)

Core Algorithmic Principle

Randomized hash functions for probabilistic bucket assignment

Multi-layered proximity graph for greedy traversal

Clustering (IVF) + subspace quantization (PQ)

Index Build Time

Fast (O(n))

Slow (O(n log n))

Medium (depends on clustering iterations)

Index Memory Footprint

Low to Medium (hash tables)

High (graph structure stored in memory)

Very Low (compressed PQ codes)

Query Latency (Typical)

O(L * bucket size), where L = hash tables

O(log n) with high constants

O(nprobe * (D / m)) where m = PQ subspaces

Tunable Accuracy/Latency Knob

Number of hash tables (L) and hash length (k)

efSearch (priority queue size)

nprobe (clusters searched)

Exact Distance Calculations

Native Support for Filtering

Billion-Scale Suitability

Challenging (requires many tables)

Excellent (with sufficient RAM)

Excellent (low memory footprint)

Dynamic Data Support (Incremental Adds)

Possible but degrades graph quality

Primary Latency Bottleneck

Hash computation & bucket scanning

Graph traversal & distance calculations

Distance approximations & list scanning

LOCALITY-SENSITIVE HASHING

Frequently Asked Questions

A technical FAQ on Locality-Sensitive Hashing (LSH), a cornerstone algorithm for efficient approximate nearest neighbor search in high-dimensional spaces, critical for reducing retrieval latency in RAG systems.

Locality-Sensitive Hashing (LSH) is a family of algorithmic techniques designed to hash high-dimensional input vectors so that similar vectors map to the same hash buckets with high probability, enabling efficient approximate nearest neighbor (ANN) search. The core mechanism involves defining a hash function family H where the probability of collision is proportional to the similarity between vectors: Pr[h(a) = h(b)] = sim(a, b). In practice, multiple independent hash functions are combined to form a compound key, and vectors are indexed into hash tables. During a query, only vectors residing in the same bucket(s) as the query vector are considered for exact distance computation, drastically reducing the search space from the entire dataset to a small, relevant subset. This probabilistic approach trades perfect accuracy for sub-linear query time, making it fundamental for latency-sensitive applications like semantic search in Retrieval-Augmented Generation (RAG).

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.