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:
Design a system that returns the Top K most frequently searched queries in near real-time.
The API should expose:
Response:
[
{
"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
The algorithm requires loading all queries into memory. For example:
The map grows with every unique query: Map<String,Integer>
Problems:
High memory usage
Cannot handle continuous streams
Requires full dataset before calculation
Expensive sorting, sorting has O(N log N) time complexity
The system should separate ingestion, aggregation, and serving. This prevents expensive computations from happening every time a user requests trending searches.

Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.