Design an API Aggregator
✈️ What is an API aggregator? It is a service that asks many outside systems the same question and merges their answers into one. A flight search site is the classic example: you type "San Francisco to New York", and behind the scenes the site asks a dozen airlines and travel sites for prices at the same time, then shows one sorted list. The hard part is that the outside systems are slow, flaky and all different.
Interviewers ask this problem under several names: "an API aggregator", "many external providers behind one interface", "a flight price aggregator", or "call N services with a timeout and return partial results". They are the same design. We build it as a flight price aggregator, because concrete fares make the rules easy to follow. The provider interface covers the "one interface" version, the parallel calls with a deadline cover the timeout version, and the result object that reports which providers are missing covers partial results.
This problem is less about class hierarchies and more about behavior under failure. It tests whether you can keep an orchestrator simple while it deals with threads, deadlines and partial answers.
Requirements
"Design a flight price aggregator. It queries several providers for fares and shows the user one combined list. Providers can be slow or down."
Clarifying Questions
You: Do all providers have the same API?
Interviewer: No. Each airline and travel site has its own format: one returns prices in cents, another in dollars as strings, field names differ. We will add new providers over time.
You: How long can one search take?
Interviewer: The page should answer within half a second. If a provider has not answered by then, show what we have.
You: So a slow or failing provider should not fail the whole search?
Interviewer: Right. Return the fares we got, and tell the caller which providers are missing and why, so the page can say "some results may be incomplete".
You: What if two providers sell the same flight?
Interviewer: Show the flight once, at the cheapest price, with the provider that offers it. Sort the list by price.
You: What about a provider that is down for an hour? Do we keep asking it on every search?
Interviewer: No. If it fails three searches in a row, stop asking it for a minute, then try again.
You: Is the aggregator shared by many users at once?
Interviewer: Yes, many searches run at the same time.
You: Do we book the flight, or cache results?
Interviewer: No booking. Caching is a good follow-up, but not now.
The second and third answers shape the whole design. "Answer within half a second, with what you have" rules out calling providers one after another, and it means a search returns two things: the fares and a status for each provider.
💡 Interview tip: When a problem involves calls to outside systems, ask three questions early: how long can we wait, what do we do when one fails, and is the result useful when some parts are missing. The answers decide whether you need parallel calls, a deadline and partial results.
Final Requirements
- Providers sit behind one interface. Adding a provider must not change the aggregator.
- A search asks every provider for fares on a route, in parallel.
- A search never waits longer than a fixed timeout. Providers that have not answered by then count as timed out.
- The result lists each flight once, at the lowest price among providers that answered in time, sorted by price, then by flight number. Ties on price for the same flight go to the provider registered first.
- The result also reports every provider's status:
ok,timeout,errororskipped. - A provider that fails (timeout or error) 3 searches in a row is skipped for the next 60 seconds. After that it is asked again with a clean count. Any successful answer resets the count.
- Many searches can run at the same time.
Out of scope
- Booking, payment and seat availability.
- Real HTTP clients, authentication and rate limits of the providers.
- Currency conversion (all prices are in one currency).
- Caching results (we come back to it in Extensibility).