Keyboard Community · The keyboard execution flow in one diagram (with sequence chart)

1.1K
KEr/keyboard·posted by dev_zhou·3 hours agoInternals

The keyboard execution flow in one diagram (with sequence chart)

It took me two weeks of on-and-off digging and plenty of wrong turns. Writing the process down as it happened so the next person spends less time.

Worth noting: the official docs do cover this, just in a very inconspicuous spot. I only found it reading the source comments, where the author explains the reasoning — roughly "so that it degrades into predictable behaviour in extreme cases".

// 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)
}

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.

The first thing was to collapse the variables. We were changing config and upgrading the version at the same time, and afterwards nobody could say which change caused what. We rolled back to moving one variable at a time, re-ran three times, and only then did the curve settle. Tedious, but not skippable.

608 comments

608 comments

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

514
Nnikic·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.

481
Oops_wang·2 days 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.

487
WwinterOPMod·2 days ago

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

419
Mmike_xu·2 days ago

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

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

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

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

413
Bbob_chen·yesterday

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.

394
Cchen_dev·2 days ago

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

384
Nnikic·2 days ago

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

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

327
Kkite·2 days ago

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

319
Cchen_dev·just now

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

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

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

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

34
Aalice_devOP·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".

87
Llinlin·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".

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

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

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

6
LlinlinMod·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.

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

208
Lli_ming·2 days agoeditedLevel 6

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.

110
Bbob_chen·2 days agoeditedLevel 6

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

13
Nnikic·2 days 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.

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

261
Lli_ming·2 days agoedited

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

90
Hhuang_ke·yesterday

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

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

162
Cchen_devOP·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".

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

202
Rran_boOP·just now

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

91
Wwinter·2 days ago

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

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

189
Bbob_chen·yesterday

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.

16
Bbob_chen·28 minutes ago

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

185
Cchen_devMod·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.

180
Rran_bo·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.

177
Rran_bo·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.

207
Hhuang_ke·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".

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

116
Hhuang_ke·2 days agoedited

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

452
Mmike_xu·2 days agoLevel 6

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

475
Lli_ming·2 days agoLevel 6

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.

97
Oops_wangOP·yesterday

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

278
Kkite·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.

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

18
Mmike_xu·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.

146
Sswoole_lee·28 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.

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

506
Nnikic·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.

271
Ttang_hao·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
Kkite·5 hours 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.

10
Llinlin·2 days agoLevel 6

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

202
Ddev_zhou·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
Ttang_hao·2 days agoedited

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.

76
Rran_bo·3 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.

173
Rrase·12 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.

161
Aalice_dev·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.

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

226
Oops_wangOP·2 hours 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".

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

50
Ddev_zhouOP·5 hours ago

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

14
Zzhu_zongOP·2 days ago

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

29
Nnikic·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.

264
Zzhou_yiOP·5 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.

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

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

12
Nnikic·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
Ttang_hao·just nowedited

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

162
Sslow_query·12 minutes 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.

153
Sswoole_leeMod·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.

132
Llinlin·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".

4
Sslow_query·12 minutes ago

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

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

100
Hhuang_ke·2 days ago

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

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

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

35
Kkite·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.

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

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

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

86
Hhuang_ke·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".

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

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

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

73
Sswoole_lee·yesterday

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.

67
WwinterOP·5 hours 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.

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

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

270
Wwinter·2 hours ago

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

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

64
Kkite·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·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
Nnikic·2 days ago

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

15
Sswoole_leeOP·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
Zzhou_yi·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.

59
Nnikic·2 days ago

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

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

26
Oops_wang·28 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.

334
Bbob_chen·yesterday

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.

97
Sswoole_lee·12 minutes ago

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

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

318
Kkernel_panic·2 days agoedited

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

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

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

1
Kkernel_panicOP·2 days ago

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

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

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

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

13
Rran_bo·2 days ago

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

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

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

12
Aalice_dev·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.

4
Kkite·2 days ago

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

9
Bbob_chenMod·12 minutes ago

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

1
Nnikic·2 days ago

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

470
Mmike_xu·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.

1

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