Skip to content
Moweb
Web DevelopmentProduct Engineering

Migrating to Rust from PHP or Python: Speed, Server Cost and the Hot Path Test

· December 2, 2026
Migrating to Rust from PHP or Python: Speed, Server Cost and the Hot Path Test

Moving a backend from PHP or Python to Rust can cut CPU and memory per request sharply. Here is what the published numbers say, how much work a migration really is, and the Hot Path Test we use to plan one that pays for itself.

Your backend works. Customers use it, the team ships on it, and it pays the bills. But the cloud invoice keeps climbing, a few endpoints top every slow-request report, and adding servers has become the default answer to growth.

More companies are answering that problem by moving their backends to Rust. This guide covers what a move from PHP or Python to Rust actually gets you, what the published numbers say, how much work it really is, and the **Hot Path Test** we use to plan a migration that pays for itself.

What moving to Rust does for your servers

When CPU-heavy backend code moves to Rust, four things improve, and each one shows up somewhere a business can see it:

  • **Lower CPU per request.** Each request costs less processor time, so the same server carries more traffic and you need fewer or smaller instances. Anthropic measured about 70% less CPU per request after moving its API from Python to Rust.
  • **Less memory per request.** Rust compiles to native code and has no garbage collector, so it holds less in memory while it works. Anthropic's figures show memory per request down by more than 80%, and memory is often what forces a move to a bigger instance.
  • **Faster responses.** CPU-bound endpoints return sooner. Anthropic cut median time to first token by about 20%.
  • **More requests per second from the same hardware.** When each request needs less CPU, each core serves more of them. That is headroom for traffic spikes without autoscaling into more servers.

What Anthropic's move from Python to Rust tells us

In November 2026, Anthropic published Migrating our Messages API to Rust, describing how it moved the API behind Claude from Python to Rust. Anthropic reports that the new Rust service "uses about 70% less CPU per request" and "cuts median time to first token by about 20%". A chart in the same post shows memory used per request falling by more than 80%.

Three details in that post matter more than the headline numbers to anyone planning their own migration:

  • **A half-measure barely helped.** Before the full rewrite, Anthropic moved only the parser into Rust. That "reduced the API's CPU usage by about 8% to 10%", because each token crossed the Python and Rust boundary three times and the crossings ate much of the gain. The big saving came when the whole request path ran in Rust.
  • **Writing the new code was the easy part.** Anthropic says Claude wrote a first version of the core request path in about a week. Proving it behaved exactly like the old service took months.
  • **The old system became the referee.** The team found its roughly 700 integration tests were not enough. Instead, they shadowed real production traffic through both versions and compared the results step by step, using the live Python service as the source of truth.

The lesson is not "rewrite everything tomorrow". It is that Rust delivers large savings on the code that carries the load, and that a migration succeeds or fails on how carefully you prove the new system matches the old one.

Rust vs PHP vs Python: the numbers

On CPU-heavy work, Rust is in a different league from both PHP and Python, and Python is not a faster alternative to PHP. Independent benchmarks agree on that ordering (lower is better in every row):

| Measure | Rust | PHP | Python | Source | | --- | --- | --- | --- | --- | | Pure computation task (fannkuch-redux), seconds | 3.81 | 130.83 | about 300 | Benchmarks Game | | Text search with regular expressions (regex-redux), seconds | 0.78 | 1.88 | 1.41 | Benchmarks Game | | Run time across the study's benchmarks, C = 1 | 1.04 | 27.64 | 71.90 | Pereira et al., 2017 | | Energy used, C = 1 | 1.03 | 29.30 | 75.88 | Pereira et al., 2017 | | Peak memory, Pascal = 1 | 1.54 | 2.57 | 2.80 | Pereira et al., 2017 |

Two things stand out. When the work runs in the language's own loops, Rust finished the computation task about 34 times faster than PHP. When PHP and Python hand the work to a library written in C, as with regular expressions, the gap shrinks to about 2 times. So the saving depends on where your CPU time is actually spent. The Pereira study ran in 2017, before PHP 8's JIT and newer Python releases, so read its ratios as a direction rather than a forecast.

Concurrency tells the same story from another angle:

  • **PHP with PHP-FPM** serves each request in its own worker process. The PHP manual says the `pm.max_children` setting "sets the limit on the number of simultaneous requests that will be served", so more simultaneous traffic means more processes and more memory.
  • **Python (Django, FastAPI, Flask)** runs on CPython, whose global interpreter lock lets only one thread execute Python bytecode at a time. Apps scale across cores by running more processes. Python 3.13 added an optional build without the lock, but it is not the default.
  • **Rust** compiles to native code with no interpreter and no global lock, so one process can use every core and keep many connections open at once.

Is moving to Rust a rewrite?

Yes. Moving a backend to Rust means rewriting that code in Rust; there is no converter that turns PHP or Python into production Rust. What you control is how much you rewrite at once.

A big-bang rewrite of the whole application is the highest-risk way to do it. Features freeze, two systems drift apart, and the business waits months to see any saving. The lower-risk way is phased: move the code that burns the most CPU first, prove each piece on real traffic, and keep going only while each phase pays for itself. Many systems end up mostly in Rust this way. Others stop after two or three hot paths because the rest of the app is not where the cost is.

Three ways in, from one hot path to a full migration

