Skip to content

Cold cache: health check throws all instances out of the load balancer

System Designtested on 3 VMs Ubuntu 24.04, Go, PostgreSQL, Valkey, HAProxy, k6

The cache is flushed, for example by a restart or a big deploy. All requests hit the database at once. Worse than the load is the health check: it considers healthy instances dead.

Symptom

21:15:48 s1,UP s2,UP  cache=0
21:15:50 s1,UP s2,UP 2/3  cache=51
21:15:55 s1,UP 1/3 s2,UP 1/3  cache=181
21:15:57 s1,UP 1/3 s2,DOWN  cache=246
21:15:59 s1,DOWN s2,DOWN  cache=304
21:16:07 s1,DOWN s2,DOWN  cache=476
 http_req_failed................: 38.49% 6276 out of 16305

The cache fills at about 30 keys per second – the database cannot do more at 84 ms per query. After eleven seconds HAProxy takes both instances out, even for links that are already back in the cache.

Diagnosis

Look at the readiness check. It asks PostgreSQL with a one-second limit; under load the ping waits for a free connection.

sed -n 92,100p main.go
func bereit(w http.ResponseWriter, r *http.Request) {
	ctx, cancel := context.WithTimeout(r.Context(), time.Second)
	defer cancel()
	if !cacheBereit(ctx) && db.Ping(ctx) != nil {
		http.Error(w, "Datenbank nicht erreichbar", http.StatusServiceUnavailable)
		return
	}
	w.Write([]byte("bereit\n"))
}

Causes

s1,DOWN s2,DOWNHealth check as an accelerant

/readyz only checks the database. Under load the ping arrives too late, and HAProxy considers the instance dead although it could answer every cached link at once.

singleflight aloneMerging misses barely helps

singleflight merges concurrent misses for the same key. With many different keys, the error rate only dropped from 38.5 to 37.8 %.

shared contextChecks in the wrong order

Checking the database first and then the cache with the same ctx lets the database ping use up the whole second – the cache check fails right away too. In the course 34 % errors remained this way.

Fix

The instance is ready when the cache or the database answers – the fast check first (code above).
21:22:15 s1,UP s2,UP  cache=0
21:22:23 s1,UP s2,UP  cache=310
21:22:33 s1,UP s2,UP  cache=647
 http_req_duration..............: avg=980.42ms min=865.1µs med=2.54ms max=9.43s p(90)=5.53s p(95)=7.5s
 http_req_failed................: 0.00% 0 out of 14084

Don't: Call the cache “optional” and tie the health check to the database alone. The goal during a cold start is slow instead of broken – both instances stay in the load balancer.

▶
Break it yourself and find it – free
System design hands-on, Day 6: when the cache causes trouble – cold start, stampede and a health check as an accelerant.
Go to day 6 →

FAQ

What is a cache stampede?

Many requests miss the same key at the same time and all ask the database. singleflight lets only one request ask; the others wait for its result.

Why is p95 still high after the fix?

The misses still wait for the database. The median stays at 2.5 ms from the cache – the system is slow instead of broken during the cold start.

Related error patterns

More