Isolate failures
One slow tenant or one slow endpoint consumes all threads and starves the rest.
Say it like this
One slow provider should not stall every other channel, so I give each dependency and each priority its own worker pool and queue: a bulkhead so a Twilio outage fills the SMS compartment and leaves push untouched.
Basic solution
Partition resources: separate pools, queues or instances per tenant, priority or dependency.
One pool per channel
Each channel has its own queue and worker pool. When Twilio stalls, only the SMS pool fills; push and email keep draining at full speed.
How it works
- List the resources that requests compete for inside the service and between services: worker threads, database and HTTP connections, queue capacity, memory, and the rate limits of each downstream provider.
- Split each resource along the dimension that fails independently. For a notification system that is per dependency (APNs, Twilio, SES) and per priority tier (transactional, marketing); for a multi-tenant API it is per tenant; for a large fleet it is per cell.
- Give every compartment a hard cap that it cannot exceed: a thread pool of 20 for one provider, a connection pool of 10, a queue that holds at most 10,000 messages. The cap is what stops a failure spreading.
- Decide which compartment a piece of work belongs to as early as possible, at the gateway, in the router worker or in the queue topology, and route it there. A request never borrows a thread or a connection from another compartment.
- When a compartment is full, work for that compartment waits in its own queue or is rejected; the other compartments keep their full capacity and never notice.
- Record how full each compartment is (threads busy, queue depth) so you can see which compartment is flooding before users do.
Why it works
- A failure consumes resources until something caps it. Capping per compartment turns a fleet-wide stall into a contained one.
Worked example
Notification system with three channels
- 2,900 notifications/s average; sender workers call APNs, Twilio and SES with a 2 s timeout each.
- Single shared pool of 200 worker threads. Twilio starts timing out: every SMS send holds a thread for 2 s. At 870 SMS/s (30% of traffic) that is 1,740 thread-seconds per second, more than the pool. Within 10 seconds all 200 threads are stuck in Twilio calls and push and email stop too.
- Bulkheads: three pools, push 100 threads, SMS 60, email 40, each fed by its own queue. The same Twilio outage saturates the SMS pool in 10 seconds; the SMS queue grows at 870 messages/s and is drained later. Push and email never notice.
- Add a priority dimension: transactional SMS gets 40 of the 60 threads and its own queue, so a marketing blast still cannot starve OTPs even inside the SMS compartment.
- Cost: the same 200 threads, but no compartment can use more than its share, so push peaks above 100 concurrent calls now queue briefly. That is the trade.
Variants and when each wins
Thread or connection pool per dependency
- Inside one process, each downstream service gets its own pool of threads or connections, so a slow provider can exhaust only its own pool. It wins as the cheapest first step inside a single service.
Separate queues per channel and priority
- Between services, each channel and priority tier gets its own queue and its own consumers, so a flood in one queue cannot delay another and each can scale on its own. It wins for pipelines, where it is the standard answer.
Per-tenant quotas
- Each customer gets a cap on concurrency or queue depth, so one noisy tenant cannot starve the rest. It wins for multi-tenant APIs, provided every request carries a tenant id to key the quota on.
Cells (shuffle sharding)
- The whole stack (servers, caches, databases) is split into independent cells and each customer is assigned to one cell or a small random set of cells. It wins once a single failure affecting every customer is unacceptable, because a failure takes out one cell and a bounded fraction of customers.
Recommendation: pools per dependency inside a service, queues per channel and priority between services, and cells once a single failure affecting all customers is unacceptable.
Failure modes
Compartment too small
- Push notifications start queueing during a normal evening peak even though the rest of the fleet is idle. It happens because the pool was sized for the average rate, not the peak. Size each pool from p99 concurrency, which is request rate multiplied by latency, add 30% headroom, and alert when a pool is saturated.
Hidden shared resource
- The bulkheads are in place, yet one provider outage still stalls everything. It happens because every pool shares one database connection pool or one Redis client underneath, and that shared resource fills up. Trace every resource a request touches, partition the shared one too, or give it its own bulkhead with a short timeout.
Routing leaks between compartments
- Marketing messages appear in the transactional queue and OTPs wait behind them. It happens because a producer set the wrong priority or relied on a default. Classify each message at the router from its template, not from whatever the caller claimed.
Too many compartments
- Dozens of tiny pools are each starved while the fleet as a whole is underused. It happens because the resources were partitioned on every dimension anyone could think of. Partition only on dimensions that genuinely fail independently; two or three is usual.
Full compartment with no backpressure
- The SMS queue grows without limit during a Twilio outage and the broker runs out of disk. It happens because nothing bounds the queue when its consumers stall. Set a maximum depth per queue with a policy to drop or defer beyond it, and alert on the growth rate.
Interviewer follow-ups
Answer out loud before you open each one.
How do you size each compartment?
Concurrency needed is rate times latency: 870 SMS/s at 50 ms is 44 concurrent calls, so 60 threads with headroom. Measure p99 latency, not average, and re-check after a provider changes. Leave the sum of compartments within what the host can run.
What is the difference between a bulkhead and a circuit breaker?
A bulkhead limits how much of your capacity a dependency can consume; a breaker stops calling the dependency when it is failing. The bulkhead contains the damage, the breaker shortens it. With both, a slow dependency fills its small pool, the breaker trips on the timeouts, and the pool empties again.
How does shuffle sharding improve on plain cells?
With plain cells a bad customer takes out everyone in their cell. With shuffle sharding each customer gets a random subset of nodes, so two customers rarely share the exact same subset. A poison request from one customer affects only the nodes it hits, and most other customers still have healthy nodes in their subset.
Where do you put the bulkhead for a database?
At the connection pool, partitioned by workload class: a pool for interactive reads, a smaller one for background jobs, a tiny one for reporting. Then a runaway report cannot take the connections the checkout path needs. Add statement timeouts per class as well.
Connections
- In study paths
- Staying up under load
- Pairs with
- Keep urgent work ahead of bulk work · Stop calling a failing dependency · Never wait forever · Drop load before you fall over · Pool database connections
- Pulls against
- Scale out, not up
- Used in
- Notification System
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon EC2 Auto Scaling | Router, Push pool: 100, SMS pool: 60, Email pool: 40 | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |
| Amazon SQS | push queue, sms queue, email queue | Pub/Sub, Cloud Tasks | Service Bus queues, Queue Storage | RabbitMQ, ActiveMQ |
| Amazon SES | SES | No first-party service; SendGrid via Marketplace | Azure Communication Services Email | SendGrid, Mailgun, Postmark |