← Building blocks

Pull broken instances out of rotation

Reliability & failure handlingfundamental

The load balancer keeps sending traffic to a crashed or overloaded server.

Say it like this

The balancer needs a signal that is true, cheap and local, so each instance exposes a liveness probe that says the process is alive and a readiness probe that says it can serve, and neither one calls a downstream dependency.

Basic solution

Expose liveness and readiness endpoints; the balancer or orchestrator removes failing instances and restarts them.

Kubernetes probesALB health checks/healthz

Readiness gates traffic, liveness gates restarts

The balancer polls readiness and stops routing to an instance after three failures. The orchestrator polls liveness and restarts a wedged process. Neither probe touches the database.

How it works

  1. Each instance exposes two HTTP endpoints. The liveness endpoint answers "is this process alive and not deadlocked?" The readiness endpoint answers "can this process serve requests right now?", meaning its config is loaded, its connection pool is open and its warm-up has finished.
  2. The load balancer calls the readiness endpoint every few seconds, say every 5 s with a 2 s timeout, and records whether each call succeeded.
  3. After N consecutive failures (say 3) the balancer marks the instance unhealthy and stops routing new requests to it. Requests already in flight on that instance are allowed to finish, which is called connection draining.
  4. After M consecutive successes the balancer adds the instance back into rotation.
  5. The orchestrator (Kubernetes, ECS) calls the liveness endpoint on its own schedule, and after repeated failures it kills and restarts the container.
  6. During a deploy, a new instance returns "not ready" until its warm-up completes, so the balancer sends it no traffic until it can serve well.
  7. During shutdown, the instance first flips readiness to failing, waits long enough for the balancer to notice and drain, then stops accepting connections and exits.

What a readiness check should test

  • Things local to the instance: the process can allocate a thread, the config is loaded, the local cache is warm enough. Not whether the database or a downstream service is reachable.

Worked example

URL shortener redirect fleet

  • 30 redirect instances behind a balancer, 40K redirects/s at peak, so about 1,300/s per instance.
  • Instance 12 deadlocks: its request threads are all stuck. Without health checks the balancer keeps sending it 1,300/s; all of those time out after 5 s. Users see 3.3% of redirects fail for as long as the instance is broken, which may be hours.
  • With readiness every 5 s and 3 failures: the balancer stops routing to it after about 15 s. Requests lost: about 15 s × 1,300/s = 20K, then zero. The orchestrator’s liveness probe fails too and restarts the container; it rejoins after warm-up, about a minute later.
  • Deploy: 30 instances replaced one at a time. Each new one takes 20 s to load the key cache and reports not ready until then. No request ever lands on a cold instance. Without readiness gating, each new instance would serve 20 s of slow, miss-heavy redirects.

Variants and when each wins

Shallow check

  • The endpoint returns 200 whenever the process can answer HTTP at all. It wins for catching crashes and deadlocks cheaply, but misses a process that is alive and broken, such as one whose config failed to load.

Local-state readiness

  • The endpoint checks the instance's own prerequisites: config loaded, pool initialised, warm-up done. It wins as the default, because it is cheap and reports only things this instance can fix.

Deep dependency check

  • The endpoint pings the database and downstream services. It is tempting but dangerous: when the database blips, every instance reports unhealthy at once and the balancer pulls the whole fleet, turning a partial outage into a total one.

Passive health (outlier detection)

  • The balancer watches real request outcomes per instance and temporarily ejects any instance whose error rate is far above its peers. It wins for failures the probes cannot see, such as one instance returning 500s only for real traffic.

Recommendation: local-state readiness plus passive outlier detection, with dependency health surfaced as a metric rather than as the probe result.

Failure modes

Deep check takes the fleet out

  • A 20-second database blip makes every instance report unhealthy and the balancer has nowhere left to send traffic. It happens because the readiness check calls the database, so all instances fail together. Probe only local state; let the application return errors for the database outage and let the circuit breaker handle it.

Probe interval too slow

  • A dead instance keeps receiving traffic for a minute. It happens because the probe runs every 30 s and needs 3 failures. Use a 5 s interval, a 2 s timeout and 2 or 3 failures, balanced against the extra probe load.

Liveness restarts a busy instance

  • Under heavy load, instances are restarted again and again. It happens because the liveness probe times out when every request thread is busy, and the orchestrator reads that as a hang. Serve probes from a dedicated thread, keep the liveness timeout generous, and never let liveness depend on load.

No draining on shutdown

  • Every deploy produces a burst of connection resets. It happens because the process exits while the balancer is still routing requests to it. Fail readiness first, wait for one probe interval plus a margin, then stop; configure a termination grace period that allows for it.

Readiness always true

  • New instances serve cold and slow for a minute after each deploy. It happens because the readiness endpoint returns 200 from the first millisecond. Gate readiness on warm-up completing, and measure the time to ready.

Probes count as traffic

  • Logs and metrics are flooded with probe requests, and rate limits trip on the probe path. It happens because the probe path is treated like any other request. Exclude it from logging, authentication and rate limits.

Interviewer follow-ups

Answer out loud before you open each one.

Why should readiness not check the database?

Because the database is shared. If it fails, all instances fail the check together and the balancer removes the entire fleet, which turns degraded responses into no responses at all. Report dependency health as a metric and let the request path degrade; use readiness only for what is specific to this instance.

What is the difference between liveness and readiness?

Liveness answers "should this process be restarted" and should fail only when the process is wedged. Readiness answers "should this process receive traffic right now" and can flip during warm-up, overload or shutdown. Restarting on readiness failures would restart healthy instances that are merely busy.

How do you choose the probe interval and thresholds?

Detection time is roughly interval times failure threshold, so 5 s and 3 failures is about 15 s. Shorter catches failures faster at the cost of probe load and false positives on transient blips. Two to three failures with a 5 s interval is a common balance; use passive outlier detection for sub-second reaction.

How do you do a zero-downtime deploy with these?

Start the new instance, wait for readiness to pass, add it to the balancer; then fail readiness on an old instance, wait for the balancer to drain it, stop it. Repeat one or a few at a time. The probes are what make the sequence safe; without them you are guessing at timing.

Connections

In study paths
Surviving a failed node
Pairs with
Spread traffic across servers · Survive losing a node · Deploy without downtime · Stop calling a failing dependency · Degrade instead of failing

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.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Application Load BalancerLoad balancerCloud Load BalancingApplication Gateway, Azure Load BalancerNGINX, HAProxy, Envoy
Amazon EKSOrchestratorGKEAKSKubernetes, k3s
Amazon EC2 Auto ScalingInstance 11, Instance 12Compute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon AuroraURL storeAlloyDB for PostgreSQL, Cloud SQLAzure Database for PostgreSQL or MySQLPostgreSQL, MySQL, CockroachDB