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.
Glossary
Locality-Sensitive Hashing (LSH)

What is Locality-Sensitive Hashing (LSH)?
A foundational algorithm for fast, approximate similarity search in high-dimensional spaces.
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.
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.
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.
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.
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.
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.
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.
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.
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 / Metric | Locality-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 | High (adjust via |
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% |
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 eachh(v)is a single, randomized hash function (e.g., based on random projections). - The
k-dimensional output ofg(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.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Locality-Sensitive Hashing (LSH) is a core technique for approximate nearest neighbor search. These related concepts define the algorithms, metrics, and trade-offs involved in optimizing vector retrieval.
Approximate Nearest Neighbor (ANN) Search
Approximate Nearest Neighbor (ANN) Search is a class of algorithms designed to find vectors in a database that are most similar to a query vector, trading off perfect accuracy for significantly faster query times compared to an exhaustive linear scan. LSH is a foundational ANN technique.
- Core Trade-off: Enables sub-linear or logarithmic query time by accepting that some true nearest neighbors may be missed.
- Use Case: Essential for real-time semantic search, recommendation systems, and retrieval-augmented generation (RAG) where querying billions of vectors is required.
- Algorithm Families: Includes hashing-based methods (LSH), graph-based methods (HNSW), and tree-based methods.
Hierarchical Navigable Small World (HNSW)
Hierarchical Navigable Small World (HNSW) is a graph-based algorithm for approximate nearest neighbor search that constructs a multi-layered graph. It is a contemporary, highly performant alternative to LSH for many high-recall use cases.
- Mechanism: Creates a hierarchy of graphs where long-range connections on higher layers enable fast, logarithmic-time search via greedy traversal.
- Comparison to LSH: Typically offers higher recall and lower latency for a given memory footprint but can have higher index construction time and is less naturally suited for certain filtering scenarios.
- Key Parameters: Controlled by
M(maximum connections per node) andefConstruction/efSearchfor quality/speed trade-offs.
Recall & Precision
Recall and Precision are the fundamental evaluation metrics for any approximate search algorithm like LSH, quantifying the trade-off between completeness and accuracy.
- Recall: The fraction of true nearest neighbors (from an exhaustive search) that are successfully retrieved by the ANN system. High recall means missing fewer relevant results.
- Precision: The fraction of retrieved items that are true nearest neighbors. High precision means the returned list contains less irrelevant noise.
- LSH Tuning: Increasing the number of hash tables or hash functions generally improves recall at the cost of higher memory usage and query latency. Recall@K is the standard operational metric.
Product Quantization (PQ)
Product Quantization (PQ) is a lossy compression technique for high-dimensional vectors often used in conjunction with coarse quantizers like IVF. It reduces memory footprint, enabling billion-scale indexes in RAM, a common complement to LSH's retrieval role.
- Mechanism: Splits a vector into subvectors, quantizes each subspace independently using a learned codebook, and represents the vector by a short code of sub-quantizer indices.
- Distance Calculation: Uses Asymmetric Distance Computation (ADC) for accurate distance approximations between a raw query vector and compressed database vectors.
- System Design: In a multi-stage pipeline, LSH can perform fast candidate generation, after which a more precise but expensive PQ-based distance calculation re-ranks the final results.
Distance Metrics
A Distance Metric is a mathematical function that defines similarity between vectors. The choice of metric dictates the geometry of the search space and the design of the LSH family.
- LSH Family Dependency: An LSH scheme is defined for a specific distance metric (e.g., Euclidean LSH, Cosine LSH).
- Common Metrics:
- Cosine Similarity: Measures angular similarity; ideal for text embeddings. Corresponding LSH uses random hyperplanes.
- Euclidean Distance (L2): Measures straight-line distance. Corresponding LSH often uses stable distributions (p-stable LSH).
- Inner Product: Used for similarity of normalized vectors. Requires specialized LSH constructions.
- Index Selection: The vector database must use an index (like LSH) compatible with the application's chosen distance metric.
Filtered & Hybrid Search
Filtered Search is a hybrid query that combines vector similarity search with conditional metadata filters. Integrating LSH with filtering requires careful engineering to maintain performance.
- Challenge: Applying a metadata filter (e.g.,
date > 2024) before or after the LSH lookup can drastically impact recall. - Strategies:
- Pre-Filtering: Apply metadata filters first, then run LSH on the filtered subset. Risk: can eliminate all true nearest neighbors.
- Post-Filtering: Run LSH first, then filter the results. Risk: may return fewer than K results if the filter is selective.
- Advanced Solutions: Modern vector databases implement single-stage filtered search where the index structure (or query planner) integrates filters during traversal, a complex but critical optimization.

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us