Home
ArenaGraphSignalTopics
/Distributed Systems Architecture
Chapter 8 • Module 4 5 min breakdown +15 XP Module

Distributed Secondary Indexing

From Track:Distributed Systems ArchitectureDistributed Systems & Consensus

In a horizontally sharded database, routing queries by the primary Shard Key (e.g. WHERE user_id = 42) is instantaneous because the router knows precisely which node holds the record.

However, real-world applications must frequently query by non-shard attributes:

sql
Loading code editor...

When a database is sharded by user_id, searching by email requires Distributed Secondary Indexing.

There are two fundamental approaches:

  1. Document-Partitioned Local Secondary Indexes (Scatter-Gather)
  2. Term-Partitioned Global Secondary Indexes
Interactive Blueprint
Rendering diagram...

1. Document-Partitioned Local Secondary Indexes

In a Local Secondary Index (LSI) (used by MongoDB, Cassandra Local Indexes, PostgreSQL Citus), each physical shard maintains an index exclusively for the records residing on that local shard.

text
Loading code editor...

Write Path (Blazing Fast & Atomic):

  • Inserting or updating a record requires modifying only one single shard.
  • The row insert and its local index entry commit inside a standard single-node ACID transaction with zero network overhead.

Read Path (The Scatter-Gather Penalty):

  • When searching by WHERE email = '[email protected]', the router has no idea which shard holds Alice's record.
  • The router must scatter the query to every single shard in the cluster and gather the results!

2. The Mathematics of Tail Latency Amplification

Why are scatter-gather queries dangerous in large clusters?

Suppose a cluster has shards, and each individual shard has a chance of experiencing a temporary latency hiccup (e.g. background garbage collection or SSD write queue flush):

The probability that a scatter-gather query hits at least one slow shard is:

text
Loading code editor...

In a 100-shard cluster, over 63% of all client requests experience the 99th-percentile worst-case tail latency!


3. Term-Partitioned Global Secondary Indexes

In a Global Secondary Index (GSI) (used by Amazon DynamoDB, Google Spanner, CockroachDB), the index itself is partitioned independently by the indexed attribute (e.g. email).

text
Loading code editor...

Read Path (Blazing Fast Single-Shard Point Read):

  • Querying WHERE email = '[email protected]' routes directly to Index Shard A.
  • Index Shard A returns the exact primary key pointer in without fanning out to any other nodes!

Write Path (Distributed Complexity):

  • Inserting a new user requires writing to two different physical machines:
    1. Data Shard 1 (stores the user row).
    2. Index Shard A (stores the email index entry).
  • Synchronous GSI (Spanner / CockroachDB): Uses distributed Two-Phase Commit with Raft/Paxos (higher write latency).
  • Asynchronous GSI (DynamoDB): The database writes to Data Shard 1 immediately and replicates to the GSI asynchronously via a change-data-capture stream (Eventual Consistency).

4. Architectural Comparison: LSI vs GSI

MetricLocal Secondary Index (LSI)Global Secondary Index (GSI)
Write PerformanceFast ( single-shard ACID)Slower (Cross-shard 2PC or async stream)
Read PerformanceSlow ( Scatter-Gather)Blazing Fast ( targeted point read)
Tail LatencyHigh (Amplifies with shard count)Low (Single-node lookup)
ConsistencyStrict Local SerializabilityStrongly Consistent (2PC) or Eventual (CDC)
Best Used ForWrite-heavy metrics / log ingestionHigh-throughput user lookups & auth services

5. Production Failure Postmortem: The 64-Shard Fanout Collapse

Incident Overview:

In 2021, a fintech banking service experienced severe latency degradation during peak morning trading, with p99 API latency climbing from to over .

What Happened:

  1. The accounts table was sharded across 64 PostgreSQL instances by account_number.
  2. A newly deployed fraud-detection microservice looked up accounts by phone_number using a Local Secondary Index.
  3. Every fraud check fanned out 64 parallel queries to all database nodes simultaneously.
  4. At 15,000 fraud checks/sec, the database cluster was overwhelmed by 960,000 internal scatter-gather queries per second, exhausting connection pools and disk read queues.
Interactive Blueprint
Rendering diagram...

Remediation:

  • Provisioned a dedicated Global Secondary Index (GSI) table partitioned by phone_number with change-data-capture (Debezium + Kafka).
  • Reduced query fanout from to , dropping p99 latency back to .

6. Chapter Summary & Secondary Indexing Rules

text
Loading code editor...
Milestone Verification

Ready for the next lesson?

Mark this module complete to record verified progress and earn +15 XP toward your architect profile.