Find the most similar thing — fast — brute-force vector search and the 10 ms wall

Once every item is turned into a list of numbers, 'find the most similar item' means 'find the closest list' — but checking every stored list one-by-one is exact yet hopeless at a billion items, and that single wall is the reason this whole system exists.

Scene 01

Find the most similar thing — fast

  1. Watch
  2. Try it
  3. Predict
  4. Capture
acoustic → electroniccalm → energeticLo-fi RainAcoustic Suns…Campfire FolkCoffeehouseIndie DriveSynth DawnNeon CityClub PulseRave PeakBass DropMidnight DriveGarage BeatStudy BeatsLonely SynthNow PlayingEvery song is a pair of numbers (x, y). 'Now Playing' is too — and 'most simi…SCALE14 songsTouch all 14: instant. The toy hides th…10 ms0 ms
What to watch for

An app needs to answer one question over and over: 'find the items most similar to this one.' To make 'similar' something a computer can compute, we turn every item into a short list of numbers — here, each song becomes two numbers: how acoustic-vs-electronic it is, and how calm-vs-energetic. Now 'the most similar song' just means 'the song whose list of numbers is closest to mine.' Watch: 'Now Playing' lights up, and the search measures the straight-line gap to every single song, one at a time, before it can rank them. For 14 songs that's instant. Hold onto one fact: to be SURE of the closest, it had to touch every point.

Continue unlocks when the animation finishes.
Implementation

Highlighted lines are the ones running in the diagram right now.

FlatIndex.search
the brute-force scan: rank every stored vector, keep the best k
def search(query, k):
heap = [] # k smallest distances so far
for vec in all_vectors: # touches all N
d = distance(query, vec)
heap.push((d, vec.id))
if len(heap) > k:
heap.pop_largest()
return argsort(heap)[:k] # the true top-k
distance
one gap = a sum over EVERY dimension of the vector
def distance(query, vec):
total = 0.0
# every dimension counts — closeness lives in all D
for i in range(D): # D = 1536
diff = query[i] - vec[i]
total += diff * diff # one multiply-add
return total
FlatIndex.search_parallel
split the vectors across cores, then merge the partial top-k
def search_parallel(query, k, cores):
shards = split(all_vectors, cores)
partials = parallel_map(
lambda shard: scan(shard, query, k),
shards,
)
# each core still scans its whole shard:
# sum of shard sizes == N, work is unchanged
return merge_top_k(partials, k)

Where this sits in Build a vector database (Pinecone / Weaviate / pgvector style)

Scene 01 of 15, in the Why vectors act — Why a billion-vector exact scan can't hit 10 ms, and what 'closest' even means.. Turn items into lists of numbers and 'most similar' becomes 'closest list' — but checking every one is exact and hopeless at a billion. That wall is why this whole system exists.

Up next. We keep saying 'closest list of numbers', but we never said what 'closest' actually means. Before we can make search fast, we have to pin down exactly how to measure how similar two vectors are — and it turns out there is more than one way, and picking the wrong one silently returns garbage.

All 15 scenes in Build a vector database (Pinecone / Weaviate / pgvector style) · Every curriculum

Built with Arqly
Every scene in Build a vector database (Pinecone / Weaviate / pgvector style) builds on the one before it.All 15 Build a vector database (Pinecone / Weaviate / pgvector style) scenes