← pradnya.dev

Benchmarking API Performance: Python vs Go vs Rust

Same API, three production stacks, nine metrics. Where Rust actually matters, and where it doesn't. I built the same API in Rust, Go and Python with each ecosystem's production stack, wrote down six hypotheses, and measured seven workloads on nine metrics across 4 full runs. Rust wins on CPU-bound work and tail latency, the language barely matters when the request is waiting on I/O, two one-line changes moved the numbers more than the language did, and with PostgreSQL on the path the driver mattered more than the language.

November 2026, updated with Phase 2 · 22 minute read · code, harness and raw data

Why I did this

On 25 November 2026 Anthropic published Migrating our Messages API to Rust, describing a full rewrite of their Messages API from Python to Rust. The numbers they report are the kind that get a language chosen: about 70% less CPU per request, more than 80% less memory per request, and roughly 20% lower median time to first token, after public traffic moved to Rust in September 2026. Their CPU figure is for one region and their latency figure is from a subset of Claude Code traffic, and their post is about the verification work far more than about Rust itself. It is a production migration at a scale where traffic had grown roughly 1,800× in under three years.

That post made me want to know the shape of that gain. "Rust is faster than Python" is true and useless. Which kinds of request get faster, by how much, which parts of the gain are the language, which parts are the runtime, which parts are a library choice, and what does it cost in code, dependencies and build time? Those questions can be answered at small scale, on one machine, with everything else held constant. So I held everything else constant.

What this is and is not. This is a lab benchmark: one laptop, seven synthetic endpoints, ten seconds of load per cell, an in-memory store instead of a database. It is not the production traffic of a large service, and nothing here predicts what a migration like Anthropic's would yield. What it can do is isolate where the differences come from, which production numbers cannot.

The setup

Same contract, three implementations

ImplementationStackLinesDeps
RustAxum 0.8 on Tokio, serde_json, sha2 with the asm feature17793 crates
GoStandard library net/http with Go 1.22 routing, encoding/json, crypto/sha2561820
Python ×10FastAPI on Uvicorn with uvloop, httptools and orjson; ten workers, one per core9321 packages
Python ×1The same code, one worker, so the cost of scaling Python stays visible9321 packages

The four rows are not all the same comparison. Rust, Go and single-worker Python each run one process with one store and one lock, so every request sees every write: that is the identical-semantics comparison and the fair application-level one. Ten-worker Python is the native-deployment comparison: it is how FastAPI is actually run on a ten-core box, and it has ten stores and ten locks, so its write numbers carry a weaker consistency guarantee (more on that below). Read the Python ×1 row against Rust and Go for fairness, and the Python ×10 row for what you would deploy.

Every implementation serves the contract in docs/API.md: a store seeded with 10,000 items, and seven endpoints that each isolate one thing an API service spends time on.

ScenarioEndpointWhat it isolates
healthGET /healthThe framework and HTTP stack floor
getGET /items/{id}Routing, a shared-state read, a small JSON encode
listGET /items?limit=200Serializing 200 objects
createPOST /itemsJSON decode, validation, a write under a lock
cpuGET /compute/hash?n=50005,000 chained SHA-256 rounds on the request path, no I/O
ioGET /io/wait?ms=10A 10 ms async wait, no CPU
mixedGET /mixedA 5 ms simulated database wait, 200 hash rounds, JSON

What the harness measures

One shared contract test, plain Python with no dependencies, runs against every implementation before the harness will benchmark it. If two implementations answer the same request differently, their numbers are not comparable, so the harness refuses. All of this ran on Apple M5 with 10 cores, with the load generator on the same machine. That understates every absolute number equally; read everything as relative. The numbers below are medians of 4 full runs; the report in the repo shows the min–max spread for every cell.

Six hypotheses, written before the first run

The point of writing them down first is to find out which ones are wrong.

