Nidal Siddique Oritro
← Back to posts

Laravel rewrite in Rust: the false premise and why PHP was never the problem

codebox 13 min read
Laravel rewrite in Rust: the false premise and why PHP was never the problem
Summary i reran Laravel's Rust benchmark on my own hardware. The 18x is real, and it measures Eloquent's data path, not PHP.

If you don’t wanna read such a long post, TLDR at the bottom.

Laravel introduced Laravel Rust this week. Same conventions, same feel, new syntax. Models, migrations, a container, routes, a queue, all the stuff you already know from Laravel PHP, written in Rust. That part is nice, and i’m not here to knock it.

The part i’m here for is the claim that came with it. Their benchmark has Laravel Rust at 1,774 requests per second and Laravel PHP at 97. That’s about 18x, and the story people took from it was simple: PHP is the slow part, Rust fixed it.

The “PHP is dead” crowd came back with elation. Nobody i saw stopped to check whether the language was what got blamed here. The comparison was Laravel’s default Eloquent path in PHP against a brand new Rust ORM, and “Rust vs PHP” is a very different thing to say about that. So i spent some time running the numbers myself, to see where the cost actually is.

What the endpoint does

Everything in this benchmark is one endpoint. A JSON feed of 50 chirps with their authors, out of a database that has 200 chirps and 10 authors. No writes, no auth, no search. The authors say the dataset is small on purpose, because they didn’t want to test MySQL, they wanted a workload that touched a database.

In Laravel PHP the whole thing is one line:

php
Chirp::with('author')->orderByDesc('id')->paginate(50);

That’s three queries, and i checked with the MySQL general log instead of trusting the docs:

sql
select count(*) as `aggregate` from `chirps`
select * from `chirps` order by `id` desc limit 50 offset 0
select * from `authors` where `authors`.`id` in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Then Eloquent builds 50 model objects. Each row goes through newFromBuilder(), its attributes get copied in and the retrieved event fires. Attribute access goes through __get() and getAttribute() with casts and mutators. Then toArray() walks the whole graph again to turn it into JSON. The table has 200 rows, so MySQL has nothing to do. Every millisecond in this benchmark is the framework deciding what to do with rows after they come back.

How i ran it

i didn’t build this from scratch. It’s the repo from ihxnnxs, which already has Laravel’s benchmark and the Symfony ports. i ran it on my own hardware, one target at a time, with a few additions of my own (more on those below).

App1 vCPU, 4 GiB per container, in a 4-core test container
DatabaseMySQL 8.4, 1 vCPU, 4 GiB, one per app
Load generatork6 2.3.0 on a separate machine on the same LAN, 2 cores pinned, 0.78 ms average round trip
Workload50% latest feed, 20% next page, 10% previous page, 20% detail, gzip checked on every response

Things to know before you read any number below:

  • Each configuration ran once. Differences of a few tenths of a millisecond at steady load are noise. Only the gaps in the ramp are solid.
  • The Rust numbers include Caddy inside the 1 vCPU container, because the k6 script checks for gzip and the Rust binary doesn’t compress on its own. Rapira measured removing Caddy at +27% throughput, which on my numbers would put Rust at around 1,200. That’s an extrapolation from their run, not something i measured, and it’s still under Symfony on Swoole.
  • Two unrelated containers (a Postgres and a mail catcher) were running on the same host the whole time. They sat at about 0% CPU, but they were there.
  • My absolute numbers are lower than the ones in the other posts because it’s a different machine. The ordering is what transfers.

Benchmark 1: steady load

What this measures: how much latency and CPU each stack needs to serve a calm 50 requests per second for five minutes. No stress, no ramp. It tells you what a request costs when nothing is going wrong.

Config: 50 RPS flat, 5 minutes, the best clean 10 second window is scored (same as upstream).

Be aware: 50 RPS is a tiny load. A phone can do it. So look at the CPU column before the latency column.

Stackp50p95p99CPURAM
Symfony + Swoole, Doctrine scalar2.06 ms3.626.333.9%54 MiB
Symfony + FrankenPHP, Doctrine scalar2.49 ms3.534.196.1%66 MiB
Laravel Rust (with Caddy)2.61 ms4.145.035.5%74 MiB
Laravel PHP, toBase() join, JIT4.12 ms6.237.5614.6%139 MiB
Laravel PHP, Eloquent, JIT13.36 ms17.8720.2257.4%206 MiB
Laravel PHP, Eloquent as shipped17.06 ms22.9328.7774.7%113 MiB

