Search Sreeni's Insights

Filter by content type
technical February 10, 2025 12 min read

Building Resilient Distributed Systems

Lessons learned from scaling infrastructure at hypergrowth startups. Exploring patterns for fault tolerance, graceful degradation, and observability.

Share:

Building Resilient Distributed Systems

When systems grow beyond a single server, everything changes. The assumptions that worked at small scale become liabilities. Network partitions happen. Services fail. Latency spikes occur at the worst possible moments.

The Fallacies of Distributed Computing

Peter Deutsch’s eight fallacies remain as relevant today as when they were written:

  1. The network is reliable
  2. Latency is zero
  3. Bandwidth is infinite
  4. The network is secure
  5. Topology doesn’t change
  6. There is one administrator
  7. Transport cost is zero
  8. The network is homogeneous

Every distributed system bug I’ve debugged traces back to violating one of these assumptions.

Patterns for Resilience

Circuit Breakers

When a downstream service fails, continuing to send requests only makes things worse. Circuit breakers prevent cascade failures:

class CircuitBreaker {
  private failures = 0;
  private lastFailure: Date | null = null;
  private state: 'closed' | 'open' | 'half-open' = 'closed';

  async call<T>(fn: () => Promise<T>): Promise<T> {
    if (this.state === 'open') {
      if (this.shouldAttemptReset()) {
        this.state = 'half-open';
      } else {
        throw new Error('Circuit is open');
      }
    }

    try {
      const result = await fn();
      this.onSuccess();
      return result;
    } catch (error) {
      this.onFailure();
      throw error;
    }
  }
}

Bulkheads

Isolate failures to prevent them from taking down the entire system. Separate thread pools, connection pools, and rate limits per dependency.

Timeouts and Retries

Every network call needs a timeout. Every retry needs exponential backoff with jitter:

const retry = async <T>(
  fn: () => Promise<T>,
  maxAttempts: number = 3
): Promise<T> => {
  for (let attempt = 1; attempt <= maxAttempts; attempt++) {
    try {
      return await fn();
    } catch (error) {
      if (attempt === maxAttempts) throw error;
      
      const delay = Math.min(1000 * Math.pow(2, attempt), 10000);
      const jitter = Math.random() * 1000;
      await sleep(delay + jitter);
    }
  }
  throw new Error('Unreachable');
};

Observability is Non-Negotiable

You can’t fix what you can’t see. Instrument everything:

  • Metrics: Request rates, error rates, latency percentiles
  • Traces: Follow requests across service boundaries
  • Logs: Structured, correlated, searchable

The goal isn’t to collect data—it’s to answer questions quickly when things go wrong.

Conclusion

Resilience isn’t a feature you add at the end. It’s a property that emerges from deliberate design decisions made throughout the development process. Start with the assumption that everything will fail, and design accordingly.

Related Posts