Inferensys

Glossary

Locality-Sensitive Hashing (LSH)

Locality-Sensitive Hashing (LSH) is a family of hashing techniques designed to maximize the probability of collision for similar items, enabling efficient approximate nearest neighbor search by hashing vectors into buckets.
Engineer reviewing vector database search results on laptop, embeddings visualization on screen, home office coding session.
VECTOR QUERY OPTIMIZATION

What is Locality-Sensitive Hashing (LSH)?

A foundational algorithm for fast, approximate similarity search in high-dimensional spaces.

Locality-Sensitive Hashing (LSH) is a family of randomized hashing algorithms designed to maximize the probability of collision for similar input items, enabling efficient approximate nearest neighbor (ANN) search by grouping analogous vectors into the same hash buckets. Unlike cryptographic hashing, which seeks to minimize collisions, LSH functions are engineered so that points close in the original space (e.g., by Euclidean distance or cosine similarity) have a high likelihood of hashing to the same value. This property allows search systems to quickly narrow the candidate set by only comparing the query vector to others in its bucket, achieving sub-linear query time at the cost of probabilistic recall.

LSH is implemented by constructing multiple independent hash tables using different, randomly generated hash functions from a locality-sensitive family, such as p-stable distributions for L2 distance or random hyperplanes for cosine similarity. To perform a query, the system hashes the input vector to retrieve candidate neighbors from corresponding buckets across all tables, then computes exact distances within this reduced set. Key trade-offs are controlled by parameters for the number of hash functions and tables, balancing higher recall and precision against increased memory usage and construction time. This makes LSH a core technique for latency reduction in large-scale semantic search and recommendation systems.

CORE MECHANICS

Key Features of LSH

Locality-Sensitive Hashing (LSH) enables efficient approximate nearest neighbor search by using hash functions designed to maximize collisions for similar items. Its core features define its probabilistic nature, performance trade-offs, and practical implementation.

01

Probabilistic Guarantee

Unlike cryptographic hashing, LSH provides a probabilistic guarantee of collision. The core property is defined by a distance metric (d), a similarity threshold (R), and probabilities (p1) and (p2), where (p1 > p2). A hash family (H) is ((R, cR, p1, p2))-sensitive if:

  • If (d(x, y) \leq R), then (Pr[h(x) = h(y)] \geq p1) (similar items collide with high probability).
  • If (d(x, y) \geq cR), then (Pr[h(x) = h(y)] \leq p2) (dissimilar items collide with low probability). This formalizes the approximate nature of the search, trading perfect recall for massive speedups.
02

Amplification via AND/OR Constructions

Single hash functions provide weak separation. LSH amplifies the gap between (p1) and (p2) using AND and OR constructions.

  • AND Construction (Concatenation): Combine (r) hash functions; a collision requires all (r) to match. This reduces (p1) and (p2), making the hash more selective and shrinking bucket sizes.
  • OR Construction (Multiple Hash Tables): Create (L) independent hash tables, each with its own AND-concatenated hash function. An item is a candidate if it collides in any table. This increases the overall (p1), boosting recall. Tuning (r) and (L) controls the recall vs. precision trade-off and query time.
03

Hash Families for Common Metrics

LSH requires hash functions tailored to the target distance metric. Key families include:

  • Cosine Similarity (Random Projections): For vectors on a unit sphere, hash bit (h_i(x) = \text{sign}(w_i \cdot x)), where (w_i) is a random Gaussian vector. This implements SimHash.
  • Euclidean Distance (L2) - p-stable distributions: Uses E3F2LSH or p-stable LSH. Hashes are computed as (h_{a,b}(v) = \left\lfloor \frac{a \cdot v + b}{w} \right\rfloor), where (a) is drawn from a p-stable distribution (e.g., Gaussian for L2) and (w) is a bucket width.
  • Jaccard Similarity (MinHash): For sets, the hash function is the minimum element after a random permutation. The probability of hash equality equals the Jaccard index.
04

Sub-Linear Query Time