Zero failures and zero dropped iterations on all six. Over the whole five minutes Rust and Symfony on FrankenPHP both land at 2.50 ms p50, so i wouldn’t read anything into the gap between them.

Stock Laravel uses three quarters of a core to serve fifty requests a second. Symfony, which is also PHP, does it with 3.9%.

CPU used to serve 50 requests a second: Laravel 74.7 percent, Symfony on Swoole 3.9 percent

Benchmark 2: the ramp

What this measures: where each stack stops being healthy. Load climbs from 1 to 2,000 requests per second over twenty minutes, and i record where latency starts to climb and where errors begin.

Config: same endpoint mix as above. “Knee” is the highest 10 second window before the first error where p95 is still under 100 ms. “Best clean window” is the highest throughput with zero failures, which is how upstream scores it.

Be aware: the generator keeps the ramp going after a stack falls over, so overall failure percentages for the stacks that break early look terrible and tell you more about the ramp than the stack. That’s why i report where each one broke, not how many requests failed overall.

StackKnee (p95 under 100 ms)Best clean window
Symfony + Swoole1,557 RPS1,566
Laravel Rust (with Caddy)959976
Symfony + FrankenPHP9381,025
Laravel PHP, toBase() join, JIT392402
Laravel PHP, Eloquent, JIT7680
Laravel PHP, Eloquent as shipped5960

Both the Rust and the Swoole knees are CPU-bound. docker stats shows the Swoole container pinned near 102% from 930 seconds in and the Rust one near 100% from 575 seconds in, so neither is the load generator running out of room.

Rust stays clean right up to its limit. At 942 requests per second its p95 is 3.9 ms, against 3.6 ms for Symfony on Swoole at the same rate, so they’re level there. Then Rust goes from a p95 of 5.3 ms to 252 ms in ten seconds. Symfony on Swoole keeps going for another 600 requests per second before its p95 passes 100 ms.

The 18x does reproduce on my box. Rust is 16x stock Laravel (959 against 59). Symfony on Swoole is 26x.

Where each stack stops being healthy, from Symfony on Swoole at 1,557 RPS down to stock Laravel at 59

Benchmark 3: the same Laravel app, one change at a time

What this measures: how much of the gap is the framework’s data layer, and how much is something else. This is the one that answers the “was it PHP” question, because everything here is Laravel PHP.

Config: same Laravel app, same Octane, same FrankenPHP, same PHP version, same MySQL. Three variants: default Eloquent with JIT off, default Eloquent with JIT on, and a join that skips models with JIT on. The third one is ihxnnxs’s ProjectionChirpController, 56 lines against 19 for the default one. It joins the authors in SQL, selects plain rows with toBase() and formats the timestamps by hand.

Be aware: JIT is a separate variable. Without the middle row, the join version would get credit for gains that JIT gave it, so i added that row myself.

Laravel PHPKneep50 at 50 RPS
Eloquent, JIT off59 RPS17.06 ms
Eloquent, JIT on76 RPS13.36 ms
toBase() join, JIT on392 RPS4.12 ms

JIT is worth about 1.3x. Skipping the model layer with JIT held on in both rows is worth 5.2x. Same language, same server, same database, about 40 more lines of controller code.

One Laravel app, three variants: 59, 76 and 392 RPS

Benchmark 4: the control on the app server

What this measures: whether the gap is PHP or the app server Laravel happens to run on.

Config: Laravel here runs on FrankenPHP through Octane. The repo has a Symfony build on FrankenPHP as well. Same server, same PHP, same container limits. Only the framework and the data layer differ.

Be aware: this is the control i’d put the most weight on, because the comparison is fair in the one way the original wasn’t.

On FrankenPHPKnee
Symfony (Doctrine scalar)938 RPS
Laravel (Eloquent, JIT on)76 RPS

That’s 12x with the language and the app server held constant.

Symfony at 938 RPS against Laravel at 76 RPS on the same FrankenPHP server

So why isn’t the claim defensible

