# Why Rewrite in Rust? What Anthropic, Cloudflare and Bun Actually Saved

Why teams rewrite in Rust and what it really saves: Anthropic's 70% CPU cut, Cloudflare's Pingora, Grab, Bun and Discord side by side, where the savings come from, and when a rewrite isn't worth it.

By Waseem Anjum · Published 2026-11-27 · https://fabwaseem.com/blog/why-rewrite-in-rust

> **TL;DR:** Teams rewrite in Rust to cut CPU and memory at scale, remove garbage-collection pauses and eliminate memory-safety bugs. Anthropic reports about 70% less CPU and more than 80% less memory per request after moving its Messages API from Python to Rust, and Cloudflare about 70% less CPU and 67% less memory for Pingora. Latency improves far less (Anthropic: about 20% on time to first token), partial rewrites can save little, and verifying behaviour is now the hard part. For most Node.js services, move one hot path to a Rust addon first.

Why rewrite in Rust? Teams do it because Rust runs native code with no garbage collector, so a service needs less CPU and memory to serve the same traffic, and the compiler rules out whole classes of memory bugs. The savings are real: Anthropic reports about 70% less CPU and more than 80% less memory per request after moving its Messages API from Python to Rust. But the numbers from every public rewrite also show where the gains stop: latency improves far less than CPU, partial rewrites can save almost nothing, and proving the new code behaves like the old one is now the hard part.

This guide puts the published results side by side, explains where the savings actually come from, and gives you a way to decide whether your Node.js or Python service should follow.

## Why rewrite in Rust? The four reasons teams give

Most rewrites start with a cost or stability problem, not a love of the language. Rust compiles to native code, frees memory deterministically instead of with a garbage collector, and catches use-after-free and data races at compile time. At high traffic, that turns into fewer servers, steadier tail latency and fewer crashes.

The reasons in the public write-ups fall into four groups:

- **CPU and memory cost at scale.** Anthropic's API traffic grew roughly 1,800 times between early 2024 and fall 2026, and the team wrote that Rust "would allow us to use far less CPU per request." Grab cites the same driver; Cloudflare's was mainly the architecture limits of its NGINX setup.
- **Garbage collection pauses.** Discord's Read States service, written in Go, showed latency and CPU spikes roughly every two minutes, because Go forces a collection at least that often and each one scanned a cache holding millions of entries. Tuning Go did not remove them.
- **Memory-safety bugs.** Bun moved from Zig to Rust because a large share of the bugs on its fix list were use-after-free, double-free and missed frees on error paths. Rust's ownership rules and `Drop` cleanup turn those into compile errors.
- **Predictable deployment.** npm chose Rust for its authorization service partly because it compiles to a single standalone binary, with no runtime to install.

## Does Rust really use fewer resources?

Yes for CPU and memory, in every rewrite with published numbers. The size of the gain depends on what it replaced: a Python service and a Go service both cut CPU by roughly 70 to 80%, while a rewrite of already-native Zig code ran only 2 to 5% faster and fixed a memory leak instead. Cloudflare's 70% came another way: it replaced NGINX, written in C, plus Lua scripts, and credits faster Rust code over Lua, a design that reads headers in place and shares data across threads without locks, and far fewer new TLS connections.

| Rewrite | From → to | CPU | Memory | Latency |
| --- | --- | --- | --- | --- |
| Anthropic Messages API (2026) | Python → Rust | About 70% less per request (one region) | More than 80% less per request | Median time to first token about 20% lower |
| Cloudflare Pingora proxy (2022) | NGINX + Lua → Rust | About 70% less | 67% less | Median TTFB 5 ms lower, p95 80 ms lower |
| Grab counter service (2025) | Go → Rust | 4.5 cores instead of 20 at 1,000 QPS | Not published | p99 similar or slightly worse |
| Bun runtime (2026) | Zig → Rust | 2 to 5% faster | 2,000 in-process builds: 609 MB instead of 6,745 MB | Not the goal |
| Discord Read States (2020) | Go → Rust | Beat Go on every metric after tuning | Cache raised to 8 million entries | GC spikes every ~2 minutes gone |
| npm authorization (2019) | Node.js → Rust | Not published | Not published | Not published |