#HypothesisVerdict
1CPU-bound work: Rust and Go an order of magnitude ahead of Python; Rust leads Go by a small constantMostly right, with a twist. Rust leads Go by 1.9×, not a small constant, and only after a library flag. With the sha2 crate's default software SHA-256, Rust was slower than Go.
2I/O-bound work: all three converge; the differences are memory and tail, not throughputConfirmed, and Rust was the slowest.
3Serialization: Rust > Go > Python, but orjson narrows the gap more than people expectConfirmed, dramatically, once FastAPI's default response path was bypassed.
4Tail latency: Rust has the flattest p99/p50; Go's tail shows the GC; Python degrades fastestTail confirmed; the GC explanation was not. A run with Go's collector off did not flatten its tail.
5Memory: Rust < Go ≪ Python × workers; the ratio matters for densityConfirmed at the baseline, wrong per connection. Rust is the heaviest per idle connection.
6Engineering cost: Rust costs the most lines and the longest buildHalf right. Rust and Go cost the same lines; Rust's cost is the dependency graph and the build.

The experiment in one chart

Throughput of Rust, Go and Python on four workloads, each as a share of the fastest
Each group is one workload; each bar is that language's throughput as a share of the fastest on that workload, with the real requests per second on it.

Read it top to bottom. On CPU-bound work Rust is well ahead. On a simple lookup Rust and Go are tied (1.0× across four runs). On a request that is waiting on something, all three are within 9% and Rust is last. The language does not decide how fast a waiting request goes. It decides what it costs while it waits, and that is where Rust's edge over Go lives: in p99, CPU per request and memory, not in requests per second on ordinary endpoints.

Where Rust wins

1. CPU on the request path

Throughput per workload for the four implementations
Requests per second at 64 connections, all seven workloads. Each panel has its own scale; whiskers are the min–max across runs.

On 5,000 rounds of SHA-256 per request, Rust served 20,468 requests a second (19.2k–23.7k across runs). Go served 10,975, ten-worker Python 3,135, one-worker Python 927. That is 1.9× Go and 6.5× Python with every core lit. Ten Python workers gave 3.4× the throughput of one, not 10×, because each worker's threadpool still serialises on its own GIL and the ten processes contend with each other and with the load generator.

Past saturation is where the difference stops being a factor and becomes a behaviour:

Share of requests that missed a 1-second deadline under CPU overload
500 concurrent clients, 20,000 hash rounds per request, a 1-second timeout.

Rust missed the deadline on 0 requests in every run, with a p99 of 174 ms. Go missed 0.21% with a p99 of 707 ms. Ten-worker Python missed 7.95% at the median and anywhere from 0.45–21.47% run to run; one-worker Python 63.30%. The detail I find most interesting: Go's median under overload (69 ms) is close to Rust's (59 ms) while its p99 is 707 ms against 174. One hypothesis is scheduling: Go keeps most requests moving and lets a tail of them wait out the deadline, while Tokio's work-stealing spreads the delay evenly across every in-flight request. I have not profiled either runtime to confirm that, so treat it as the next thing to check, not a finding. What is measured is the shape: if you have an SLO, evenly is what you want.

Under connection overload rather than CPU overload (1,000 concurrent clients on the lookup endpoint), Rust, Go and ten-worker Python all served every request. Single-worker Python timed out 2.31%.

2. Tail latency

Median to p99 latency on a simple lookup for each implementation
Left dot p50, right dot p99 on the lookup endpoint. A short line is a predictable service; a long one is a service whose median lies to you.

On the lookup endpoint, Rust's p99 is 0.91 ms and 2.6× its median. Go's is 1.98 ms at 6.5×. Ten-worker Python's is 5.30 ms at 8.5×. Rust's lookup p99 ranged from 0.71–2.04 ms across the four runs, so its median is the fair number and the comparison holds at the worst run too. Single-worker Python has a flat tail (2.0×) but a median six times higher, which is a different kind of slow. Go's p95 is 0.89 ms against a 0.30 ms median. My first draft blamed the garbage collector; the counterfactual below says otherwise. On the CPU endpoint the gap between Go's median and its p99 opens to 23.8× (1.60 ms to 38.16 ms); Rust's stays at 2.0×. If your SLO is written against p99 rather than the median, Rust is the only one of the three whose tail stays flat under load.

