KatInterview

Designing a Modular and Fault-Tolerant Retry Library

System DesignModel answer
MediumModel answerSystem DesignAPI DesignFault ToleranceConcurrency

Last updated

Design a pluggable retry library that supports configurable backoff, jitter strategies, stopping criteria, cancellation timeouts, and dynamic retry headers.

Problem Statement

When building resilient distributed applications, transient network failures and service disruptions are inevitable. Designing a robust, client-side retry library helps shield downstream services and handle transient faults gracefully.

Your goal is to design the architecture and public interface for a reusable, pluggable Retryer library component that executes a given operation and automatically retries it upon failure.


Core Requirements

  1. Default Capabilities

    • Out-of-the-box support for execution with configured retries.
    • Configurable maximum attempt counts (or retry budgets).
    • Flexible backoff algorithms (e.g., fixed delay, exponential backoff).
    • Jitter techniques to avoid synchronization issues across concurrent callers.
    • Configurable success/failure rules (e.g., matching exception types or inspectable HTTP response status codes).
    • Respect for deadlines, context timeouts, or cancellation signals.
  2. Extensibility & Architecture

    • Decouple the execution loop from decisions on backoff calculation, failure identification, and monitoring.
    • Allow callers to plug in custom strategies without modifying the retry runner's core implementation:
      • Custom backoff policies.
      • Custom failure predicates (checking exceptions or execution output).
      • Event observers (metrics, logging, tracing).
      • Dynamic server delay hints (e.g., parsing a Retry-After header).
  3. Usability & Convenience

    • Provide clean, developer-friendly interfaces for common use cases (e.g., sensible defaults, procedural calls, or function decorators) while exposing full strategy configuration for advanced usages.

Key Deliverables

  • Propose the object model and public interface (types, classes, or interfaces).
  • Detail the internal algorithm governing the execution loop.
  • Demonstrate how customization hooks integrate cleanly using object composition or standard patterns.
  • Discuss operational edge cases, safety concerns (e.g., thundering herd, non-idempotency, deadline propagation), and complexity.

Hints

Hint 1

Decouple the loop driver from decisions. Structure backoff, jitter, and retry evaluation into separate, composable strategy interfaces.

Hint 2

Ensure the core loop inspects both thrown exceptions AND valid returned values (e.g., HTTP 500 responses without thrown exceptions).

Hint 3

Ensure non-retryable exceptions are re-thrown immediately rather than being masked by a generic execution wrapper.