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 N=108 vectors by dimension d=256 gives 102.4 GB in fp32 (4 B per float) and 51.2 GB in fp16 (2 B per float). Product quantisation (PQ) splits each vector into M=32 subspaces and encodes each 8-dimensional sub-vector as a 1-byte centroid ID, shrinking the vectors to 32 B each (3.2 GB total) plus negligible codebooks.

Steps

  1. Compute uncompressed fp32 memory: each float uses 4 bytes, so one 256-dimensional vector uses 256×4=1,024 B. For N=108 items, total memory is 108×1,024 B=102.4 GB (≈95.4 GiB).
  2. Compute fp16 memory: halving precision to 2 bytes per float halves vector size to 512 B, giving 108×512 B=51.2 GB (≈47.7 GiB), usually with negligible loss in retrieval accuracy.
  3. Compute PQ memory: with M=32 sub-quantisers and 1 byte (8 bits, or 28=256 centroids) per sub-quantiser, each vector takes 32 B, so 108 vectors take 108×32 B=3.2 GB (≈2.98 GiB), a 32× compression over fp32. The 32 codebooks (32×256 centroids of dimension 256/32=8 in fp32) take only 32×256×8×4 B=262,144 B≈0.26 MB.
  4. Explain how PQ works: each 256-dimensional vector is partitioned into M=32 disjoint sub-vectors of dimension 8, and k-means with k=256 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.
  5. 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., 5×–10×) 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, 32 4-byte integer links per vector adds 128 B/vector=12.8 GB for 108 items), which can outweigh the PQ codes.

Common mistakes

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?