Scenario
You are building the “Trending Searches” feature for a large e-commerce platform.
Every day, the search system processes:
500 million search queries
Millions of unique search terms
Queries coming from multiple regions
Multiple search clusters
The product team wants:
Trending Searches
1. iphone 17
2. air fryer
3. java spring boot
4. running shoes
5. wireless headphonesProblem
Design a system that returns the Top K most frequently searched queries in near real-time.
The API should expose:
GET /trending-searches?k=10Response:
[
{
"query": "iphone 17",
"count": 1254300
},
{
"query": "air fryer",
"count": 985000
}
]Initial Implementation
📢 Get actionable Java and Spring Boot insights every week, including practical code tips and real-world, use-case-based interview questions, to help you level up your backend skills—join 8300+ subscribers for hand-crafted, no-fluff content.
Upgrade to paid now (60% discount) and get the annual membership at $50/year forever that is ~ $4/mo.
Testimonials
Why does this implementation not work at production scale?
The algorithm requires loading all queries into memory. For example:
500 million queries/day
+
millions of unique terms
=
Huge memory requirementThe map grows with every unique query: Map<String,Integer>
Example:
{
"iphone": 100000,
"java": 50000,
"docker": 30000
}Problems:
High memory usage
Cannot handle continuous streams
Requires full dataset before calculation
Expensive sorting, sorting has O(N log N) time complexity
How would you redesign this for real-time trending searches?
The system should separate ingestion, aggregation, and serving. This prevents expensive computations from happening every time a user requests trending searches.