Sources: [Anthropic](https://www.anthropic.com/research/migrating-api-to-rust), [Cloudflare](https://blog.cloudflare.com/how-we-built-pingora-the-proxy-that-connects-cloudflare-to-the-internet/), [Grab](https://engineering.grab.com/counter-service-how-we-rewrote-it-in-rust), [Bun](https://bun.sh/blog/bun-in-rust), [Discord](https://discord.com/blog/why-discord-is-switching-from-go-to-rust), [npm case study (PDF)](https://www.rust-lang.org/static/pdfs/Rust-npm-Whitepaper.pdf).

Read the scope next to each number. Anthropic's CPU figure covers one region from August to October, while its memory figure covers all of production. Grab's own post describes its savings as 70%, 80% and "5x" in different places. Discord published its results as charts, not figures, and npm reported reliability rather than speed: no alerts in a year and a half in production. None of these are lab benchmarks; they are one team's service before and after.

![Published Rust rewrite results: Anthropic about 70 percent less CPU and over 80 percent less memory per request, Cloudflare Pingora 70 percent less CPU and 67 percent less memory, Grab 20 cores down to 4.5, Bun 2 to 5 percent faster with a fixed build memory leak](https://res.cloudinary.com/wasicloudinary/image/upload/v1795763556/fabwaseem/projects/zbomls8pbvo87t0kjaeg.jpg)

## Where do the savings come from?

Three things: two come from the language and one from the redesign. Rust removes the interpreter and the garbage collector, which is where much of the CPU and memory goes in Python and Node.js services. And a new design, such as a shared connection pool, can save as much as faster code.

- **No interpreter, no runtime.** Python executes bytecode and adds work every time it creates or frees an object; Rust runs machine code without that overhead. Anthropic doesn't break down its savings, but its memory per request fell by more than 80% after the move.
- **No garbage collector.** Memory is freed when values go out of scope, so there are no pauses and no heap headroom kept for the collector. This is what fixed Discord's two-minute spikes.
- **Design changes made during the rewrite.** Cloudflare's latency gains came from architecture, not faster code: Pingora shares connections across threads, so connection reuse for one major customer rose from 87.1% to 99.92%, and across all customers it opens a third as many new connections per second. The old NGINX setup already handled requests in the sub-millisecond range.

Anthropic's own history is the clearest evidence that the full rewrite matters. In March 2026 the team moved only the output-parsing code to Rust and called it from Python. CPU fell by just 8 to 10%, because each token of a reply crossed between Python and Rust three times. In the team's words, "the gains didn't justify the added complexity," so they rewrote the whole request path.

## Why doesn't 70% less CPU mean 70% faster responses?

Because most of a request's time is not spent on your CPU. An API call waits on the network, the database or, for Anthropic, the model itself. Rust shrinks the part your code spends computing, so CPU and memory fall sharply while end-to-end latency improves only by the share that was compute.

Anthropic's numbers show this exactly: about 70% less CPU, but only about 20% lower median time to first token, measured on Claude Code requests between roughly September 7 and 21. Grab saw the same pattern in a Go service: a fraction of the cores, with p99 latency "similar (or perhaps even slightly worse)." If your goal is a faster product rather than a cheaper bill, profile first. A Rust rewrite of a service that spends 90% of its time waiting on Postgres will cut the bill, not the wait.

## What did Anthropic's Rust rewrite change?

Anthropic moved the core request path of its Messages API, built in Python in 2023, to Rust, and switched public traffic in September 2026. Claude wrote a first version of the core request path in about a week. Checking it against the Python service took from April to August.

The checking is the useful part for anyone planning a rewrite. In April the Rust API passed about 98% of its ported tests, yet only about a quarter of real production inputs produced matching results. The team ran production requests through both versions, called the model once and replayed its reply into Rust so both saw the same output, and compared every step down to the first differing byte. The match rate went from 24% in late April to 99.6% by August. Auditing the ported tests also turned up more than 100 assertions that had been weakened along the way.

Their summary of the project: "Verification, not generation, had become the bottleneck." Bun's rewrite tells the same story. About 50 Claude Code workflows ported 535,496 lines of Zig in 11 days, at around $165,000 in tokens at API pricing, and the rewrite still introduced 19 known regressions, all since fixed. Writing the new code got cheap; proving it is equivalent did not.

## Why not just use Go?

Go is the usual alternative, and for some projects it is the better fit. Microsoft ported the TypeScript compiler to Go rather than Rust, and reports most builds about 10x faster with roughly half the memory. The team called it a port, not a rewrite, and chose Go because its code closely resembles the existing TypeScript codebase and garbage collection costs them little.

The pattern across the public cases:

- **Pick Go** when you are porting an existing codebase line by line, the work is mostly batch processing where GC barely matters, or the team needs to move fast without learning ownership and lifetimes. TypeScript's [native compiler](https://fabwaseem.com/blog/typescript-7) is the reference case.
- **Pick Rust** when garbage collection itself is the problem (Discord), when every core matters at very high request rates (Grab, Cloudflare), or when memory-safety bugs are the main source of crashes (Bun).

Rust's costs are real either way, and they are mostly people and time; the cost section below covers them.

## Should you rewrite your Node.js or Python service in Rust?

Usually not the whole service. Rewrite when one measurable cost is large and comes from the runtime itself: CPU per request at high traffic, memory held by the garbage collector, or GC pauses in tail latency. If the service mostly waits on a database or another API, tune it or move one hot path first.

![Decision flow for a Rust rewrite: measure where time goes, keep and tune I/O-bound services, move one CPU-heavy hot path to a Rust addon, and only rewrite the whole service when CPU, memory or GC cost is large and verified with shadow traffic](https://res.cloudinary.com/wasicloudinary/image/upload/v1795763558/fabwaseem/projects/xyore6n7d0sbcdeam6xc.jpg)

| Your situation | Better first move |
| --- | --- |
| Most request time is database or network wait | Optimise queries, caching and connection pooling |
| One function dominates CPU (parsing, hashing, image or text processing) | Move that function to a Rust addon |
| Long-running jobs are slow but not latency-critical | Move them to a queue and scale workers |
| GC pauses show up in p99 latency at steady load | Tune the runtime, then consider Rust for that service |
| CPU per request is the main infrastructure bill at high QPS | A full rewrite, verified against production traffic |

For Node.js, also check what you can get without leaving JavaScript. In [Bun vs Node.js](https://fabwaseem.com/blog/bun-vs-node) and [Hono vs Express](https://fabwaseem.com/blog/hono-vs-express), changing the runtime or the HTTP framework moved throughput substantially with no rewrite at all. If your heavy work is AI calls, the bottleneck is usually the model provider, and a queue helps more than a faster language; see [AI pipelines with BullMQ](https://fabwaseem.com/blog/ai-automation-pipeline-nodejs-bullmq).

## How do you move one hot path from Node.js to Rust?

Build a native addon with napi-rs. You write the function in Rust, mark it with `#[napi]`, and the CLI compiles a `.node` file plus an `index.js` loader with `index.d.ts` types that you import like any module. Your Express, Fastify or NestJS code stays as it is.

```toml title="Cargo.toml"
[package]
name = "hotpath"
version = "0.1.0"
edition = "2024"

[lib]
crate-type = ["cdylib"]

[dependencies]
napi = "3"
napi-derive = "3"

[build-dependencies]
napi-build = "2"
```

```rust title="build.rs"
fn main() {
  napi_build::setup();
}
```

```rust title="src/lib.rs"
use napi_derive::napi;

/// Runs on a whole string in one call, so the Node.js-to-Rust boundary is crossed once.
#[napi]
pub fn count_words(text: String) -> u32 {
  text.split_whitespace().count() as u32
}
```

```json title="package.json"
{
  "name": "hotpath",
  "napi": { "binaryName": "hotpath" },
  "scripts": { "build": "napi build --platform --release" },
  "devDependencies": { "@napi-rs/cli": "^3.11.1" }
}
```

```bash
npm install
npm run build
node -e "const { countWords } = require('./index.js'); console.log(countWords('rust   makes this fast'))"
# 4
```

Mind the boundary. Strings and plain objects are converted and copied each time they cross between JavaScript and Rust, while Buffers and typed arrays can be passed without a copy. Crossing the boundary too often is what limited Anthropic's partial port. Send a whole buffer, file or batch in one call, and never call into Rust once per item in a loop. Measure the function before and after; if it was not near the top of your profile, the addon only adds a build step. Python has the same path: PyO3 and maturin build a Rust function into a module you import from Python.

## You already run Rust in your JavaScript toolchain

Most of the "Rust rewrite" wave that JavaScript developers feel is in their tools, not their services. Vite 8 replaced esbuild and Rollup with Rolldown, a Rust bundler the [Vite team reports](https://vite.dev/blog/announcing-vite8) at 10 to 30x faster than Rollup, with Linear's production build dropping from 46 seconds to 6. Oxlint and Biome replace ESLint, and Bun itself now runs on Rust.

The gains there are large because linting and bundling are pure CPU work over thousands of files, the best case for native code. On this site's own codebase, Oxlint ran the same rules as ESLint in 118 ms instead of 9.56 seconds; the details are in [Oxlint vs Biome vs ESLint](https://fabwaseem.com/blog/oxlint-vs-eslint), and the bundler change is covered in [Vite 8 and Rolldown](https://fabwaseem.com/blog/vite-8-rolldown-upgrade-guide). Adopting these tools is the cheapest way to get Rust's speed without writing any Rust.

## What does a Rust rewrite cost?

The bill is mostly people and time, not code. Rust takes longer to learn than Go or TypeScript, async Rust behaves differently from both, some libraries you rely on may not exist yet, and you run two stacks until the old one is retired. AI agents now write the first draft quickly, but the result still needs the same review and verification.

- **Learning curve.** npm's engineers needed about a week to learn Rust and write a service they built in about an hour in Node.js and two days in Go. Grab's advice was to avoid introducing lifetimes too early and accept extra clones at first.
- **Async is different.** Grab's team called async Rust "quite different" from Go, and a blocking synchronous Redis call inside async code made their service perform poorly.
- **Library gaps.** Grab had to use unofficial or less popular crates for Datadog and Redis, and wrote its own parser because its internal Go config libraries had no Rust version.
- **Two stacks at once.** Monitoring, logging and alerting have to run for both stacks for as long as both are in production, which npm named as its biggest downside.
- **Machine-translated code is a starting point.** Bun's write-up describes a point in the port where "absolutely none of it worked yet," and about 4% of its Rust code is `unsafe`, partly because Bun wraps C and C++ libraries.
- **Move in slices where you can.** Replace one endpoint or one service at a time behind the same API, rather than switching everything on one day.

## How do you rewrite a service in Rust safely?

Treat the old service as the specification and prove equivalence on real traffic before you switch. Passing the ported unit tests is not enough: Anthropic's Rust API passed 98% of its tests while matching only a quarter of production inputs.

1. **Measure first.** Record CPU per request, memory, p50 and p99 latency, so you can tell later whether the rewrite paid off.
2. **Write language-independent tests.** HTTP-level tests that run against both versions catch what ported unit tests miss.
3. **Shadow production traffic.** Send copies of real requests to the new service, compare the responses and log the first difference. Group the differences so one bug does not show up as thousands.
4. **Fix external nondeterminism.** If a dependency's answer varies, as a model reply does, record it once and replay it to both versions.
5. **Switch gradually.** Move a small percentage of traffic, compare error rates and cost, then increase.
6. **Keep the old service runnable** until the new one has been stable through a full release cycle.

## Key takeaways

- Teams rewrite in Rust to cut CPU and memory at scale, remove garbage-collection pauses and eliminate memory-safety bugs.
- Anthropic reports about 70% less CPU and more than 80% less memory per request after moving its Messages API from Python to Rust; Cloudflare reports about 70% less CPU and 67% less memory for Pingora.
- Latency improves much less than CPU, because most request time is spent waiting on I/O: Anthropic's time to first token fell about 20%.
- Partial rewrites can disappoint: Anthropic's first Python-calls-Rust step saved only 8 to 10% CPU because data crossed the boundary three times per token.
- AI agents now write rewrites in days, but verifying behaviour against production traffic took Anthropic from April to August.
- For most Node.js and Python services, move one CPU-heavy function to a Rust addon before considering a full rewrite.

## Questions

### Why is everyone rewriting in Rust?

Mostly to cut infrastructure cost and crashes. Rust runs native code without a garbage collector, so a busy service needs less CPU and memory and has no GC pauses, and its compiler rejects use-after-free and data races. Anthropic, Cloudflare, Grab, Discord and Bun all cite one of those reasons.

### Does Rust use less memory than Go or Node.js?

Usually, because it has no garbage collector holding extra heap. Anthropic reports more than 80% less memory per request after leaving Python, and Cloudflare 67% less than its NGINX setup. Against Go, the bigger win is often the absence of GC pauses rather than raw memory.

### Is a Rust rewrite worth it?

Only when a measured cost comes from the runtime itself: CPU per request at high traffic, memory held by a garbage collector, or GC pauses in tail latency. If the service mostly waits on a database or other APIs, tuning or moving one hot path to Rust gives most of the benefit for far less risk.

### Why did Bun rewrite in Rust?

Bun moved from Zig to Rust because many of the bugs on its fix list were use-after-free, double-free and missed frees on error paths, which Rust's ownership rules turn into compile errors. The rewrite took 11 days with Claude Code, ran 2 to 5% faster, and cut memory for 2,000 in-process builds from 6,745 MB to 609 MB.

### Why did Anthropic rewrite its API in Rust?

API traffic grew roughly 1,800 times between early 2024 and fall 2026, and the Python service's CPU cost grew with it. A partial port saved only 8 to 10% CPU, so the team rewrote the core request path; it now uses about 70% less CPU and more than 80% less memory per request.

### Is Rust faster than Go?

For CPU-heavy services it usually needs fewer resources: Grab's counter service went from 20 cores in Go to 4.5 in Rust at the same load. Its p99 latency stayed similar, though, and Microsoft chose Go for the TypeScript compiler port, so the right choice depends on the workload.