3. Density: CPU and memory per request

CPU milliseconds per 1,000 requests by workload
Server CPU-milliseconds per 1,000 requests, all seven workloads. Lower is better.

Per 1,000 lookups, Rust spends 22 ms of CPU, Go 29, ten-worker Python 96. On the I/O endpoint, where throughput is equal, the CPU cost is not: Rust 31 ms, Go 64, Python 110. That is the number Anthropic's 70% is made of.

Idle memory of each implementation
Resident memory of the whole process tree before any load.

Idle memory: Rust 9.9 MB, Go 13.4 MB, one Python worker 66.0 MB, ten Python workers 710.7 MB, which is 72× Rust. Under load Rust holds 13 to 15 MB where Go holds 2.2× that and ten-worker Python 53.8×. Across a fleet, that is the bill.

Where Rust does not win

I/O-bound endpoints

Throughput on a 10 ms wait, nearly equal for all four implementations
Requests per second on the 10 ms async wait against the theoretical ceiling of 64 connections ÷ 10 ms.

On the 10 ms wait, all four implementations land between 5.2k and 5.7k requests per second, a spread of 9%, with p99 between 12.7 and 14.6 ms. The theoretical ceiling with 64 connections and a 10 ms wait is 6,400 per second, and everyone is near it. Rust is last. Tokio's sleep(10 ms) completes in about 12.3 ms on this macOS build even with a single connection, while Go's timer and uvloop's sleep for about 11.3 ms. The mixed endpoint tells the same story: Go 10.9k, Python 10.2k, Rust 8.6k requests per second. If your service is "call the database and return the result", Rust buys you 2.1× to 3.5× less CPU and 2.0× to 54.7× less memory while you wait. It does not buy you speed.

Serialization

On the 200-item list, Rust is 1.1× Go and 1.6× tuned Python (60.2k vs 54.1k vs 37.5k requests per second). Serde is fast; so are encoding/json and orjson. Serialization alone is not a reason to choose Rust.

Memory per connection

This inverted my hypothesis. Holding 1,000 idle keep-alive connections cost Rust 31.6 KB each, Go 22.9 KB, ten-worker Python 18.5 KB, single-worker Python 11.8 KB. Hyper's per-connection buffers are the largest of the three. For a 100,000-connection streaming fan-out that is 3.2 GB versus 2.3 GB versus 1.9 GB, which is to say none of them is the bottleneck; the fixed cost is.

Two one-line changes

Before and after throughput for the two framework defaults the first run exposed
Hollow dot: framework default. Filled dot: after the change.

I kept the first run, with every framework at its defaults, in the repo on purpose, because the gap between it and the final numbers is a result.

FastAPI. In the baseline, Python's list endpoint was 14× slower than Rust and burned 1.7 ms of CPU per request. That is not orjson being slow. A handler that returns a dict goes through FastAPI's pure-Python jsonable_encoder before the response class serializes it, and on 200 dicts that pass costs more than everything else in the request combined. Returning ORJSONResponse(...) directly from the handler skips it. One line per handler took Python's list throughput from 5.2k to 37.5k requests per second (7.3×) and the gap to Rust from 14× to 1.6×. If you run FastAPI in production and have never checked which path your responses take, check.

Rust. In the baseline, Rust was slower than Go on the CPU endpoint: 8.4k requests per second against 14.7k. Go's standard-library crypto/sha256 uses the ARM SHA-2 instructions out of the box. Rust's sha2 crate defaults to a software implementation. Enabling its asm feature took Rust to 20.5k (2.4×) with an identical digest. The language sets the ceiling; the library decides whether you reach it, and Go's standard library reaches it by default more often than Rust's crate ecosystem does.

Things I did not expect

