Rust Community · Ten Rust anti-patterns — how many are you guilty of?

575
Rsr/rust·posted by tang_hao·2 days agoInternals

Ten Rust anti-patterns — how many are you guilty of?

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

What genuinely surprised me was the tail. The average looked great while P99 jumped by an order of magnitude past some threshold. The cause was not Rust itself but our upstream connection reuse — the load test traffic was too clean and hid the long-tail requests.

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.

Ddeveloper.mozilla.orgExternal link · opens in a new tab
232 comments

232 comments

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

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

488
Llinlin·2 days ago

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

484
Rrase·2 days ago

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

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

4
Kkernel_panic·2 days ago

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

420
Zzhu_zong·2 days ago

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

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

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

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

319
Hhuang_ke·3 minutes ago

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

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

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

226
Oops_wang·3 minutes ago

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

14
Kkernel_panic·2 days ago

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

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

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

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

326
Aalice_devMod·2 days 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.

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

81
Aalice_dev·yesterdayedited

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

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

92
Hhuang_ke·12 minutes ago

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

278
Aalice_dev·28 minutes ago

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

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

391
Rran_bo·2 days ago

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

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

274
Ttang_hao·2 days agoedited

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

257
Zzhu_zong·2 days ago

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

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

253
Ddev_zhou·12 minutes ago

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

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

39
Mmike_xu·2 days ago

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

9
Nnikic·3 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.

229
Rran_bo·2 days agoedited

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

228
Sslow_query·2 days ago

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

169
Rran_bo·2 days ago

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

161
Oops_wangOP·2 days ago

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

95
Ttang_hao·1 hour ago

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

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

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

3
Sswoole_lee·2 days ago

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

186
Sswoole_lee·2 days ago

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

423
Oops_wang·12 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.

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

170
Lli_ming·2 days ago

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

132
Wwinter·2 days ago

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

124
Zzhou_yi·2 days ago

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

118
Wwinter·just now

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

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

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

93
Ddev_zhou·2 days ago

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

82
Rrase·2 days ago

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

75
Rrase·just now

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

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

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

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

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

63
Sslow_queryOP·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
Lli_ming·2 days ago

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

60
Ddev_zhouMod·2 days ago

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

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

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

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

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

53
Aalice_dev·yesterday

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

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

31
Rrase·2 days ago

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

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

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

26
Kkernel_panic·2 days ago

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

21
Sslow_query·2 days ago

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

21
Oops_wang·2 hours ago

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

19
Cchen_dev·2 days ago

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

13
Rrase·2 days ago

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

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

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

8
Aalice_dev·2 days ago

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

360
Cchen_dev·2 days ago

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

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

5
Wwinter·2 days ago

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

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

5
RraseMod·1 hour ago

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

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

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

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

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

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

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

4
Llinlin·2 days ago

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

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

2
Cchen_dev·28 minutes agoedited

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

1
Nnikic·28 minutes 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.

158
Bbob_chen·12 minutes ago

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

192
Kkite·1 hour ago

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

484
Oops_wang·5 hours 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.

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

18
Aalice_dev·2 days 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.

503
Wwinter·2 days agoLevel 6

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

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

166
Sslow_queryOP·2 days agoLevel 6

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
Cchen_dev·2 hours agoLevel 6

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

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

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

33
Cchen_dev·2 days ago

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

408
Oops_wang·2 days agoLevel 6

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

163
Oops_wang·2 days agoeditedLevel 6

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

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

29
Wwinter·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
Bbob_chen·5 hours ago

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

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

365
Oops_wang·2 hours ago

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

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

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

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

1
Hhuang_ke·2 days ago

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

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

1

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