Keyboard Community · Open-sourced a rate-limiting middleware for keyboard with token bucket and sliding window

623
KEr/keyboard·posted by slow_query·6 hours agoTooling

Open-sourced a rate-limiting middleware for keyboard with token bucket and sliding window

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.

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.

Self-host — full control44%
Managed service — less work28%
Hybrid: core self-hosted17%
Not decided yet11%

1059 votes total

321 comments

321 comments

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

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

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

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

384
Aalice_dev·2 days agoedited

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

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

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

364
Sswoole_lee·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".

344
Ddev_zhou·12 minutes ago

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

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

303
Cchen_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.

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

227
Rran_bo·2 days ago

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

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

211
Wwinter·2 days ago

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

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

138
KkiteOP·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.

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

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

188
Sswoole_leeOP·2 days agoedited

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

486
Wwinter·28 minutes 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.

8
Wwinter·2 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.

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

502
Lli_ming·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.

377
Llinlin·3 minutes 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.

11
Zzhu_zong·just nowedited

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

343
Oops_wang·2 days agoedited

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

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

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

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.

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

104
KkiteOP·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.

28
Bbob_chenOP·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.

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

175
Rran_bo·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".

215
Sslow_query·2 days ago

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

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

159
KkiteOP·2 days agoedited

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

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

137
Oops_wangOP·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.

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

109
Rrase·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
Ttang_hao·2 days ago

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

94
Hhuang_ke·2 days agoedited

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

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

229
Aalice_devMod·2 days ago

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

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

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

58
Kkernel_panic·2 days ago

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

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

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

44
Wwinter·12 minutes 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.

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

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

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

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

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

54
Lli_ming·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.

21
Ttang_hao·12 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.

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

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

234
Sswoole_lee·2 days ago

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

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

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

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

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

208
Aalice_dev·2 days ago

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

30
Rran_bo·2 days ago

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

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

26
Oops_wang·2 days ago

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

24
Hhuang_ke·3 minutes ago

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

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

16
Oops_wang·2 days ago

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

16
Zzhou_yi·5 hours 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.

14
Kkernel_panicOP·2 hours ago

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

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

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

302
Mmike_xu·2 days ago

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

56
Zzhou_yi·2 days agoedited

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

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

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

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

11
Wwinter·2 days ago

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

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

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

1
Sslow_query·12 minutes ago

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

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

223
NnikicOPMod·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.

3
Bbob_chen·12 minutes ago

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

497
Kkernel_panic·28 minutes ago

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

201
Aalice_dev·2 days ago

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

499
Mmike_xu·2 days agoLevel 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.

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

18
Llinlin·2 days ago

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

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

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

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

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

2
Bbob_chen·12 minutes 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".

1
Ttang_hao·yesterday

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

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

112
Mmike_xu·5 hours 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.

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

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

179
Hhuang_ke·2 days agoedited

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

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

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

14
Ddev_zhou·3 minutes ago

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

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

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

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

341
Wwinter·2 days ago

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

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

69
Wwinter·12 minutes ago

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

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

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

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

1
Kkernel_panic·2 days ago

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

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

1

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