The primary performance benefit of LSH is achieving sub-linear query time relative to dataset size (N). An exhaustive (linear) scan is (O(Nd)) for (d)-dimensional vectors. LSH reduces this to roughly (O(L \cdot (r + \text{bucket size}))). The expected bucket size is controlled by the AND construction ((r)). In practice, for large (N), query time can be orders of magnitude faster than a linear scan, enabling real-time similarity search on billion-scale datasets, albeit with an approximate result.

05

Tunable Recall-Precision Trade-off

LSH does not have a single "correct" result. Performance is controlled by hyperparameters that create a tunable recall-precision curve:

  • Number of hash functions per table ((r)): Increasing (r) makes buckets sparser, raising precision but lowering recall.
  • Number of hash tables ((L)): Increasing (L) provides more chances for collision, raising recall but increasing memory and query time.
  • Bucket width ((w)) for Euclidean LSH: A larger (w) puts more points in each bucket, increasing recall but reducing precision. Engineers tune these parameters based on application SLOs for recall@K and query latency.
06

Memory and Indexing Overhead

The efficiency of LSH comes with structural overhead:

  • Memory for Hash Tables: Maintaining (L) hash tables requires storing (L \times N) hash keys and pointers, leading to significant memory consumption. This is the cost of the OR construction.
  • Indexing Time: Precomputing hash values for all dataset points during index build is typically (O(NLrd)). While often acceptable as a one-time cost, it is non-trivial for streaming data.
  • Comparison to Graph Indexes: Unlike HNSW, LSH does not store a graph structure. Its memory overhead is more predictable (scales with (L) and (N)) but can be higher for the same recall level, especially in high dimensions.
ALGORITHM COMPARISON

LSH vs. Other ANN Methods

A technical comparison of Locality-Sensitive Hashing against other prominent approximate nearest neighbor (ANN) search algorithms, focusing on core operational characteristics and performance trade-offs.

Feature / MetricLocality-Sensitive Hashing (LSH)Hierarchical Navigable Small World (HNSW)Inverted File Index (IVF)

Core Algorithmic Principle

Randomized hash functions for probabilistic bucket collisions

Multi-layered proximity graph with long-range connections

Voronoi cell partitioning via clustering (inverted index)

Index Build Time

Fast (O(nd) for n d-dim vectors)

Slow (O(n log n) for high-quality graph)

Medium (dominated by clustering, e.g., k-means)

Index Memory Overhead

Low (stores hash tables and signatures)

High (stores graph edges and neighbor lists)

Medium (stores centroids and inverted lists)

Query Latency Profile

Predictable, hash computation + bucket scan

Variable, depends on graph traversal path length

Predictable, centroid search + list scan)

Tunability for Recall/Latency

Limited (adjust via # hash tables/functions)

High (fine-tune via efSearch, M parameters)

High (adjust via nprobe parameter)

Exact Search Capability

Dynamic Data Support (Inserts/Deletes)

Native Support for Filtered Search

Distance Metric Flexibility

High (requires LSH family for metric)

High (graph works with any metric)

High (works with any metric)

Typical Best-Case Recall@10 (at comparable latency)

85-92%

95-99%

90-97%

LOCALITY-SENSITIVE HASHING (LSH)

Frequently Asked Questions

Locality-Sensitive Hashing (LSH) is a cornerstone algorithm for approximate nearest neighbor search in high-dimensional spaces. These questions address its core mechanisms, trade-offs, and practical implementation within vector database infrastructure.

Locality-Sensitive Hashing (LSH) is a probabilistic hashing technique designed so that the probability of collision (hashing to the same value) is much higher for similar input items than for dissimilar ones. It works by applying a family of hash functions that map high-dimensional vectors into buckets or bins. Similar vectors are likely to land in the same bucket, while distant vectors are likely to be separated. During a query, only vectors within the same bucket(s) as the query vector need to be compared, dramatically reducing the search space from the entire dataset to a small subset.

Core Mechanism:

  • A family of LSH functions g(v) = [h1(v), h2(v), ..., hk(v)] is defined, where each h(v) is a single, randomized hash function (e.g., based on random projections).
  • The k-dimensional output of g(v) acts as a compound hash key, determining the vector's bucket.
  • To increase recall, multiple independent hash tables (using different function families) are constructed. A query vector is hashed across all tables, and the union of candidates from the matching buckets is examined.
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.