| Users / Queries | 100 Users | 10,000 Users | 1 Million Users | 100 Million Users |
|---|---|---|---|---|
| Query Volume (QPS) | ~100 QPS | ~1,000 QPS | ~10,000 QPS | ~100,000 QPS |
| Data Size | MBs to low GBs | GBs to tens of GBs | Hundreds of GBs to TBs | Multiple TBs to PBs |
| Index Size | Small, fits in memory | Larger, partial in memory | Large, needs optimized storage | Very large, requires partitioning |
| Query Latency | Low (ms) | Low to moderate | Moderate to high if unoptimized | High without advanced indexing |
| Index Maintenance Cost | Low | Moderate | High, impacts write speed | Very high, requires tuning |
Database indexing in HLD - Scalability & System Analysis
As user count and query volume grow, the first bottleneck is usually the database query performance. Without proper indexing, queries become slow because the database scans large tables. Indexes speed up reads but add overhead on writes. At medium scale, index maintenance and memory usage become bottlenecks. At very large scale, index size and disk I/O limit performance.
- Index Optimization: Use appropriate index types (B-tree, hash, composite) based on query patterns.
- Partial and Covering Indexes: Index only needed columns to reduce size and improve speed.
- Index Maintenance: Schedule index rebuilds and updates during low traffic to reduce write impact.
- Horizontal Scaling: Shard data to distribute indexes across multiple database instances.
- Caching: Use in-memory caches (Redis, Memcached) to reduce database hits for frequent queries.
- Read Replicas: Offload read queries to replicas with their own indexes to reduce load on primary.
- Partitioning: Split large tables and indexes by range or hash to improve query and maintenance performance.
Assuming 10,000 QPS at medium scale:
- Each query reads indexed data in ~1-5 ms.
- Index size can be 10-30% of data size; for 1 TB data, index ~100-300 GB.
- Memory needed to hold hot indexes: tens of GBs to hundreds of GBs.
- Write overhead: each insert/update requires index update, increasing write latency by ~10-30%.
- Network bandwidth depends on replication and sharding; expect 1-10 Gbps for large scale.
When discussing database indexing scalability, start by explaining how indexes improve read speed but add write overhead. Then describe how index size and maintenance become bottlenecks as data grows. Finally, outline solutions like index optimization, partitioning, caching, and sharding. Use clear examples and relate to real-world query patterns.
Your database handles 1000 QPS with indexes. Traffic grows 10x to 10,000 QPS. What do you do first and why?
Answer: The first step is to analyze if the current indexes and hardware can handle the increased read load. Likely, the database CPU or disk I/O will become bottlenecks. To fix this, add read replicas to distribute read queries and reduce load on the primary. Also, consider caching frequent queries to reduce database hits. This approach improves read scalability without immediately changing indexes or hardware.
Practice
Solution
Step 1: Understand what an index does
An index helps the database find data faster by creating a quick lookup structure.Step 2: Identify the main benefit
This quick lookup reduces the time needed to search large tables.Final Answer:
To speed up data retrieval by allowing quick lookups -> Option BQuick Check:
Index = Faster data search [OK]
- Thinking indexes store extra data copies
- Confusing indexes with backups
- Assuming indexes encrypt data
user_id in SQL?Solution
Step 1: Recall SQL syntax for creating indexes
The standard syntax isCREATE INDEX index_name ON table_name (column_name);Step 2: Match the syntax with options
CREATE INDEX idx_user ON users (user_id); matches the correct syntax exactly.Final Answer:
CREATE INDEX idx_user ON users (user_id); -> Option AQuick Check:
CREATE INDEX ... ON ... (column) [OK]
- Swapping table and column names
- Using wrong keyword order
- Missing parentheses around column
orders with an index on order_date, what will be the output of the query plan for SELECT * FROM orders WHERE order_date = '2023-01-01';?Solution
Step 1: Understand how indexes affect query plans
An index onorder_dateallows the database to quickly locate rows matching the date.Step 2: Identify the expected query plan
The database will perform an index seek to find matching rows efficiently, not scan the whole table.Final Answer:
Index seek on order_date to quickly find matching rows -> Option DQuick Check:
Indexed column = index seek [OK]
- Assuming full table scan despite index
- Confusing sorting with filtering
- Ignoring index usage in query plan
Solution
Step 1: Analyze index usage conditions
Indexes only help if queries use the indexed column in WHERE, JOIN, or ORDER BY clauses.Step 2: Identify why no speed improvement occurs
If queries don't use the indexed column, the index is ignored, so no speed gain.Final Answer:
The queries do not filter or sort using the indexed column -> Option CQuick Check:
Index helps only if column used in query [OK]
- Assuming syntax error disables index
- Believing all indexes slow queries
- Thinking database disables indexes by default
status and created_at. Which indexing strategy balances read speed and write overhead best?Solution
Step 1: Understand composite vs separate indexes
A composite index on (status, created_at) helps queries filtering on both columns efficiently.Step 2: Consider write overhead and query patterns
Separate indexes increase write overhead more than one composite index and may be less efficient for combined filters.Step 3: Exclude unsuitable options
No indexes hurt read speed; full-text index is for text search, not filtering by status/date.Final Answer:
Create a composite index on (status, created_at) -> Option AQuick Check:
Composite index = balanced read/write [OK]
- Creating many separate indexes causing slow writes
- Skipping indexes hurting read performance
- Using full-text index for non-text filtering
Ready to prove it?
Pass the test and this pattern moves unknown
