Engineering With Java

Engineering With Java

Spring Boot Interview Question : Building Trending Search Queries at Scale

Everyone searched for the same thing. Kafka sent it to the same place. Chaos followed

Suraj Mishra's avatar
Suraj Mishra
Aug 18, 2026
∙ Paid

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 headphones

Problem

Design a system that returns the Top K most frequently searched queries in near real-time.

The API should expose:

GET /trending-searches?k=10

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.

So far we have covered 70+ real world based interview questions and will add up to 100 by end of this year.

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 requirement

The 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.

User's avatar

Continue reading this post for free, courtesy of Suraj Mishra.

Or purchase a paid subscription.
© 2026 Suraj Mishra · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture