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

> Our Rust backend stack (Tokio, axum, SQLx, Serde), piece-by-piece migration with PyO3 or NAPI-RS vs a full rewrite, shadow-traffic checks, and when to skip Rust.

Source: https://bytevancer.com/blog/rust-backends-part-2-how-we-build-migrate · Published 2026-11-26 · ByteVancer · Backend Engineering

**The stack we build Rust backends on, the two ways to move an existing Python or Node.js API, how to prove the new one matches the old, and when not to use Rust.**

> **Part 2 of 2: Rust Backends** — [Part 1: Why Rust, and what Anthropic's migration shows](https://bytevancer.com/blog/rust-backends-part-1-why-rust-anthropic-migration) · **Part 2: How we build and migrate a backend in Rust (you're here)**

[Part 1](https://bytevancer.com/blog/rust-backends-part-1-why-rust-anthropic-migration) 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

- **Async runtime** — Crate: Tokio; Role: The runtime the server runs on
- **Web framework** — Crate: axum (or Actix Web); Role: Routes and request handlers
- **Database** — Crate: SQLx; Role: Queries checked at compile time; Postgres, MySQL, MariaDB, SQLite
- **Data formats** — Crate: Serde; Role: JSON and other formats in and out
- **Build** — Crate: Cargo; Role: Dependencies, builds, crates.io
- **Interop** — Crate: PyO3 / NAPI-RS; Role: Rust 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**
- Is the database or a third-party API the bottleneck? → **Don't reach for Rust yet; fix queries, caching or the call pattern**
- Is the slow part one function with a big input and a small result? → **Move it into Rust with PyO3 or NAPI-RS**
- Does the hot path touch data at every step (per token, per row, per message)? → **Rewrite the whole path in Rust; a crossing per item eats the gain**
- Otherwise → **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**
Old API as oracle → Shadow real traffic → Call once, replay → Compare fingerprints → Alert on new diffs → Switch in stages

1. **Old API as oracle** — The current service's answers are the truth the new one must match.
2. **Shadow real traffic** — Copies of live requests go to the Rust service; users only see the old one.
3. **Call once, replay** — Slow or paid dependencies are called once and replayed to both sides.
4. **Compare fingerprints** — Hashes and checkpoints, not raw customer data.
5. **Alert on new diffs** — A scheduled job groups mismatches and flags any new kind.
6. **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](https://bytevancer.com/services/custom-software-development) 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](https://bytevancer.com/blog/rust-backends-part-1-why-rust-anthropic-migration)
2. **Part 2: How we build and migrate a backend in Rust (this post)**

## Frequently asked questions

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

## Keep reading

- [Rust Backends, Part 1: Why Rust, and What Anthropic's Migration Shows](https://bytevancer.com/blog/rust-backends-part-1-why-rust-anthropic-migration) — Why teams pick Rust for backends, what Anthropic got from moving its Messages API to Rust, how Rust compares to Go, Node.js, Python and Java, and the costs.
- [All ByteVancer engineering articles](https://bytevancer.com/blog)

## 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](https://bytevancer.com/services/custom-software-development) or [talk to the team](https://bytevancer.com/contact).

## Sources

- [Anthropic — Migrating our Messages API to Rust (Nov 25, 2026); some figures from its charts](https://www.anthropic.com/research/migrating-api-to-rust)
- [Tokio](https://tokio.rs/)
- [axum documentation (docs.rs)](https://docs.rs/axum/latest/axum/)
- [Actix Web](https://actix.rs/)
- [GitHub — launchbadge/sqlx](https://github.com/launchbadge/sqlx)
- [Serde](https://serde.rs/)
- [The Cargo Book](https://doc.rust-lang.org/cargo/)
- [PyO3 user guide](https://pyo3.rs/)
- [NAPI-RS](https://napi.rs/)
- [Rust Blog — Rust compiler performance survey 2025 results (Sep 10, 2025)](https://blog.rust-lang.org/2025/09/10/rust-compiler-performance-survey-2025-results/)
- [Google Open Source Blog — Rust fact vs. fiction: 5 insights from Google's Rust journey in 2022 (Jun 27, 2023)](https://opensource.googleblog.com/2023/06/rust-fact-vs-fiction-5-insights-from-googles-rust-journey-2022.html)
- [Stack Overflow Developer Survey 2025 — Technology](https://survey.stackoverflow.co/2025/technology)
- [Python docs — Glossary (GIL, free threading)](https://docs.python.org/3/glossary.html)

---

*This is the Markdown copy of https://bytevancer.com/blog/rust-backends-part-2-how-we-build-migrate — the page itself has the interactive charts and diagrams.*
