KatInterview

Design a Cost-Reduction Gateway for Metered Upstream APIs

System DesignModel answer
MediumModel answerSystem Designcachingrate-limitingmicroservices

Last updated

Design a shared proxy that sits between internal microservices and pay-per-call APIs, cutting spend through caching, request collapsing, batching, and budget enforcement.

Background

Your organization runs dozens of microservices, and a meaningful slice of the monthly infrastructure bill comes from calls to metered API endpoints — third-party data providers, payment processors, enrichment services, and internal platforms that chargeback per request. As the number of services grows, so does duplicate and redundant outbound traffic.

Your task is to design a shared gateway layer that intercepts those outbound calls and reduces how many actually reach the upstream, without changing what the calling service receives. For every technique you propose, explain where the saving comes from, when it is safe to apply, and what could go wrong.

Assume endpoints charge per request, that traffic is a mix of read-style lookups and writes that must never be sent twice, and that the gateway can be introduced gradually. No concrete scale or pricing is provided — state your own assumptions whenever you need numbers.

Constraints

  • A service must never receive someone else's data, a response that is too stale for its use case, or the side-effect of a duplicated write.
  • The gateway itself adds a network hop and a processing step; its latency impact and its own availability are part of the design.
  • The gateway costs money to operate; that cost must be smaller than the savings it generates.

Useful questions to consider (clarifications)

  • What makes up the bill: per-call charges, data egress, or both?
  • Which endpoints are read-only, and what freshness do callers actually need?
  • Do any upstreams offer batch endpoints or volume pricing tiers?
  • Which services and which endpoints account for the majority of spend today?
  • Can services change their code to point at the gateway, or must interception be transparent?

Part 1 — Requirements and placement

Define what the gateway must and must not do. Decide where it runs — a centralized gateway process, a sidecar next to each service, or an in-process client library — and justify that choice. Sketch the main components and walk through the lifecycle of one request.

Cover:

  • Functional and non-functional requirements, including latency and availability targets for the gateway itself
  • Placement decision with trade-offs
  • Component diagram (described in prose or ASCII)
  • How existing services adopt it without a big-bang migration

Part 2 — Mechanisms that reduce cost

Propose the concrete techniques the gateway uses to reduce billable calls or the cost per call. For each one, state when it is safe to apply.

Cover:

  • Response caching: what is cacheable, how cache keys are constructed, and how freshness is managed
  • Collapsing multiple in-flight identical requests into one upstream call
  • Batching separate requests from different callers into a single upstream call
  • Per-service quotas, rate limits, and spending budgets
  • Routing to cheaper providers, pricing tiers, or alternative request shapes

Part 3 — Correctness, failures, and cost visibility

Explain how you avoid the three correctness risks from the constraints section: data leakage across callers, serving stale data, and double-charging writes on retry. Then cover what happens when things break, and how you prove the gateway is actually saving money.

Cover:

  • Safe retries and idempotency handling for write-style requests
  • Fail-open vs. fail-closed behavior for gateway failures, cache failures, and upstream failures
  • Per-service cost metering and attribution
  • Metrics and alerts that demonstrate savings and catch regressions

Hints

Hint 1

Start with measurement, not mechanism. Before designing any saving technique, establish how you'd know which services call which endpoints and how often — that shapes every other decision.

Hint 2

Think carefully about what makes a cache key safe. The same request payload from two different services, or two different tenants, may not be interchangeable. Leaking data between callers is worse than making an extra billable call.

Hint 3

Writes need a different contract than reads everywhere in the design — retries, caching, request collapsing, and failure policy all need separate treatment for anything that mutates state.