The claim was “Rust outperforms PHP by a lot”. What the benchmark shows is that Rust outperforms Laravel’s default PHP data path by a lot, which is true and not the same thing.

Symfony beat the premise on its own. Now you might say Swoole is a different server, so it isn’t a fair comparison. Fair, which is why the FrankenPHP row exists. Symfony on FrankenPHP matched Laravel Rust at the knee, and Symfony on Swoole passed it by about 600 requests per second. If the language was the bottleneck, a PHP app wouldn’t be able to do that. It did, with the same PHP the Laravel numbers came from.

None of this says Rust is bad. Rust stays clean up to its limit, it’s lean on memory, and the Laravel Rust framework looks like a serious piece of work. It says PHP is not the dead weight the headline made it look like.

Why Laravel PHP failed here

It’s the framework. Not the language, and not the server.

What Laravel Eloquent does with each row compared with Symfony scalar hydration

Laravel is built around developer happiness, and Eloquent is the clearest example. You write Chirp::with('author')->paginate(50) and you get models. Every row becomes an object with magic behind it. Reading $chirp->author goes through __get(). Reading an attribute goes through getAttribute(), which checks for casts and mutators on every access. Hydrating fires events. Serializing it goes back through all of that again. That’s a lot of convenience, and every bit of it runs on every row of every request.

It’s a fair trade when you have a 500 line controller and a team to onboard, because the abstraction saves you real time. On a read-heavy JSON endpoint where nobody touches a model, it’s overhead, and you pay for it 50 times a request, on every request. At 50 requests per second that was three quarters of a core.

Here’s what i didn’t do. i showed that skipping the model layer is worth 5.2x, and i showed the shape of the queries, but i didn’t profile Laravel to split that 5.2x between __get(), casts, event dispatch and toArray(). A flame graph would tell you that, and i’d like to see one. What the numbers support is “the model layer is the cost”. Where inside it is the next question.

Laravel Rust carries the same shape over. i ran the same MySQL log on the Rust version and it issues the same three statements, a count, the chirps, and the authors with IN (10, 9, 8, ... 1), down to the laravel_through_key alias. Rust makes the work much cheaper, which is why it gets from 59 to 959, but it’s still doing that work.

Why Symfony performed better

It does less. The Symfony port in the repo uses Doctrine’s scalar hydration, so rows come back as plain arrays and never become entity objects. It also joins the authors in SQL, so the feed is two queries (a count and one joined select) instead of three. And the serializer reads those arrays directly.

There’s no __get(), no cast pass, no events, no second walk over an object graph. Symfony is not magic and not faster by nature. It lets you ask for exactly the data you want, in the shape you want it, and it doesn’t build anything you didn’t ask for. That costs you more code per endpoint, and in this benchmark it’s worth about 12x over the default on the same server.

The fix doesn’t need Symfony either. The join version of the Laravel controller is that same idea, written in Laravel, and it got 5.2x for about 40 lines.

What i’d do about it

If i were looking at 97 requests per second on this endpoint i’d open a profiler before i opened a new language. A flame graph on that is gonna show hydration, casts, relation matching and toArray(). That’s an afternoon of work.

For a company with years of Laravel code, hundreds of dependencies and a team that knows the stack, a rewrite is the expensive way to find that out. Changing how the hot endpoints read data gets most of the gain, and you keep the language, the packages and the people.

Taylor said this week he’s not sure how to do soft deletes in Laravel Rust and that he never loved them in the PHP version. That’s the right instinct, and it applies to the model layer too.

i’m not telling anyone to leave Laravel. The 18x is real, it measures the data layer, and PHP was never the bottleneck.

TL;DR

Laravel introduced Laravel Rust with 1,774 requests per second against 97 for Laravel PHP, about 18x, and the framing was that PHP is the slow part. i reran it on my own hardware. The 18x reproduces: Rust is 16x stock Laravel. But Symfony, still PHP, on the same app server as Laravel, did 938 against Laravel’s 76. Symfony on Swoole did 1,557 and passed Rust. Inside Laravel itself, skipping the Eloquent model layer was worth 5.2x with the same language, server and database. The Rust version runs the same three queries as the PHP one. So the claim is true about Laravel’s default data path and not about PHP. Rust isn’t bad here. PHP isn’t the problem either.

Sources

Comments