| Users / Queries | 100 Users | 10K Users | 1M Users | 100M Users |
|---|---|---|---|---|
| Search Queries per Second (QPS) | 10 QPS | 1,000 QPS | 50,000 QPS | 5,000,000 QPS |
| Data Size (Indexed Items) | 10K items | 1M items | 100M items | 10B items |
| Index Size | Small (few GB) | Medium (hundreds GB) | Large (TBs) | Very Large (PBs) |
| Latency Expectation | <100 ms | <200 ms | <300 ms | <500 ms |
| Infrastructure | Single server with local index | Cluster with distributed index | Multi-region clusters with replication | Global distributed system with sharding and CDN |
| Filter Complexity | Simple filters (few fields) | Moderate filters (multi-field) | Complex filters with facets and ranges | Highly dynamic filters with personalization |
Search and filter design in LLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
The first bottleneck is the search index and query processing. As users and data grow, the index size increases, making queries slower. The CPU and memory on the search servers become overwhelmed handling complex filters and high QPS.
- Horizontal scaling: Add more search nodes to distribute query load and index shards.
- Index sharding: Split the index into smaller parts by data ranges or categories to reduce query scope.
- Caching: Cache frequent queries and filter results to reduce repeated computation.
- Pre-aggregation: For filters, precompute counts or facets to speed up filtering.
- Load balancing: Use smart routing to send queries to the least busy nodes.
- Use of CDN: For static filter metadata or autocomplete suggestions, serve from CDN to reduce backend load.
- Asynchronous processing: For complex filters, consider background jobs to prepare results.
At 10K users generating 1,000 QPS, each query touching 1MB of index data means 1GB/s data throughput. This requires multiple servers with fast SSDs and high network bandwidth.
Storage for 1M items with index size ~100 bytes per item is ~100MB, but with inverted indexes and facets, it can grow to 100GB+.
At 1M users and 50,000 QPS, network bandwidth and CPU become critical. Each server can handle ~5,000 QPS, so at least 10 servers are needed just for query handling.
Start by clarifying the scale and query patterns. Discuss data size, query complexity, and latency needs. Identify bottlenecks early (index size, CPU, memory). Propose incremental scaling: caching, sharding, horizontal scaling. Mention trade-offs like consistency and freshness of index.
Your search database handles 1,000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Add horizontal scaling by adding more search nodes and shard the index to distribute the query load. Also, implement caching for frequent queries to reduce load.
Practice
Solution
Step 1: Understand the role of filters in search
Filters help users reduce the number of results by selecting specific criteria.Step 2: Identify the effect of filters on results
Filters narrow results to match user preferences, making search faster and more relevant.Final Answer:
To narrow down search results based on user preferences -> Option CQuick Check:
Filters narrow results = C [OK]
- Thinking filters increase results
- Assuming filters slow down search intentionally
- Confusing filters with UI removal
Solution
Step 1: Understand comparison operators in queries
The symbol '<' means less than, so 'price<100' filters prices below 100.Step 2: Eliminate incorrect operators
'>' means greater than, '=' means equal, '!=' means not equal, so they don't match 'less than 100'.Final Answer:
price<100 -> Option BQuick Check:
Less than operator = A [OK]
- Using '>' instead of '<' for less than
- Confusing '=' with less than
- Using '!=' which means not equal
category='books' and price < 20, which data structure best supports fast filtering?Solution
Step 1: Analyze filtering needs
Filtering by category and price requires quick lookup by category and efficient price range queries.Step 2: Choose data structure supporting these queries
A hash map keyed by category allows fast category lookup; sorted price lists enable quick filtering by price.Final Answer:
A hash map keyed by category with sorted price lists -> Option AQuick Check:
Hash map + sorted list = B [OK]
- Using unsorted lists causing slow searches
- Using queue or stack which don't support efficient filtering
- Ignoring the need for sorting by price
Solution
Step 1: Understand date comparison issues
Comparing dates stored as strings can cause wrong order because string comparison is lexicographic.Step 2: Identify why this causes incorrect results
Dates like '12/01/2023' and '02/12/2023' compared as strings may not sort correctly, causing wrong filter results.Final Answer:
Date values are stored as strings and compared lexicographically -> Option AQuick Check:
String date comparison causes errors = A [OK]
- Assuming numeric comparison works on strings
- Ignoring index relevance
- Thinking missing filter param causes wrong filtered results
Solution
Step 1: Consider scalability and performance needs
Millions of products require fast, scalable search with flexible filters.Step 2: Evaluate options for search and filtering
Distributed search engines with inverted indexes enable fast text search; faceted filters allow flexible attribute filtering efficiently.Step 3: Eliminate inefficient approaches
Scanning large SQL tables or in-memory filtering is slow and not scalable; key-value stores lack complex search capabilities.Final Answer:
Use a distributed search engine with inverted indexes and faceted filters -> Option DQuick Check:
Distributed search + faceted filters = D [OK]
- Relying on full table scans for large data
- Ignoring indexing for search speed
- Using memory-heavy filtering for millions of items