Ten-worker Python has no shared memory, so it is a different comparison. The contract test found this before the benchmark did: an item created with a POST on one Uvicorn worker is a 404 on the other nine (1 hit in 10). Python's only route to all ten cores is ten processes, which means ten copies of every in-memory structure and no shared state without Redis or a database round-trip that Rust and Go do not need for the same feature. This is the biggest practical difference in the study and it is architectural, not a benchmark number. It also means the ten-worker create figures are not the same guarantee as the other three: ten process-local locks are not one shared lock. The single-worker row is the like-for-like comparison; the PostgreSQL phase will give all four the same semantics.

What it costs

Lines of code, dependencies, cold start and build time per implementation
Lines for the identical contract, transitive dependencies, cold start and clean build time.

Rust and Go took the same number of lines for the same contract: 177 and 182. Python took 93. Rust ships 93 transitive crates; Go ships 0; Python 21 packages. A clean release build is 13.7 s for Rust with LTO on and 3.2 s for Go, and the gap widens with project size. Artifacts are 1.3 MB and 9.3 MB. Cold start to first healthy response: 10 ms for Rust, 13 ms for Go, 173 ms for one Python worker, 416 ms for ten.

Go is the honest competitor in this study. On ordinary endpoints its throughput is a tie with Rust: 166.2k against 163.6k requests per second on the lookup, within 10% on the list and the write. Its gaps are the CPU ceiling (1.9×), the p99 (2.2× on the lookup, 6.2× under CPU load), and the per-request CPU and memory (1.4× and 2.2×), all with zero dependencies, a 3.2-second build and the same line count. The reason to pick Rust over Go is the ceiling and the tail, not the throughput on the endpoints most services are made of.

Every number in one place

Medians of 4 runs. The full report with p95, p99.9, max, CPU utilisation, memory growth per request and the min–max spread per cell is in the repo.

ScenarioRust req/sGoPython ×10Python ×1Rust p99 msGoPython ×10Python ×1
health164,679166,92274,14431,6201.022.015.973.93
get163,587166,17273,44828,8090.911.985.304.35
list60,24854,06537,50814,9432.346.178.008.29
create160,374140,49249,18019,7380.832.136.236.29
cpu20,46810,9753,1359276.1638.1653.8478.66
io5,1925,5985,6625,44513.6012.7012.7914.58
mixed8,64110,94010,1749,8809.207.818.078.20
ScenarioRust CPU ms/1kGoPython ×10Python ×1Rust peak MBGoPython ×10Python ×1
health2129833213.230.0722.269.2
get2229963513.429.8719.367.6
list1271231866715.230.2716.767.7
create243614451824.9777.3987.1202.8
cpu4186503,0261,10113.340.1732.571.0
io31641106413.226.5719.567.8
mixed721031178413.627.3720.567.9

The create row's memory is the store growing by roughly two million items during the run, not the runtime; the per-item cost is the fairer comparison and is above.

So what should you pick

Read against Anthropic's post, the shape fits. Their service is CPU-heavy per request (parsing and streaming tokens), which is the column where this study finds Rust's largest advantage, and their 70% CPU reduction sits inside the range this lab measured between tuned Python and Rust on CPU-bound and serialization work. That is a consistency check, not a prediction; their numbers come from production traffic at a scale this experiment does not approach.

Phase 2: the same contract on PostgreSQL

Ten-worker Python does not offer the same consistency guarantee as the others, so its write numbers are not the same comparison. The fix is to put one database behind all four. Every service gained a database mode: with DATABASE_URL set, the lookup, list, create and mixed endpoints go to PostgreSQL through each ecosystem's production driver and pool (Rust sqlx, Go pgx, Python asyncpg), and the mixed endpoint drops its simulated 5 ms wait because the round-trip is the I/O. Pools are eager and the per-service connection budget is kept close to equal: 16 for each single-process service, 2 per worker (20 in all) for ten-worker Python, which cannot share a pool across processes. With the database in place, ten-worker Python passes the contract test without the process-local-state exception.

Per language, requests per second with the in-memory store and with PostgreSQL on four endpoints
Light bar: in-memory store. Solid bar: PostgreSQL behind the same endpoint, with its share of the in-memory figure. Medians of 2 Postgres runs; every scenario starts from a freshly reloaded 10k-row table.

