How much memory does an approximate nearest neighbour (ANN) index of 100 million 256-dimensional item embeddings require in fp32 (32-bit floating point), in fp16 (16-bit floating point), and with product quantisation (PQ) using 32 sub-quantisers of 1 byte each? How does PQ achieve this compression, and what are the trade-offs?
ML system design, Retrieval and ranking
Level L3 Intermediate. Length Short. Nature Mat Mathematical.
What's being asked
Whether you can do back-of-the-envelope memory sizing for large vector indexes and explain how product quantisation compresses vectors at the cost of recall.
Key idea
Multiplying vectors by dimension gives in fp32 ( per float) and in fp16 ( per float). Product quantisation (PQ) splits each vector into subspaces and encodes each -dimensional sub-vector as a -byte centroid ID, shrinking the vectors to each ( total) plus negligible codebooks.
Steps
- Compute uncompressed fp32 memory: each float uses , so one -dimensional vector uses . For items, total memory is ().
- Compute fp16 memory: halving precision to per float halves vector size to , giving (), usually with negligible loss in retrieval accuracy.
- Compute PQ memory: with sub-quantisers and (, or centroids) per sub-quantiser, each vector takes , so vectors take (), a compression over fp32. The 32 codebooks ( centroids of dimension in fp32) take only .
- Explain how PQ works: each -dimensional vector is partitioned into disjoint sub-vectors of dimension , and -means with is run separately in each subspace to learn 32 small codebooks. Each sub-vector is then replaced by the 1-byte index of its nearest centroid, allowing fast approximate inner products or distances via precomputed lookup tables.
- State the recall–memory trade-off and graph index overhead: lossy quantisation introduces distance approximation error that lowers recall, so systems often fetch more candidates (e.g., –) and re-score with exact vectors from disk or RAM. In addition, graph indexes such as HNSW (hierarchical navigable small world) store neighbour link lists per vector (for example, 4-byte integer links per vector adds for items), which can outweigh the PQ codes.
Common mistakes
- Forgetting to multiply by for fp32 and reporting instead of .
- Treating HNSW (hierarchical navigable small world) plus PQ memory as just the vector payload while ignoring the graph adjacency lists.
- Assuming PQ compresses vectors without changing recall; aggressive quantisation can hurt tail queries unless paired with a re-scoring step.
How long your answer should be
About 2 minutes; three numbers, two sentences on PQ, and the recall and graph-link caveats.
Read this
Previous in ML system design: Why do large recommenders and search engines split prediction into candidate generation (retrieval) followed by ranking? Suppose a catalogue has 10 million items and a heavy ranking model takes 50 µs per item; how does scoring the whole catalogue compare with using a two-tower model and approximate nearest neighbour (ANN) search to retrieve 1,000 candidates first?
Next in ML system design: What artefacts and metadata must you version so that a trained ML model can be reproduced and safely rolled back in production, and why can bit-exact reproduction still fail?