Rust Backends, Part 2: How We Build and Migrate a Backend in Rust

Part 1 covered why teams pick Rust and what Anthropic got from it. This part is practical: the stack we build Rust backends on, the two ways to migrate an existing Python or Node.js API, how to prove the new one matches the old, and when to skip Rust. Anthropic's write-up is the public reference for what works at scale; the rest is our approach.

TL;DR

  • Our default stack: Tokio, axum (or Actix Web), SQLx, Serde and Cargo.
  • Moving one function into Rust (PyO3 for Python, NAPI-RS for Node.js) is cheap, but it pays only when data rarely crosses the boundary. Anthropic's incremental step, Rust parsing output called from Python, saved only 8–10% CPU, because each token crossed the boundary three times.
  • A full rewrite needs a referee: run the old API as the oracle and compare outputs on shadowed traffic.
  • Tests are not enough. In April about 98% of Anthropic's tests passed while only about a quarter of production inputs matched.
  • Skip Rust when the database is the bottleneck, traffic is modest, or the team can't absorb the learning curve.

Our default Rust stack

LayerCrateRole
Async runtimeTokioThe runtime the server runs on
Web frameworkaxum (or Actix Web)Routes and request handlers
DatabaseSQLxQueries checked at compile time; Postgres, MySQL, MariaDB, SQLite
Data formatsSerdeJSON and other formats in and out
BuildCargoDependencies, builds, crates.io
InteropPyO3 / NAPI-RSRust inside Python (built with maturin) or pre-compiled Node.js addons

We default to axum on Tokio and use Actix Web when a team already knows it. SQLx checks queries at compile time, so a query that no longer fits the schema, such as one using a renamed column, fails the build instead of failing in production.

Two ways to migrate

Piece by piece

PyO3 or NAPI-RS

Keep the Python or Node.js service; move one hot function into a Rust module.

Low risk, ships in small steps.

Pays only if data crosses the boundary rarely. Anthropic's incremental step, Rust parsing output called from Python, cut CPU just 8–10%: each token crossed Python and Rust three times.

Rewrite the hot path

Rust owns the request

The old service hands the request to Rust, which does the rest.

Where Anthropic got about 70% less CPU per request.

Needs a full verification plan before real traffic moves.

Which one we'd pick
  1. 01Is the database or a third-party API the bottleneck?

    YES → Don't reach for Rust yet; fix queries, caching or the call patternNO ↓

  2. 02Is the slow part one function with a big input and a small result?

    YES → Move it into Rust with PyO3 or NAPI-RSNO ↓

  3. 03Does the hot path touch data at every step (per token, per row, per message)?

    YES → Rewrite the whole path in Rust; a crossing per item eats the gainNO ↓

  4. profile first, then decide

How to verify a rewrite: the old API is the referee

Anthropic treated its Python production API as an oracle. Real requests were shadowed to the Rust version and the outputs compared. The model was called once and its output replayed, so both sides saw the same input. No customer text was read; comparisons used checkpoints and fingerprints. A script ran every 10–30 minutes and alerted a coordinating Claude agent when a new kind of difference appeared.

Tests alone would have misled them. In April about 98% of tests passed, but only about a quarter of production inputs matched. Per Anthropic's charts, the match rate started at 24%, dipped to 6.5% in early May before climbing to 99.6%. Exhaustive testing was never an option: 20 on/off options make more than a million combinations, about 12 days at one second each.

Our verification plan for a Rust rewrite
  1. 01

    Old API as oracle

    The current service's answers are the truth the new one must match.

  2. 02

    Shadow real traffic

    Copies of live requests go to the Rust service; users only see the old one.

  3. 03

    Call once, replay

    Slow or paid dependencies are called once and replayed to both sides.

  4. 04

    Compare fingerprints

    Hashes and checkpoints, not raw customer data.

  5. 05

    Alert on new diffs

    A scheduled job groups mismatches and flags any new kind.

  6. 06

    Switch in stages

    Move traffic in slices once the match rate holds; keep the old path as fallback.

Where AI coding agents fit

Anthropic ran Claude in three loops: an inner loop in a sandbox with a sub-second input check and a fake model, an outer loop that fixed pull requests against shadowed traffic, and a memory loop of notes, CLAUDE.md, tests, lints and merge checks. Per Anthropic's charts, merged changes went from under 100 per two weeks to about 1,000 in late June and over 3,000 from August 10.

Their sharpest line: "A verifier can tell an agent whether the outputs matched, but not whether the fix is good enough to ship." That is the line we hold too. Agents write a lot of code; an engineer decides what ships.

When NOT to choose Rust

  • The database or an outside API is the bottleneck. A faster server waits just as long.
  • Traffic is modest and the bill is fine. Anthropic moved after about 1,800x growth.
  • The product changes weekly and nobody on the team knows Rust. At Google, only a third felt as productive in Rust within two months.
  • Fast rebuilds matter to your workflow: 55% of respondents in Rust's 2025 compiler survey wait over 10 seconds for one.
  • You can't hire or train for it. Stack Overflow 2025: 14.8% of respondents use Rust, 57.9% use Python; our inference is a smaller hiring pool.
  • Python threads are fighting over the GIL: try Python 3.13's free-threaded build, or one PyO3 module, first.

How we'd migrate yours

  1. Profile: find the hot path and what it costs today.
  2. Pick the shape: one PyO3 or NAPI-RS module, or a rewrite of the whole path.
  3. Build on the default stack, with SQLx-checked queries.
  4. Shadow, compare and switch in stages, with the old service as fallback.

If your API is hitting CPU or latency limits, our custom software development team can profile it and tell you whether Rust is worth it before any code is written.

The whole series

  1. Part 1: Why Rust, and what Anthropic's migration shows
  2. Part 2: How we build and migrate a backend in Rust (this post)

Questions, answered

Should I rewrite my backend in Rust?

Only if profiling shows CPU or latency in your own code is the limit, and the gain beats slower builds and a learning curve. If the database or an outside API is the bottleneck, Rust won't help.

What is the best Rust web framework for a backend?

We default to axum on the Tokio runtime, with SQLx for database access and Serde for JSON. Actix Web is a solid alternative, especially if your team already knows it.

Can I use Rust inside an existing Python or Node.js app?

Yes. PyO3 builds native Python modules in Rust (packaged with maturin), and NAPI-RS builds pre-compiled Node.js addons. It pays off only when data doesn't cross the boundary at every step; Anthropic's incremental step, Rust parsing output called from Python, saved just 8–10% CPU.

How do you test a Rust rewrite against the old API?

Shadow real traffic to the new service and compare its outputs with the old API as the oracle, using fingerprints rather than raw customer data. Anthropic found tests alone misleading: about 98% passed while only about a quarter of production inputs matched.

How much faster is Rust than Python for an API?

There is no single number. After Anthropic's move, its Messages API used about 70% less CPU per request and median time to first token fell about 20%. Your gain depends on where the time goes.

Planning a move to Rust?

We profile your API, pick the migration shape and verify the new service against the old one before any traffic moves. See our custom software development services.