Keyboard Community · Why this keyboard pattern gets slow in production: a source-level explanation

479
KEr/keyboard·posted by li_ming·3 hours agoAnnouncement

Why this keyboard pattern gets slow in production: a source-level explanation

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

// Minimal reproduction: you must use a real long-tail distribution here.
// Uniform load-test traffic will never trigger this.
func (s *Server) handle(ctx context.Context) error {
    conn, err := s.pool.Acquire(ctx)
    if err != nil {
        return fmt.Errorf("acquire: %w", err)
    }
    defer conn.Release()

    return s.do(ctx, conn)
}

We also fixed monitoring along the way: replaced average-based alerts with percentiles and split them per endpoint. False alerts dropped by about seventy percent and the on-call rotation visibly cheered up.

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.

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.

279 comments

279 comments

· first 120 loaded
M
Ttang_haoOP·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.

455
Kkite·2 hours ago

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

514
Mmike_xu·28 minutes ago

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

96
Sswoole_lee·yesterday

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

7
Oops_wangOP·1 hour 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.

429
Cchen_dev·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.

48
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.

366
Zzhu_zong·3 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.

17
Nnikic·2 days agoedited

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

499
Aalice_dev·2 days agoedited

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.

407
Aalice_dev·2 days agoedited

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.

292
Sslow_query·2 days ago

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

169
Llinlin·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.

37
Kkite·2 days ago

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

362
Sswoole_lee·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.

10
Hhuang_keOP·2 days ago

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

6
Mmike_xuOP·2 days ago

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

3
Llinlin·2 days ago

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

400
Oops_wang·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.

459
Nnikic·just now

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.

387
Ttang_hao·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.

3
Bbob_chen·2 hours ago

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

311
Cchen_dev·2 days ago

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

304
Zzhu_zong·2 days ago

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

211
Cchen_dev·1 hour 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.

165
Bbob_chen·just now

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

470
Sslow_query·12 minutes ago

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

23
RraseOP·2 days agoLevel 6

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.

410
Sswoole_leeOP·2 days agoLevel 6

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

357
Rran_bo·2 days agoLevel 6

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

247
Lli_ming·2 days agoLevel 6

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

160
NnikicMod·12 minutes agoedited

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.

370
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.

85
Ttang_hao·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.

18
Zzhu_zong·2 days agoLevel 6

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

6
Oops_wang·5 hours agoedited

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

275
Kkernel_panic·3 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.

113
Ddev_zhou·1 hour ago

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

65
Zzhou_yi·2 days agoLevel 6

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
Cchen_dev·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.

46
RraseOPMod·2 days agoLevel 6

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.

487
Bbob_chen·2 days agoLevel 6

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

11
Hhuang_ke·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.

14
Kkernel_panic·1 hour agoedited

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

92
Lli_ming·just nowedited

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

14
Nnikic·just now

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

9
Cchen_dev·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.

187
Oops_wangOP·2 days agoLevel 6

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

397
Lli_ming·2 days agoLevel 6

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

274
Mmike_xu·2 days agoLevel 6

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

250
Llinlin·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.

7
Wwinter·3 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.

383
Rrase·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.

378
Sswoole_lee·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.

366
Kkite·2 days ago

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

353
Hhuang_ke·2 days ago

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

332
Wwinter·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.

68
Aalice_dev·2 days ago

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

308
Llinlin·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.

298
Ttang_hao·2 days ago

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

1
Bbob_chen·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.

4
Sswoole_lee·2 days ago

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

275
Aalice_dev·2 days ago

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

269
Sswoole_leeOP·2 days ago

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

172
Sslow_query·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.

45
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.

212
WwinterOP·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.

150
Kkite·2 days ago

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

178
Kkernel_panicOP·2 days agoedited

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.

96
Zzhou_yiOP·2 days ago

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

91
Cchen_devOP·5 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.

81
Rran_bo·2 days ago

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

128
Ttang_hao·2 days ago

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

125
Wwinter·2 days ago

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

120
Oops_wang·2 days ago

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

1
Zzhu_zong·2 days agoedited

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

19
Sslow_query·1 hour 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.

111
Mmike_xu·2 days ago

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

105
Rran_bo·12 minutes 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.

100
Aalice_dev·28 minutes ago

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

87
Rran_boOP·2 days agoedited

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

27
Bbob_chen·2 days ago

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

77
Zzhou_yi·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.

490
Rran_boOP·2 days ago

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

7
NnikicOP·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.

1
Ttang_hao·2 days ago

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

14
Hhuang_keOP·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.

370
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.

214
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.

255
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.

346
Ddev_zhou·just now

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

1
Ttang_haoOP·28 minutes agoedited

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
Cchen_dev·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.

121
Ddev_zhou·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.

53
Ddev_zhou·2 days ago

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

442
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.

53
Mmike_xu·2 days ago

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

51
Llinlin·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.

40
Zzhu_zong·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.

32
Wwinter·2 days ago

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

410
Oops_wang·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.

510
Ttang_hao·2 days ago

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

48
WwinterMod·yesterday

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

43
Llinlin·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.

283
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.

22
Ddev_zhou·2 days ago

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

11
Wwinter·28 minutes agoedited

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.

8
Rran_boOP·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.

468
Kkernel_panic·2 days agoedited

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.

8
Mmike_xu·1 hour 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.

227
Nnikic·2 days agoedited

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.

18
Kkite·5 hours agoedited

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

5
Kkernel_panic·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.

5
Sswoole_lee·2 days ago

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

3
Zzhou_yi·28 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.

2
Ddev_zhou·2 days agoedited

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
Sslow_query·just nowedited

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

275
Sswoole_lee·5 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.

36
Lli_ming·2 days ago

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

1
Ddev_zhou·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.

1

This is the post detail page /en/c/keyboard/post/p6. 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 →