Building Resilient Distributed Systems
Lessons learned from scaling infrastructure at hypergrowth startups. Exploring patterns for fault tolerance, graceful degradation, and observability.
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:
- The network is reliable
- Latency is zero
- Bandwidth is infinite
- The network is secure
- Topology doesn’t change
- There is one administrator
- Transport cost is zero
- 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
Monolith vs Microservices - should we start with microservices?
Monoliths and microservices compared through modular boundaries, extraction signals, scaling, team ownership, distributed failure, and practical starting criteria for growing teams.
What is CQRS - separating reads and writes, and when not to use it?
CQRS explained through commands, handlers, queries, read models, eventual consistency, and the situations where separating reads and writes is unnecessary.
API Gateway pattern - one front door for many services
The API Gateway pattern explained through routing, authentication, rate limiting, aggregation, caching, Azure API Management policies and ownership boundaries for practical guidance.