CHAPTER 12

Algorithms and Data Structures for Scale

Probabilistic structures, geo-indexing, unique IDs and tools that appear in large system designs

Source: Chapter 12 PDF, pp. 1–7

Learning objective

Recognize the problem a data structure solves, understand its trade-offs, and know when to use it in a system design.

Core PDF content

PDF page 11. Why do large systems need specialized data structures? p. 1

Imagine a website with one billion users. You want to know whether a URL has been seen, estimate unique visitors, find trending topics, locate nearby drivers, or generate unique IDs across many servers. A straightforward solution may use too much memory, take too long, or require too much coordination.

Figure 1. · PDF p. 1

2. Bloom filters: "definitely not" or "possibly present" p. 1

A Bloom filter is a compact structure that helps determine whether an item may belong to a set. It uses several hash functions to set bits in a bit array.

Figure 2. · PDF p. 1
  • Definitely absent: the item is not in the set, so the application can safely skip the source lookup.
  • Possibly present: the item may exist; check the actual data source to confirm.

A standard Bloom filter can produce false positives, but it does not produce false negatives when used correctly. A false positive means an absent item is reported as possibly present; the database still protects correctness.

PDF page 23. Sketches: estimate huge datasets cheaply p. 2

Some systems need useful estimates rather than exact answers. A sketch summarizes a large stream using limited memory, with a defined approximation or error profile.

StructureQuestion answeredTypical useImportant trade-off
Bloom filterIs x present?Skip unnecessary lookupsFalse positives possible; no false negatives in standard use
HyperLogLogHow many distinct items?Unique visitors, distinct countsApproximate count; compact memory
Count-Min SketchHow often did x occur?Frequent searches, rate limitingBasic estimates can overcount
Top-K / Space- SavingWhich k items are most frequent?Trending hashtags, leaderboardsBounded memory; guarantees depend on algorithm
t-digest / HDR histogramWhat are latency percentiles?p95/p99 across servicesCompact summaries; approximation differs by method
Reservoir samplingHow to sample a stream of unknown size?Representative log or trace samplesStores only a bounded sample
MinHash / SimHash / LSHWhich items are similar?Near-duplicate documents and crawl deduplicationSimilarity is approximate

PDF page 34. Finding nearby locations efficiently p. 3

Imagine a delivery app searching for drivers within 2 km of a customer. Calculating the distance to every driver may be expensive at large scale. A geospatial index narrows down candidates first; the application then calculates exact distances for the final selection.

Figure 3. · PDF p. 3

Grid-based indexes p. 3

IndexHow it worksConsideration
GeohashEncodes latitude/longitude into a string; longer shared prefixes often indicate proximity.Nearby points can fall across cell edges, so query neighboring cells too.
S2Divides the globe into hierarchical spherical cells.Supports hierarchical location and region queries.
H3Uses hierarchical hexagonal cells.Useful for neighborhood and regional aggregation.

Adaptive spatial indexes p. 3

  • Quadtree: recursively subdivides dense map regions into smaller areas while leaving sparse regions larger.
  • R-tree: organizes spatial objects by bounding boxes; used for spatial queries such as intersections and nearby regions.

For frequently moving drivers, a hot in-memory grid or Redis geospatial feature can maintain current locations between updates.

PDF page 45. Generating unique IDs across many servers p. 4

If many application servers create orders at once, asking one central database counter for every ID can add coordination and contention. A distributed ID generator can let workers create IDs independently while preserving the required uniqueness guarantees.

Figure 4. · PDF p. 4
MethodAdvantagesTrade-offs
Database auto- incrementSimple, compact, orderedCentral contention; sequence can reveal volume; harder across shards
UUID v4Random IDs; no coordination needed128-bit; not time-sortable; can have worse index locality
UUID v7 / ULIDTime-oriented ordering and index-friendly behavior128-bit; ordering depends partly on clock behavior and format
Snowflake / Sonyflake64-bit, time-sortable, distributed generationWorker-ID coordination and clock discipline required
Ticket server / ID rangesWorkers lease blocks of IDs to generate locallyGaps possible; range allocator remains a dependency

How a Snowflake-style ID works p. 4

