What is HNSW?

Quick answer

HNSW, short for Hierarchical Navigable Small World, is the specific graph-based algorithm that has become the dominant choice for approximate nearest neighbor search across most modern vector databases, building a multi-layered graph structure that lets a search navigate quickly toward a query’s nearest matches by starting with large, coarse jumps across the collection and progressively refining toward more precise, local matches as it descends through the graph’s layers. Where this collection’s discussion of vector indexing covers graph-based indexing as a general category, this article covers specifically how HNSW’s layered structure works, why that layering makes it so effective, and the concrete parameters that control its actual behavior in practice.

Summary slides
HNSW
The core insight: layering a graph the way a highway system layers roads
The key parameters that actually control HNSW's behavior
Why HNSW handles incremental updates more gracefully than some…
Common mistakes teams make around HNSW

The core insight: layering a graph the way a highway system layers roads

HNSW’s defining idea is organizing its graph into multiple layers, with the top layer containing only a small number of vectors connected by long-range links, and each layer below adding progressively more vectors with progressively shorter, more local connections, until the bottom layer contains every single vector in the collection connected only to its truly nearby neighbors. This structure closely mirrors how a highway system is organized, major highways connect distant cities directly with few intermediate stops, while local roads handle the final, more detailed navigation once a traveler is already close to their actual destination.

A search starts at the top, sparse layer and uses its long-range connections to jump quickly toward the general region of the collection where the query’s nearest matches are likely to be, then descends to progressively lower layers, using their progressively denser, more local connections to refine that initial rough position into an increasingly precise final answer. This layered approach is what lets HNSW avoid both the slowness of examining the entire collection and the risk of getting stuck in a single, local neighborhood that happens not to contain the true nearest matches, the top layers provide the long-range navigation a purely local, single-layer graph would lack.

Why building the graph this way actually works well in practice

The reason HNSW performs so effectively comes down to how efficiently this layered structure lets a search converge on the right answer, each layer traversal only needs to examine a small number of candidate connections before moving to the next layer down, and because higher layers cover much more ground per step, the total number of comparisons needed to reach a good answer stays remarkably small even as the overall collection grows very large. This is what gives HNSW its favorable scaling behavior, search cost grows much more slowly than the size of the collection, unlike exact search’s direct, linear cost growth.

This efficiency doesn’t come free, building this layered structure in the first place requires real computational work, deciding which layer each new vector belongs to, and carefully choosing which existing vectors it should connect to at each of its layers to maintain the graph’s overall navigability. This build cost is why HNSW indexes, while fast to search, can be comparatively slower and more resource-intensive to construct than some cluster-based alternatives, a tradeoff worth understanding clearly when weighing HNSW against other indexing approaches for a specific deployment.

The key parameters that actually control HNSW’s behavior

HNSW exposes several parameters that directly shape the tradeoff between search speed, accuracy, and memory usage, and understanding what each one actually does matters for tuning it effectively rather than accepting default values that may not fit a given deployment’s needs. The number of connections each vector maintains within a layer, often denoted M, directly affects both search quality and memory usage, more connections per vector generally improve search accuracy by giving the graph more paths to navigate through, but at the direct cost of more memory consumed per vector and a somewhat more expensive index to build and maintain.

A separate parameter, often called ef during search, controls how many candidate paths get actively explored at each step of a query, with a higher value examining more candidates and producing better accuracy at the cost of slower search, while a lower value searches faster but risks missing some genuinely relevant matches along the way. A related parameter used specifically during index construction controls how thoroughly the graph’s connections get chosen when the index is first built, a more thorough construction process produces a higher-quality graph that searches better later, at the cost of a slower, more expensive build process upfront.

Why tuning these parameters is an empirical exercise, not a guessing game

Because these parameters trade directly against each other, more connections and more thorough search exploration improve accuracy but cost more memory and more time, there’s no single universally correct setting, and the right values genuinely depend on a given deployment’s actual priorities and constraints. This connects directly to the broader discussion of recall measurement covered in this collection’s discussion of approximate nearest neighbor search, tuning HNSW well requires measuring recall directly against a representative sample of real queries at different parameter settings and observing how accuracy and search speed actually trade off for that specific collection and workload, rather than guessing at reasonable-sounding values.

A team that accepts HNSW’s default parameter values without this kind of empirical tuning is very likely leaving meaningful performance on the table, either paying for more accuracy than the application genuinely needs, wasting memory and search speed, or accepting lower accuracy than the application could comfortably afford given its actual latency and cost tolerances, both are avoidable outcomes of skipping the tuning work this article describes.

Why HNSW handles incremental updates more gracefully than some alternatives

A meaningful practical advantage HNSW offers over some other indexing approaches is that it supports adding new vectors to an existing graph incrementally without requiring a full rebuild from scratch, a new vector gets assigned to a layer and connected into the existing structure directly, extending the graph rather than needing the entire index reconstructed. This connects directly to the operational challenges covered in this collection’s discussion of vector database scaling, a system with content that changes frequently benefits considerably from an indexing approach that tolerates this kind of incremental growth gracefully rather than requiring disruptive, periodic rebuilds.

This isn’t entirely free of cost, however, since a graph that’s grown through a very large number of incremental additions without ever being rebuilt can gradually drift away from the structure a fresh, complete build would have produced, and some deployments still periodically rebuild an HNSW index from scratch even though incremental updates are technically supported, specifically to restore the graph to its most efficient possible structure after a large amount of incremental change has accumulated.

Why HNSW became the dominant default across so many vector databases

HNSW’s combination of strong search accuracy, favorable scaling behavior, and support for incremental updates is a large part of why it’s become the default indexing approach across a considerable majority of modern vector databases, even though cluster-based alternatives remain genuinely competitive for certain specific use cases, particularly ones prioritizing memory efficiency or fast index construction over peak search accuracy. Understanding why HNSW earned this dominant position, rather than simply accepting it as an unexplained default, helps clarify when a cluster-based alternative might actually be a better fit for a given deployment’s own particular constraints.

This is worth understanding clearly rather than assuming HNSW is automatically correct for every situation simply because it’s the most commonly used default, the right indexing choice still depends on a deployment’s specific balance of priorities, and HNSW’s popularity reflects that it serves the most common combination of priorities well, not that it’s universally the optimal choice for every conceivable vector search deployment.

Common mistakes teams make around HNSW

1. Accepting HNSW’s default parameter values without empirically tuning them against the actual recall and latency requirements a specific deployment genuinely needs.

2. Assuming HNSW’s support for incremental updates means an index never needs periodic rebuilding, missing how accumulated incremental change can gradually degrade graph quality over time.

3. Choosing HNSW automatically without evaluating whether a cluster-based alternative might actually better fit a deployment’s specific memory or build-time constraints.

4. Underestimating the memory cost of a higher connection count parameter, discovering the impact only after deploying at a meaningfully larger scale than initial testing covered.

5. Treating HNSW’s build process as a quick, low-cost operation, without planning for the real time and computational resources a thorough, high-quality graph construction actually requires.

What connects these mistakes is treating HNSW as a black box that works automatically without needing deliberate configuration, when its actual performance, both search quality and operational cost, depends directly on understanding and tuning its layered structure and parameters against a specific deployment’s real data and real requirements.

The deeper point about HNSW is that its layered, highway-like structure is a genuinely elegant solution to the core tension in approximate nearest neighbor search, needing both the long-range navigation to avoid getting stuck in the wrong region of a collection and the local precision to actually find the closest matches once nearby, and understanding how that layering actually works is what lets a team configure and operate it as a deliberately tuned system rather than an opaque default whose behavior remains a mystery until something unexpectedly goes wrong.