MediumIntermediate

E-Commerce Product Search

SearchDatabasesCachingAPI Design

Problem Statement

ShopSearch is building a search service for a mid-size e-commerce platform with 5 million products. The search must support:

- Full-text search - search product titles, descriptions, and attributes. Handle typos ("blue jens" → "blue jeans") and synonyms ("couch" = "sofa").Faceted filtering - after searching, users filter by brand, price range, color, size, rating, and availability. Facet counts update in real time as filters are applied.Autocomplete - as-you-type suggestions showing matching products and categories in < 100 ms.Relevance ranking - rank results by text relevance, sales volume, rating, and (optionally) paid boost from sellers.Personalization - re-rank results based on the user's browsing/purchase history.Search analytics - track popular queries, zero-result queries, and click-through rates to improve the search quality.

Handle 10,000 search queries per second at peak (Black Friday).

What You'll Learn

Design a product search engine with faceted filters, autocomplete, spell correction, and relevance ranking for 5 M products. Build this architecture under realistic production constraints, then validate tradeoffs in the design lab simulation.

SearchDatabasesCachingAPI Design

Constraints

Products5,000,000
Search QPS (peak)~10,000
Search latency (P95)< 200 ms
Autocomplete latency< 100 ms
Facets updated per query~10 filter categories
Availability target99.95%
ApproachClick to expand

Interview-Ready Approach

1) Clarify Scope and SLOs

  • Problem statement: Design a product search engine with faceted filters, autocomplete, spell correction, and relevance ranking for 5 M products.
  • Design for a peak load target around 5,000 RPS (including burst headroom).
  • Products: 5,000,000
  • Search QPS (peak): ~10,000
  • Search latency (P95): < 200 ms
  • Autocomplete latency: < 100 ms
  • Facets updated per query: ~10 filter categories

2) Capacity Planning Method

  • Convert traffic and growth constraints into request rate, storage growth, and concurrency budgets.
  • Keep at least 2-3x safety margin per tier (ingress, compute, storage, async workers).
  • Reserve explicit latency budgets per hop so p95 can be defended in review.

3) Architecture Decisions

  • Search: Use primary store for writes and async index updates for search relevance + scale.
  • Databases: Define a clear system-of-record and design read/write paths separately before adding optimizations.
  • Caching: Put cache on hot read paths first and pick cache-aside or write-through explicitly.
  • API Design: Standardize API boundaries, idempotency keys, pagination, and error contracts first.

4) Reliability and Failure Strategy

  • Track indexing lag and support reindex from source of truth.
  • Use strong write constraints (transactions or conditional writes) and explicit backup/restore strategy.
  • Bound staleness with TTL + invalidation hooks for critical entities.
  • Apply strict input validation and backward-compatible versioning.

5) Validation Plan

  • Run one peak-load test, one dependency-degradation test, and one failover test.
  • Verify idempotency for all retried writes and async consumers.
  • Track user-facing SLOs first: p95 latency, error rate, and successful throughput.

6) Trade-offs to Call Out in Interviews

  • Search: Search index gives rich querying but introduces eventual consistency and index ops overhead.
  • Databases: SQL gives stronger transactional guarantees; NoSQL often gives better write scaling and flexibility.
  • Caching: Higher hit rate cuts latency/cost, but stale data and invalidation bugs become primary risks.
  • API Design: Rich APIs improve developer speed but can create long-term compatibility burden.

Practical Notes

  • Elasticsearch is the standard choice: inverted index for full-text, aggregations for facets, suggesters for autocomplete.
  • Keep the search index in sync with the product catalog via Change Data Capture (CDC) or a message queue.
  • Autocomplete: use an edge n-gram tokenizer to match partial words as the user types.

Learn the Concept

Practice Next

Reference SolutionClick to reveal

Why This Solution Works

Request path: The solution keeps ingress, service logic, and stateful dependencies separated so each layer can scale independently.

Reference flow: Web Clients -> Load Balancer -> API Gateway -> API Service -> Primary SQL DB -> Redis Cache -> Search Index

Design strengths

  • Cache sits on the read path to absorb repeated queries and keep DB pressure stable.

Interview defense

  • This design makes bottlenecks explicit (ingress, core compute, persistence, async workers).
  • It supports progressive scaling without re-architecting the core request path.
  • It keeps correctness-sensitive state changes in durable systems while offloading background work asynchronously.