Vector DBs & RAG (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
Product Quantization (PQ) and Scalar Quantization (SQ): Mathematical principles and RAM reduction up to 95%.
senior Levelvector-databasesAdvanced Internals
In Simple Words: High-dimensional vectors are stored as 32-bit floating point numbers (FP32). 1 million 1536-dim vectors consume 6 GB of raw memory:
- Scalar Quantization (SQ8): Compresses 32-bit floats into 8-bit integers (
int8) using linear scaling: $$ ext{int8} = ext{round}left(255 imes rac{x - min}{max - min} ight)$$- Yields an immediate 4x reduction in RAM (from 6 GB down to 1.5 GB) with virtually zero loss in search accuracy (< 0.5% drop in recall).
- Product Quantization (PQ): Decomposes a 1536-dim vector into 192 sub-vectors of 8 dimensions each, clusters sub-vectors into codebooks, and replaces them with 1-byte centroid IDs.
- Yields up to 95% memory reduction, allowing massive datasets to reside in memory at the cost of slightly lower recall.