Tolerancia a Fallos
Resiliencia · Aislamiento de fallos
Escrito por Darjus Kazakevic · revisado el
Every remote call fails sometimes; the design decides how far the failure spreads.
A fault-tolerant system still has faults. The difference is that each fault has a known blast radius (the part of the system it can affect). Without design, failures spread. A slow dependency holds every caller's thread, the pool empties, and a database hiccup becomes a site outage.
The tools have their own guides: timeouts, retries with backoff, circuit breakers, bulkheads. This principle is what they are for. The main cost is a fallback for every wrapped call, because failing fast only helps if you know what to return instead. That is a product decision.
En esencia — Assume each dependency will fail, time out or slow down, and limit what that failure can take with it: timeouts, isolation, and a fallback decided in advance.
Cuándo recurrir a él
Recurre a él cuando
- Every request depends on remote calls that can fail, stall or slow down
- One slow dependency could drain a shared pool and stall everything
- A failure in one feature must not take down the whole page
Reconsidéralo cuando
- Local, in-process calls with no network or remote resource to fail
- A batch job that can simply be rerun from the start on any failure
- A single-user tool where a crash and a restart cost nothing
Conceptos clave
- Fault and failure
- A fault is one part going wrong: a disk, a node, a slow query. A failure is the whole system stopping service for users. Fault tolerance keeps faults from becoming failures.
- Blast radius
- How much of the system one fault can affect. Separate pools per dependency, separate zones, and cells (independent copies of the stack, each for a group of users) all make it smaller.
- Timeouts and retries
- A timeout limits how long a call may wait. A retry repeats a call that failed for a passing reason. Retries need a budget and backoff with jitter, or they multiply the load on a service that is already struggling.
- Circuit breaker
- A switch that stops calling a dependency after repeated failures and fails fast for a while. The dependency gets time to recover, and the caller's threads stay free.
- Fallback
- The answer you return when a call fails: an empty list, a cached value, a clear error. Choose it per call, with the product owner, before the incident.
Reglas de diseño
- Give every outbound call a timeout
- Without a timeout, a slow dependency holds a thread forever and the pool drains. Set a deadline on every remote call, shorter than the caller's own deadline.
- Retry only transient errors, within a budget
- Retry timeouts and 503s, not 400s. Use backoff with jitter, and cap retries at about 10% of traffic, so a struggling service is not hit three times as hard.
- Isolate each dependency
- Give each downstream service its own connection pool and concurrency limit. Then one slow service fills its own pool, not the one every other request needs.
- Decide the fallback before the failure
- Failing fast only helps if there is something sensible to return. Choose it per call with whoever owns the product: an empty rail for recommendations, a clear error for checkout.
Errores habituales
- Retries at every layer. Three layers with three tries each turn one failed request into 27 calls on the weakest service.
- A timeout longer than the caller's. The caller has already given up, but the work continues and holds resources.
- One shared connection pool for all dependencies. A single slow service fills it, and every request stalls.
- Fallbacks that are never tested. The first time the code runs is during the outage, and it throws.