Database DesignIndexing StrategiesHard⏱️ ~3 min

What Are the Cost and Consistency Tradeoffs of Secondary Indexes?

Write Amplification

Secondary indexes enable queries on non-primary-key columns, but every write to the base table must also update all secondary indexes. A table with 2 secondary indexes writing 10K items/sec generates 30K effective writes: 10K to the base table and 10K to each index. This "write amplification" multiplies both write throughput and storage costs.

Consistency Models

Secondary index updates can be synchronous (index updated in the same transaction as base data) or asynchronous (index updated later). Synchronous updates guarantee consistency but add latency: every write waits for index updates. Asynchronous updates are faster but queries may temporarily see stale or missing data. The delay is typically milliseconds to seconds depending on system load.

Local vs Global Secondary Indexes

In distributed databases, local secondary indexes are co-located with the data they index (same partition). Queries using the partition key plus local index are fast. Global secondary indexes are stored separately and can query across partitions, but writes must update a remote partition, adding latency. Global indexes also create hot partitions (a single partition receiving disproportionate traffic) if the index key has skewed distribution.

When to Use

Add secondary indexes when read patterns justify the write cost. A query running 1000x/day saving 100ms each justifies an index. A query running 10x/day may not justify doubling write costs.

💡 Key Takeaways
Write amplification: 2 secondary indexes means 3x the writes (base table + each index)
Synchronous index updates guarantee consistency but add latency; async is faster but temporarily stale
Local secondary indexes: co-located with data, fast but limited to partition queries
Global secondary indexes: cross-partition queries but add write latency and hot partition risk
📌 Interview Tips
1Write cost math: 10K writes/sec with 2 secondary indexes = 30K effective writes, tripling throughput cost
2Async index staleness: write completes in 5ms, index update happens 200ms later, query in between misses new data
3Index justification: query saves 100ms × 1000/day = 100 seconds saved; compare to write cost increase
← Back to Indexing Strategies Overview