CLI Tools · Career · Open-sourced a rate-limiting middleware for cli-tools-career with token bucket and sliding window

1.1K
CTr/cli-tools-career·posted by winter·3 days agoPostmortem

Open-sourced a rate-limiting middleware for cli-tools-career with token bucket and sliding window

Short version: cli-tools-career needs almost no tuning at small and medium scale — the point where it starts to hurt is much further out than most people assume. Full measurements below.

Order of investigation, by return on effort: 1. Check downstream latency first — usually it is not your problem 2. Then pool hit rate and wait-queue length 3. Only then GC and allocation 4. Suspect the framework last

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.

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.

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

325 comments

325 comments

· first 120 loaded
M
Ddev_zhou·2 days ago

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

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

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

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

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

466
Sswoole_lee·2 days ago

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

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

428
Hhuang_ke·2 days ago

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

415
Lli_ming·1 hour ago

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

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

373
Kkite·2 hours 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.

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

247
Hhuang_ke·2 days ago

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

316
Mmike_xu·2 days ago

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

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

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

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

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

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

307
Aalice_dev·2 days ago

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

167
Cchen_dev·2 days ago

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

135
KkiteOP·2 days ago

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

2
Mmike_xu·28 minutes ago

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

50
Kkernel_panic·5 hours ago

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

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

438
Zzhou_yi·2 days ago

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

265
Mmike_xu·yesterday

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

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

98
Ttang_hao·2 days ago

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

133
KkiteMod·2 days ago

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

512
Kkernel_panic·just now

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

187
Nnikic·2 days ago

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

447
Ddev_zhou·2 days ago

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

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

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

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

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

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

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

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

123
Lli_ming·2 days ago

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

197
Oops_wangOP·2 days ago

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

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

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

81
Llinlin·12 minutes agoedited

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

75
Llinlin·yesterdayedited

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.

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

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

119
Wwinter·just nowedited

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

1
Ddev_zhou·2 days ago

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

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

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

35
Sswoole_lee·2 days ago

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

77
Wwinter·2 days ago

One counter-example: below cli-tools-career 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
Bbob_chen·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.

62
Aalice_dev·2 days ago

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

85
Ttang_hao·2 days agoedited

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

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

67
Zzhu_zongOP·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
Oops_wang·2 days ago

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

57
Mmike_xuMod·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.

49
Hhuang_ke·28 minutes agoedited

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

334
Sswoole_lee·3 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.

42
Oops_wang·1 hour ago

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

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

293
Rrase·2 days ago

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

235
Llinlin·2 days ago

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

112
Kkite·2 days ago

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

477
NnikicMod·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.

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

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

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

123
Lli_mingOP·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.

356
Aalice_dev·2 days agoedited

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

454
Oops_wang·just now

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

367
Oops_wang·2 days ago

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

505
Lli_mingOP·28 minutes ago

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

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

320
Ttang_hao·2 days ago

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

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

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

55
Zzhu_zong·2 days ago

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

344
Wwinter·2 days ago

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

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

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

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

40
Hhuang_ke·28 minutes ago

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

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

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

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

25
Zzhu_zong·28 minutes ago

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

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

480
Cchen_dev·5 hours ago

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

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

283
Lli_ming·2 days ago

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

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

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

459
Bbob_chenOP·2 days ago

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

115
RraseOP·3 minutes ago

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

312
Llinlin·2 hours agoedited

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

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

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

18
Sswoole_leeMod·2 days ago

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

412
Llinlin·2 days agoedited

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

1
Lli_ming·2 days ago

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

31
Llinlin·3 minutes ago

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

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

30
Llinlin·2 days ago

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

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

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

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

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

16
Cchen_dev·2 days ago

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

12
Ttang_hao·just now

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

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

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

5
Cchen_dev·2 days ago

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

2
Nnikic·2 days ago

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

1
Oops_wang·2 days ago

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

1

This is the post detail page /en/c/cli-tools-career/post/p2. 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 →