The identifier typically combines a timestamp, a worker identifier and a sequence number. The timestamp supports approximate time ordering, worker identity distinguishes generators, and the sequence allows multiple IDs within the same time unit. The supplied design describes a configuration capable of about 4,096 IDs per millisecond per node, or roughly 4 million per second. This is configuration-specific, not a universal guarantee.

PDF page 56. Other data structures that appear in system design p. 5

Focus on the operation each structure makes efficient rather than memorizing implementation details.

Structure / ideaPlain-English purposeExample
Trie / radix treeOrganizes strings by shared prefixesAutocomplete
Skip listKeeps ordered data with efficient search and updatesSorted in-memory collections
Merkle treeCompares data using hashes of groups of recordsReplica repair and integrity verification
Consistent / rendezvous hashingAssigns keys to nodes while limiting remapping when nodes changeDistributed caches and partitioning
Content-defined chunking + rolling hashSplits files at content-dependent boundariesDeduplication and delta sync
Inverted indexMaps terms to documentsFull-text search
Heap / priority queueEfficiently retrieves highest or lowest priority itemScheduling and top-K
LRU: hash map + doubly linked listSupports constant-time lookup and recency updates in a standard implementationLRU cache
Reed-Solomon / erasure codingAdds recovery fragments rather than storing full duplicate copiesDurable distributed storage
HNSW / IVF / product quantizationSpeeds up approximate nearest-neighbor search over vectorsSemantic search and RAG

Trie: follow a shared prefix p. 5

Figure 5. · PDF p. 5

Merkle tree: narrow down differing data p. 5

Figure 6. · PDF p. 5

A Merkle tree lets replicas compare hashes from the root downward. If the root hashes differ, the system compares child hashes to narrow down which ranges need repair.

PDF page 67. More structure examples p. 6

Heap / priority queue p. 6

Figure 7. · PDF p. 6

A heap keeps the item with the highest or lowest priority at the root, so the next job can be retrieved efficiently without fully sorting all waiting jobs after every change.

Other concepts to recognize p. 6

8. How do you choose the right structure? p. 6

If you need to…Consider…
Avoid lookups for missing itemsBloom filter
Estimate unique visitorsHyperLogLog
Find frequent itemsCount-Min Sketch or Top-K
Find nearby locationsGeospatial index
Generate IDs on many serversSnowflake-style IDs or UUIDs
Find similar contentMinHash / SimHash / LSH, or vector ANN indexes depending on the task
Compare replica dataMerkle tree
Pick the next urgent jobHeap / priority queue

PDF page 7Chapter recap p. 7

  • Bloom filters answer "definitely not" or "possibly present."
  • Sketches estimate distinct counts, frequencies, percentiles and samples with limited memory.
  • Geospatial indexes narrow down nearby candidates efficiently.
  • Distributed ID generators issue unique identifiers without a central request for every ID.
  • Tries, heaps, Merkle trees and inverted indexes solve different search, ordering and comparison problems.
  • Erasure coding improves storage efficiency while preserving recovery capability.
  • Approximate nearest-neighbor indexes accelerate similarity search over embeddings.

Check your understanding p. 7

QuestionOptionsAnswer and why
1. Avoid lookups for keys that may not exist?A. Bloom filter B. Priority queue C. TrieA — Bloom filter. It can identify definitely absent items.
2. Estimate unique visitors across billions of events?A. HyperLogLog B. LRU cache C. Merkle treeA — HyperLogLog. It estimates distinct counts compactly.
3. Find nearby delivery drivers?A. Bloom filter B. Geospatial index C. Priority queueB — Geospatial index. It narrows the candidates before exact distance checks.
4. Generate time-sortable IDs across workers?A. Snowflake-style ID B. SQL encryption C. HyperLogLogA — Snowflake-style ID. It combines time, worker identity and a sequence.
5. Compare data across replicas efficiently?A. Merkle tree B. Rate limiter C. UUID v4A — Merkle tree. Hashes help narrow down differing ranges.
6. Retrieve the highest-priority job efficiently?A. Heap / priority queue B. Bloom filter C. HyperLogLogA — Heap / priority queue. It exposes the next priority item efficiently.

Review questions for this chapter have not been written and reviewed yet. The chapter text above is the full source content.

References

Chapter 12 PDF, pp. 1–7; figures 1, 2, 3, 4, 5, 6, 7.

Tip: use ← and → keys to move between chapters.