**1. A Rust module inside your existing app.** Both PHP and Python can load native modules written in Rust. For PHP, ext-php-rs provides "bindings and abstractions for the Zend API to build PHP extensions natively in Rust", for PHP 8.1 and later, so a Laravel or Symfony app calls the Rust code like any built-in function. For Python, PyO3 provides "Rust bindings for the Python interpreter" and powers widely used libraries such as Polars, pydantic-core and orjson. A public PHP benchmark of route matching averaged about 2.22 seconds per run in native PHP and about 0.31 seconds as a Rust extension, roughly seven times faster on the same task.

**2. A Rust service next to your app.** The CPU-heavy work moves into a small Rust service that your PHP or Python app calls over HTTP, gRPC or a queue. The main app keeps authentication, admin screens and business workflows; Rust takes the job that dominates the CPU graph. This is the usual second phase, and each service is a clean, testable unit.

**3. A full migration of the request path.** When the API itself is the hot path, as it was for Anthropic, the whole request path moves to Rust. Done safely, it runs in shadow beside the old system until the outputs match, then traffic moves over in stages.

Remember Anthropic's 8% to 10% parser result: whichever way in you choose, move a whole unit of work, so data crosses between the old code and Rust once per request, not many times.

When moving to Rust will not pay off

Rust is not a cure for every slow backend, and pretending otherwise wastes budget.

In June 2026, Mattias Geniar published a candid account of moving Oh Dear's uptime-checking servers from PHP-FPM to Laravel Octane and then going back. Each request spent around 400 milliseconds waiting on an outbound check, so a faster runtime saved very little; his conclusion was that "the gains were too small to justify the extra moving parts." The same logic applies to Rust. If requests spend their time waiting on the database, a third-party API or a slow disk, a faster language will not move the needle. Fix these first:

  • **Slow and repeated database queries**: missing indexes, N+1 queries, unbounded reports.
  • **Work done on every request that could be cached**: settings, permissions, computed totals.
  • **Synchronous work that belongs in a queue**: emails, PDFs, image processing, webhooks.
  • **Runtime start-up cost**: long-running workers such as Laravel Octane for PHP, or a properly sized Gunicorn or Uvicorn setup for Python.

The Hot Path Test

Before we recommend any move to Rust, we run the system through six checks. Each one is a yes or no, and the order matters.

  • **Measure.** Profile production, not a laptop. Which endpoints, jobs and functions consume the most CPU time in total, not just the slowest single request?
  • **Classify.** For each hot path, is the time spent computing or waiting? Computing (parsing, matching, pricing, transforming, encrypting, generating) is a Rust candidate. Waiting (database, network, disk) is not.
  • **Fix the cheap things first.** Queries, caching, queues and runtime settings. If these bring CPU down to where you need it, stop here.
  • **Draw the boundary.** Decide what moves to Rust in this phase, and count how often data must cross between the old code and Rust per request. Move whole units of work.
  • **Prove parity before you switch.** Run the Rust path in shadow mode on real traffic, compare its output with the existing code, and switch only when they match.
  • **Count the saving in servers.** Measure CPU and memory per request before and after, then translate them into instances, cores and monthly cost. If the saving does not show on the invoice, the phase is not finished.

A system that passes checks 1, 2 and 4 has a real Rust case. One that fails check 2 needs query, caching or architecture work instead, and we will tell you so.

Where Rust usually pays off first

These are the kinds of backend work that tend to pass the Hot Path Test:

  • **Pricing, rating and rules engines** that evaluate many rules per request.
  • **Search, matching and routing** over large in-memory data.
  • **Large imports and exports**: CSV, Excel, XML and JSON parsing and validation.
  • **Report generation and data aggregation** done in application code.
  • **Image, document and file processing** in background workers.
  • **Cryptography, hashing, signing and compression** at high volume.
  • **High-traffic APIs and gateways** where every request does validation and transformation.

Admin panels, CRUD screens and low-traffic workflows are usually the last to move, if they move at all. They change often and rarely dominate CPU.

Get a free Rust migration review

If your servers are growing faster than your traffic, or a handful of endpoints keep showing up in every slow-request report, book a free 30-minute Hot Path review with Moweb's backend engineers. We will look at where your CPU actually goes, tell you which parts would gain from Rust and which need simpler fixes, and estimate what each phase would save. Our PHP, Laravel, cloud and DevOps and application maintenance teams can then carry the migration through, phase by phase, so the saving lands on your invoice.

Book your free Hot Path review or tell us about your system.

FAQ

**Is moving to Rust a full rewrite?** For the code you move, yes. A phased migration rewrites the hottest paths first, proves each one on real traffic, and only continues while each phase pays for itself.

**Is Python faster than PHP if we are going to switch anyway?** Not for CPU-heavy work, on the published benchmarks above. If you are going to rewrite for performance, rewrite in Rust.

**Will Rust help if our app is slow because of the database?** Usually not. If requests spend their time waiting on queries or external APIs, fix those first. Rust helps when the CPU is busy computing.

**How do you make sure the Rust version behaves exactly like the old one?** We run both on real traffic in shadow mode and compare outputs before switching, the same verification approach Anthropic described for its own migration.

**Can our team maintain the Rust code?** We keep each Rust component small, documented and behind a clear interface. We can maintain it for you or train your team as the migration grows.

Start a project