High Concurrency · Code review · Postmortem: how we exhausted the connection pool in high-concurrency-review

477
HCr/high-concurrency-review·posted by ran_bo·6 hours agoHelpPinned

Postmortem: how we exhausted the connection pool in high-concurrency-review

Most high-concurrency-review articles stop at "how to use it" and never cover "when not to use it". This is an attempt at the second half.

# Load test: do not jump straight to the max concurrency.
# Ramp it up, otherwise you miss the knee.
for c in 50 100 200 400 800; do
  wrk -t8 -c$c -d60s --latency http://127.0.0.1:8080/api/feed
  sleep 20
done

One last trap: in container environments remember to adjust the memory-related parameters in step. Otherwise the host limit and the process expectation disagree, and the symptom is intermittent, unreproducible failure.

What genuinely surprised me was the tail. The average looked great while P99 jumped by an order of magnitude past some threshold. The cause was not high-concurrency-review itself but our upstream connection reuse — the load test traffic was too clean and hid the long-tail requests.

On trade-offs, my view is this: if nobody on the team owns this area long-term, do not introduce a second mechanism. With two coexistence you first have to work out which one is even in play when things break, and that costs far more than the performance you saved.

120 comments

120 comments

M
Bbob_chen·2 hours agoedited

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

512
Mmike_xu·3 minutes ago

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

405
Bbob_chen·2 days agoedited

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

398
Kkernel_panic·28 minutes ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

388
Lli_ming·2 days ago

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

378
Oops_wang·1 hour ago

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

368
Sswoole_lee·2 days ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

197
Kkite·2 days ago

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

8
Lli_ming·2 days ago

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

3
Lli_ming·2 days ago

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

4
Wwinter·2 days ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

293
Sslow_queryOP·2 days ago

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

226
Ddev_zhou·2 days ago

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

272
Sslow_query·2 days ago

I just read the high-concurrency-review source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

271
Nnikic·2 days ago

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

271
Kkite·2 days agoedited

One counter-example: below high-concurrency-review 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

257
Rran_bo·2 days ago

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

224
Kkernel_panic·1 hour ago

Saved. I am reworking this area this week — this saves a lot of wrong turns.

222
Ddev_zhou·2 days ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

221
Kkernel_panic·12 minutes ago

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

180
Kkernel_panic·3 minutes ago

Saved. I am reworking this area this week — this saves a lot of wrong turns.

286
Zzhou_yi·2 days ago

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

464
Nnikic·2 days agoedited

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

101
Aalice_dev·2 days ago

I just read the high-concurrency-review source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

97
Hhuang_keMod·12 minutes agoedited

One counter-example: below high-concurrency-review 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

1
Aalice_dev·2 hours ago

Saved. I am reworking this area this week — this saves a lot of wrong turns.

19
Rran_bo·1 hour ago

This is not a high-concurrency-review problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

64
Sslow_query·2 days ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

126
NnikicOP·2 days ago

One counter-example: below high-concurrency-review 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

3
Kkernel_panic·just now

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

240
Nnikic·2 days ago

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

334
Sswoole_lee·2 days ago

I just read the high-concurrency-review source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

90
Zzhu_zong·2 days ago

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

79
Zzhu_zong·2 days ago

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

385
Hhuang_ke·2 days ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

242
Kkite·2 days ago

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

45
Zzhu_zong·2 days ago

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

35
Bbob_chen·2 days ago

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

26
Kkernel_panic·3 minutes ago

One counter-example: below high-concurrency-review 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

176
Rran_boOP·2 days ago

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

175
Mmike_xuOP·2 days ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

114
Wwinter·12 minutes ago

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

172
Ddev_zhou·2 days ago

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

166
Rran_boMod·1 hour ago

Saved. I am reworking this area this week — this saves a lot of wrong turns.

317
Oops_wang·12 minutes ago

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

160
Ttang_hao·2 days ago

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

139
Ttang_hao·2 days ago

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

109
Wwinter·3 minutes ago

One counter-example: below high-concurrency-review 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

107
Kkernel_panic·yesterday

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

150
Zzhu_zong·yesterdayedited

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

208
Ddev_zhouOP·2 days ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

20
Kkernel_panic·2 days ago

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

93
Sslow_query·2 days ago

One counter-example: below high-concurrency-review 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

1
KkiteOP·2 days ago

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

1
Mmike_xu·2 days ago

This is not a high-concurrency-review problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

31
Sslow_queryOP·2 days agoedited

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

44
Ddev_zhou·28 minutes ago

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

101
Cchen_dev·2 days ago

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

85
KkiteMod·2 days ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

284
Nnikic·2 days ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

82
Zzhu_zong·2 days ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

5
LlinlinOP·2 days ago

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

6
Kkernel_panic·2 days ago

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

353
Nnikic·2 days ago

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

110
Sslow_queryOP·2 days ago

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

224
Sslow_query·yesterdayedited

This is not a high-concurrency-review problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

152
Oops_wang·2 days ago

