Tanstack · Ops · Open-sourced a rate-limiting middleware for tanstack-ops with token bucket and sliding window

1.4K
TAr/tanstack-ops·posted by zhu_zong·6 hours agoPostmortem

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

Short version: tanstack-ops 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.

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.

Jjvns.caExternal link · opens in a new tab
266 comments

266 comments

· first 120 loaded
M
Rrase·2 days ago

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

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

496
Sslow_queryOP·yesterday

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

412
Oops_wang·just now

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

2
Lli_ming·2 days ago

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

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

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

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

356
Zzhu_zong·2 days ago

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

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

401
Bbob_chenMod·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.

362
RraseOP·2 days ago

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

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

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

293
Oops_wang·2 days ago

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

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

241
Zzhou_yi·2 days ago

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

239
Llinlin·5 hours agoedited

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

233
Kkernel_panic·1 hour ago

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

207
Rrase·2 days ago

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

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

178
Ttang_hao·2 days ago

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

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

9
Cchen_dev·2 days ago

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

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

134
Llinlin·2 days ago

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

149
Rrase·just now

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.

16
Cchen_dev·2 days ago

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

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

492
Rran_bo·2 days ago

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

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

113
Oops_wang·2 days ago

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

108
Bbob_chenOP·2 days agoedited

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

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

350
Hhuang_ke·2 days ago

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

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

134
Zzhou_yi·12 minutes ago

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

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

91
WwinterMod·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.

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

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

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

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

66
Sswoole_lee·2 days ago

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

171
Zzhou_yiOP·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.

30
Kkite·2 days ago

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

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

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

35
Zzhu_zong·2 days ago

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

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

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

45
Wwinter·2 days agoLevel 6

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.

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

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

211
Llinlin·2 days agoedited

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

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

1
Oops_wangOP·just now

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

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

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

6
Mmike_xu·2 days agoedited

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

66
Nnikic·2 days ago

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

446
Mmike_xuOP·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
Zzhou_yi·2 days ago

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

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

57
Ttang_hao·1 hour ago

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

57
Sslow_query·yesterday

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

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

1
Lli_ming·2 days ago

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

54
Cchen_dev·3 minutes ago

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

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

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

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

504
Hhuang_ke·2 days ago

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

8
Wwinter·2 days ago

One counter-example: below tanstack-ops 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
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.

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

23
Sslow_query·2 days agoLevel 6

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

152
Sslow_query·2 days agoLevel 6

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

12
Zzhu_zong·5 hours ago

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

17
Zzhou_yi·yesterday

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

242
Rrase·2 days ago

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

74
Sslow_query·2 days ago

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

45
Sslow_query·2 days ago

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

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

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

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

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

21
Mmike_xu·yesterday

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

20
Bbob_chenMod·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.

19
Zzhu_zongOP·2 days ago

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

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

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

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

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

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

5
Rrase·2 hours ago

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

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

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

3
Rran_bo·3 minutes ago

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

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

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

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

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

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

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

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

365
Sslow_query·2 days agoeditedLevel 6

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.

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

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

483
Ttang_hao·2 days agoLevel 6

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

74
Ddev_zhou·2 days ago

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

74
Wwinter·2 days ago

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

3
Cchen_dev·2 days ago

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

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

64
Rran_bo·3 minutes agoedited

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

2
Rran_bo·2 days ago

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

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

1
Zzhou_yi·2 days ago

One counter-example: below tanstack-ops 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
Wwinter·2 days ago

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

1
Zzhu_zong·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/tanstack-ops/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 →