SWC · Interviews · Open-sourced a rate-limiting middleware for swc-interview with token bucket and sliding window

2.1K
SWr/swc-interview·posted by zhu_zong·just nowJobs

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

Short version: swc-interview 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.

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.

Documentation first44%
Source code first28%
Just ask someone17%
Run a demo and learn by error11%

3575 votes total

319 comments

319 comments

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

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

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

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

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

481
Bbob_chen·2 hours 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.

282
Zzhou_yi·2 days ago

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

423
Llinlin·just now

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

408
Wwinter·3 minutes ago

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

370
Kkernel_panic·2 days ago

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

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

65
Wwinter·2 days ago

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

17
Mmike_xu·2 days agoedited

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

108
Wwinter·2 days agoLevel 6

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

79
Kkite·28 minutes 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.

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

70
Hhuang_ke·2 days ago

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

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

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

55
WwinterMod·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
LlinlinOP·12 minutes ago

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

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

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

1
Zzhu_zong·12 minutes ago

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

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

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

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

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

422
WwinterOP·2 days ago

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

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

299
Lli_ming·2 days ago

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

257
Rran_bo·28 minutes ago

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

243
Rran_bo·2 days ago

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

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

227
Mmike_xu·2 days ago

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

62
Cchen_dev·3 minutes ago

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

232
Aalice_dev·2 hours ago

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

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

382
Ddev_zhou·2 days ago

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

1
Cchen_dev·28 minutes ago

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

21
Rran_bo·2 days ago

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

439
Ddev_zhou·1 hour ago

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

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

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

324
Zzhu_zong·2 days ago

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

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

504
Rran_boMod·2 days ago

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

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

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

227
Zzhu_zong·2 days ago

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

221
Kkite·2 days ago

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

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

181
Rran_bo·28 minutes ago

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

177
Aalice_dev·just nowedited

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

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

42
Cchen_dev·12 minutes ago

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

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

153
Nnikic·5 hours ago

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

506
Bbob_chen·2 days agoedited

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

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

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

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

135
Mmike_xu·28 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.

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

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

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

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

72
Ddev_zhou·3 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.

6
Cchen_dev·2 days ago

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

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

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

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

126
Rrase·2 days ago

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

115
Rran_bo·2 hours ago

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

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

141
Rran_boOP·2 days ago

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

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

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

176
Aalice_dev·2 days ago

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

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

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

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

54
Ddev_zhou·2 days ago

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

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

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

1
Nnikic·just now

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

43
Mmike_xu·2 days ago

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

461
Rrase·yesterday

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.

27
Aalice_dev·2 days ago

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

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

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

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

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

14
Lli_ming·2 days agoLevel 6

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

398
Sslow_query·2 days agoLevel 6

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

7
Kkite·1 hour ago

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

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

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

3
Ddev_zhouMod·3 minutes ago

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

469
Rrase·5 hours ago

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

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

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

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

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

27
Kkite·2 days ago

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

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

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

13
Wwinter·2 days ago

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

12
Lli_ming·3 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.

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

6
Ddev_zhou·2 days ago

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

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

47
Zzhou_yi·2 days agoedited

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

4
Ddev_zhou·2 days ago

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

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.

43
Lli_ming·2 days ago

One counter-example: below swc-interview 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
Sswoole_lee·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.

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

11
Bbob_chen·2 days agoedited

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

2
Ttang_hao·2 days ago

One counter-example: below swc-interview 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

This is the post detail page /en/c/swc-interview/post/p11. 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 →