Open Source · Blog · Open-sourced a rate-limiting middleware for open-source-blog with token bucket and sliding window

959
OSr/open-source-blog·posted by nikic·yesterdayTutorial

Open-sourced a rate-limiting middleware for open-source-blog with token bucket and sliding window

Some background first. Our setup is open-source-blog plus three downstream services, seven figures of daily requests, peaking around nine in the evening.

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

-- The query that broke: a full scan over 20M rows.
-- A composite index took P99 from 1.8s down to 42ms.
SELECT id, title, created_at
  FROM posts
 WHERE community_id = ?
   AND status = 1
 ORDER BY score DESC
 LIMIT 20;

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.

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.

422 comments

422 comments

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

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

494
Rran_bo·28 minutes ago

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

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

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

356
Kkernel_panic·2 days ago

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

4
Kkite·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
Lli_ming·2 days agoedited

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

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

17
Aalice_dev·2 days ago

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

149
Rran_bo·2 days ago

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

25
Sswoole_lee·just now

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

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

213
Sswoole_lee·2 days ago

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

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

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

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

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

427
KkiteOP·2 days ago

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

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

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

211
Ddev_zhouOP·3 minutes ago

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

49
Ttang_haoOP·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.

57
Bbob_chen·2 days agoedited

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.

73
Zzhu_zong·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 agoedited

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

70
Ddev_zhouOP·2 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.

321
Rrase·2 days ago

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

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

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

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

385
Nnikic·just now

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.

376
Sswoole_lee·12 minutes ago

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

339
Rrase·just now

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

188
Rrase·1 hour ago

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

346
Kkernel_panic·just now

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.

182
Rrase·2 days ago

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

452
Llinlin·5 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.

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

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

10
Hhuang_ke·yesterdayedited

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.

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

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

433
Zzhu_zongMod·28 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.

369
Zzhou_yi·just now

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

19
Zzhou_yi·2 days agoedited

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

391
Cchen_dev·2 days agoeditedLevel 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.

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

71
Aalice_dev·2 days ago

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

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

33
Zzhou_yi·2 days ago

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

12
Ddev_zhou·2 days ago

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

450
Kkite·1 hour agoedited

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

31
Sslow_query·12 minutes ago

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

36
Llinlin·2 days ago

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

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

2
Llinlin·2 days ago

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

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

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

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

235
Rrase·just now

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

227
Ttang_hao·1 hour ago

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

20
Aalice_dev·2 hours 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.

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

196
Rran_bo·2 days ago

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

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

352
Llinlin·2 days agoLevel 6

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

235
Nnikic·1 hour agoeditedLevel 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.

24
Kkite·2 hours ago

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

93
Nnikic·2 hours 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.

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

122
Ttang_hao·2 days agoLevel 6

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

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

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

160
Zzhu_zong·2 days ago

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

144
LlinlinMod·2 days agoLevel 6

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

108
Oops_wang·28 minutes agoLevel 6

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

1
Kkernel_panic·2 days ago

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

1
Nnikic·just now

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

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

137
Ttang_haoOP·2 days ago

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

72
Ddev_zhou·2 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
Nnikic·2 days ago

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

128
Rran_bo·2 days ago

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

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

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

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

83
Cchen_dev·2 days agoedited

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

80
Cchen_devOP·28 minutes ago

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

9
Llinlin·2 days agoedited

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

67
Sswoole_leeOP·2 days ago

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

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

51
Bbob_chen·2 days ago

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

48
Zzhou_yiMod·2 days ago

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

32
Rran_bo·28 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.

25
Aalice_dev·2 days ago

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

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

19
Aalice_dev·2 days ago

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

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

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

12
Sswoole_lee·2 days ago

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

156
Wwinter·2 days ago

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

422
Aalice_dev·1 hour ago

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

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

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

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

130
Ddev_zhou·2 days ago

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

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

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

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

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

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

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

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

5
Wwinter·2 days ago

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

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

312
Sslow_query·28 minutes ago

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

4
Sslow_query·2 days ago

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

2
Sslow_queryMod·2 days ago

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

1
Ttang_hao·2 days ago

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

1

This is the post detail page /en/c/open-source-blog/post/p10. 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 →