Saved. I am reworking this area this week — this saves a lot of wrong turns.

38
Kkite·2 days ago

Saved. I am reworking this area this week — this saves a lot of wrong turns.

324
Sswoole_lee·2 days ago

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

65
Kkernel_panic·2 days ago

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

1
Lli_mingOP·2 days ago

This is not a high-concurrency-review problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

3
Zzhou_yiOPMod·2 days agoedited

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

1
RraseOP·2 days ago

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

1
Cchen_dev·2 hours ago

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

53
Sswoole_lee·2 days ago

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

52
Lli_ming·2 days ago

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

373
Rran_bo·2 days ago

One counter-example: below high-concurrency-review 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

126
Ttang_hao·2 days ago

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

52
Cchen_dev·2 days ago

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

106
Sswoole_lee·3 minutes ago

This is not a high-concurrency-review problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

44
Kkite·2 days ago

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

43
Ddev_zhou·2 days ago

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

40
Bbob_chen·1 hour ago

Saved. I am reworking this area this week — this saves a lot of wrong turns.

39
Zzhu_zong·12 minutes ago

One counter-example: below high-concurrency-review 7.4 the semantics of that code are different, so do not copy it verbatim. We got burned in staging and rolled back once.

35
Mmike_xu·2 hours ago

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

32
Rrase·12 minutes agoedited

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

30
Oops_wang·5 hours agoedited

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

1
LlinlinOP·2 days ago

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

257
Ddev_zhou·2 days ago

I just read the high-concurrency-review source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

96
Sslow_query·2 days ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

146
Kkite·2 days agoedited

This is not a high-concurrency-review problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

32
Llinlin·2 days ago

I just read the high-concurrency-review source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

28
Ddev_zhou·2 days ago

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

22
Sswoole_lee·2 days ago

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

18
Hhuang_ke·2 days ago

I just read the high-concurrency-review source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

76
Lli_ming·2 days ago

This is not a high-concurrency-review problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

14
Bbob_chen·2 days ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

346
Rrase·2 days ago

This is not a high-concurrency-review problem, it is a usage problem. The docs say this API is not thread-safe and you must lock around it yourself.

17
Lli_ming·2 hours ago

Saved. I am reworking this area this week — this saves a lot of wrong turns.

13
Ttang_hao·2 days ago

Agreeing with the above. One addition: with this option enabled the GC count in your metrics doubles, so adjust the alert threshold at the same time or it will keep firing.

11
Rran_bo·2 hours agoedited

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

11
Bbob_chen·2 days ago

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

11
Mmike_xu·2 days ago

I see point 3 differently. The trade-off depends on your read/write ratio: read-heavy with little writing means caching actually widens the inconsistency window.

9
Sslow_query·2 days agoedited

I just read the high-concurrency-review source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

2
Wwinter·2 days ago

This matches what we see in production. We only hit it past 3k QPS; the earlier load tests showed nothing — the test traffic was too clean, with no long-tail requests.

2
Kkite·2 days ago

I just read the high-concurrency-review source — the author actually explains the reasoning in a comment, roughly "so that it degrades into predictable behaviour in extreme cases".

1
Sslow_query·2 days ago

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

77
Nnikic·2 days agoedited

We have run this in production for two years without hitting it. That said, we never reached this scale, so our experience is not really evidence here.

498
Ddev_zhou·2 days ago

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

46
NnikicOP·2 days ago

A question: what changes in a container with a 512Mi memory limit? That is how we run it in production.

172
Cchen_dev·2 days ago

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

195
Kkernel_panic·2 days ago

Can you give a minimal reproduction? I ran it locally for ten minutes and could not reproduce on macOS with the latest version.

27
Sslow_query·just now

There is actually a simpler fix that needs no architecture change: move this check up to the gateway and the problem disappears. The cost is one extra lookup at the gateway.

78
Ddev_zhou·2 days agoedited

Thanks for sharing real numbers — far more useful than the articles that only cover concepts.

50
Hhuang_ke·2 days agoeditedLevel 6

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

267
Aalice_dev·2 days ago

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

189
Bbob_chen·2 hours ago

Has anyone run a controlled experiment? I did, reducing it to a single variable, and the difference was 4% — within noise. So I suspect the main cause is something else.

181
Mmike_xu·2 days ago

Worth learning from this debugging approach. We went straight at the logs and took a much longer route.

74
Mmike_xu·2 days ago

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

275
Rran_bo·2 days agoedited

Sharing our numbers, 8 cores 16GB, same scenario:

| Concurrency | P50 | P99 |
|---|---|---|
| 200 | 12ms | 88ms |
| 500 | 31ms | 340ms |

P99 clearly collapses at 500 concurrency, which lines up with your knee point.

21

This is the post detail page /en/c/high-concurrency-review/post/p0. Posts and comments are generated deterministically from a seeded PRNG, so the same post always renders the same content and the link can be shared, reloaded and indexed. In production this page reads MySQL for the post, Redis for hot-post caching, and fetches the whole comment tree in a single query on the path column.

See the database schema →