The language stops being the first-order variable. With PostgreSQL on the path, every implementation lands at a fraction of its in-memory throughput on the store endpoints: Rust at 26% on the lookup, Go at 40%, ten-worker Python at 33%. And the order changes. Go now leads Rust on the lookup (66,235 vs 41,883 requests per second), on create (47,900 vs 35,615) and on the lookup-plus-hash endpoint (54,439 vs 36,479), and trails only on the 200-row list (14,984 vs 17,730). Rust also spends more CPU per lookup than Go now: 78 against 66 ms per thousand. This is sqlx versus pgx, not Rust versus Go. Both are the mainstream production driver in their ecosystem, and the wire-protocol and row-decoding path inside the driver is where a database request spends its time. tokio-postgres is widely reported to be faster than sqlx; I have not tested it, and it is the first thing I would try.

Rust versus Python narrows on simple queries and widens on wide ones. On the lookup the gap went from 2.2× in memory to 1.7× on Postgres; on create from 3.3× to 1.9×. On the 200-row list it went the other way, from 1.6× to 3.4×, because every row that comes back from asyncpg becomes a Python dict before orjson ever sees it: 1,415 ms of CPU per thousand list requests against 329 for Rust. Row materialisation is the new serialisation tax, and it is paid in Python.

Tails behind a database. Go's lookup p99 is 1.71 ms, now flatter than Rust's 1.96 ms, and ten-worker Python's is 10.79 ms. Under connection overload on the lookup (1,000 clients, 1-second timeout) nobody timed out; p99 was 34 ms for Rust, 23 for Go, 150 for ten-worker Python.

The pool is the knob. Medians of one to two milliseconds on a query that Postgres answers in well under a millisecond are 64 clients queueing for 16 connections. I held the per-service connection budget near-equal on purpose, because a connection budget is what a database hands out; a pool-size sweep is the obvious next experiment, and ten-worker Python's per-worker pools are the one budget that cannot be shared.

What the database does not change. Memory and startup belong to the runtime: idle RSS is still 8.1 MB for Rust, 17.3 MB for Go and 699.5 MB for ten Python workers. The CPU-bound and pure-wait endpoints, which never touch the store, are unchanged within noise. And the phase 1 findings that mattered most, the CPU ceiling and what happens under CPU overload, are untouched by a database that is not on that path.

So the hypothesis that "the gaps narrow again behind a database" is half right. Rust versus Python narrowed on lookups and writes, widened on wide reads, and Rust versus Go inverted. Behind a database you are choosing a driver and a pool size. The language decides memory, cold start, and what happens when you are CPU-bound, which it already did in phase 1.

ScenarioRustGoPython ×10Python ×1
get, req/s41,88366,23524,13914,107
list, req/s17,73014,9845,2372,181
create, req/s35,61547,90018,50811,078
mixed, req/s36,47954,43917,6118,202
get, p99 ms1.961.7110.797.74
get, CPU ms/1k786626971
idle RSS MB8.117.3699.565.0
cold start ms2212328157

PostgreSQL 17 on the same laptop, pool of 16 per single-process service and 2 per worker for ten-worker Python, medians of 2 runs. The full tables are in the repo.

Caveats

Reproduce it

git clone https://github.com/pradnyaw25/api-performance-lab
cd api-performance-lab
brew install go oha          # plus a Rust toolchain
make setup build             # python venv, cargo + go release builds
make contract                # the gate: every implementation must pass
make bench                   # ~12 minutes -> results/<run>/REPORT.md
python3 bench/aggregate.py results/<run-a> results/<run-b> -o results/aggregate
python3 bench/charts.py results/aggregate   # the PNGs and GIFs in this post

Code, harness, raw oha output, the untuned baseline run and the 4 final runs: github.com/pradnyaw25/api-performance-lab. If a configuration is unfair to your favourite language, open an issue. That is what the contract test is for.