KatInterview

Building a Resilient HTTP Client with Retries and Rate Limiting

System DesignModel answer
MediumModel answerSystem Designapi-designerror-handlingdistributed-systems

Last updated

Design and implement a production-grade HTTP client that handles network failures, respects rate limits, and ensures safe retries.

You're implementing a client library that calls an external HTTP API in a production environment. The service you're integrating with experiences occasional failures, enforces rate limits, and expects clients to handle errors intelligently.

Design a make_request(url, method, payload, headers) function that demonstrates production-ready practices for HTTP communication.

Your Solution Should Address

Error Classification and Recovery

  • Which HTTP status codes and exceptions warrant a retry, and which should fail immediately?
  • How do you implement exponential backoff without creating retry storms?

Rate Limit Management

  • How do you respect the server's Retry-After header?
  • What client-side mechanisms prevent your code from overwhelming the API?

Safe Retry Semantics

  • Why are POST requests different from GET requests when retrying?
  • How do you prevent duplicate operations (duplicate charges, double-creates) when network errors make the outcome ambiguous?

Production Observability

  • What metrics and logs would you emit to diagnose issues in a live system?

Constraints and Expectations

  • Implement retry logic from scratch (no third-party retry libraries)
  • Handle 429 Too Many Requests and 503 Service Unavailable explicitly
  • Provide concrete code demonstrating the retry mechanism
  • Explain the reasoning behind your design choices, including what could go wrong if you skip each safeguard

Hints

Hint 1

Classify every failure into three categories: success, transient (worth retrying), and permanent (will never succeed). Only transient failures should trigger retries.

Hint 2

For POST and PATCH operations, generate an idempotency token once before the first attempt and reuse it on every retry. This lets the server deduplicate safely.

Hint 3

Add jitter to your exponential backoff—if all clients retry at the same interval, they'll hammer the recovering service simultaneously.