Save the planet, code in Rust
According to the International Energy Agency (IEA), the estimated global electricity consumption by data centers was 240 – 340 TWh in 2022. This corresponds to 1.0 – 1.3 % of the total electricity demand. These numbers don't include crypto mining, which presumably consumed an additional 110 TWh or 0.4 % of the global electricity demand.
One can argue that the increase in electricity usage of 20–70 % from 2015 to 2022 is quite moderate compared to the workload increase of 340 %. This is certainly due to more efficient hardware, but also a more efficient usage of the available resources. And that’s where coding in Rust can make a difference!
Why Rust is green
It is not easy to compare the efficiency of programming languages in general, as the results are influenced by a lot of factors. Still, a paper by R. Pereira et al.1 compares 27 programming languages regarding their energy and memory efficiency as well as speed. To get the most objective result possible, they used the fastest available implementation of a variety of benchmarks for comparison. We should keep in mind though that the following numbers build upon highly optimized implementations for synthetic benchmarks, which don't necessarily match exactly with real-world operations.
The paper states that Rust is second place in terms of energy consumption and execution time and consumes only about 3 % more energy and executes only 4 % slower than the first place, C. According to these measurements Rust is exceptionally energy efficient, escecially if you compare it the the energy consumtion of PHP, which used ~ 28 times more energy or Python, which used ~ 74 times more energy than the Rust implemententation did.
Our real-world experience
At Tweede golf, we've recently experienced Rust's great efficiency and speed in a rewrite of one of our projects. We built a book club management application in Rust that was previously written in PHP, featuring the same functionality. This application allows book clubs to borrow books, and a hierarchy of libraries to manage reservations, distribution and invoicing of these books.
Overview of reservations in the old PHP application (data has been anonymized)
Same overview in the modern Rust implementation (data has been anonymized)
Honestly, saving CPU and memory wasn't the main reason for the rewrite; maintenance was. The Rust application is much easier to maintain, and version upgrades of the Rust compiler or dependencies are unlikely to break the application (unnoticed). Additionally, we were more productive using Rust. This is likely due to the good ecosystem and toolchain, such as a compiler that provides mostly helpful error messages. Also the strict type system helps to write correct software. In our example, the Rust rewrite took only about half of the development time from the original PHP implementation, not including any requirements engineering. Of course, reimplementing an existing implementation is generally easier and faster than building the original implementation, but we hadn't expected the difference in development time to be quite so big.
Benchmarks
Let’s take a look at the performance comparison.
For the tests, we used the open-source HTTP benchmarking tool Siege with 25
concurrent users.
The applications ran locally on an Ubuntu laptop with a 12th Gen Intel® Core™ i7-12700H and 32 GiB of
memory.
As the applications are run as containers anyway, we used
the docker stats
command to estimate the resource
consumption and Docker Compose to limit resources where needed.
The most straightforward benchmark we performed does not put any constraints on either the PHP or Rust app, or on Siege. We performed the tests against the reservation overview page, as this put some computational load on the application as well as the database. The Rust version consists of an application container and a container for the DB. The PHP version also contains an nginx that is responsible for serving all the static files such as CSS, JS, and icons. The Rust application simply serves them itself. The total CPU usage is the sum of all containers, while the App CPU and Memory Usage only concern the Rust or PHP container.
Max CPU
First, we put the full load on each, the PHP and Rust application, for 20 seconds.
The corresponding Siege command is siege <url> --time 20s --concurrent 25 --no-parser --benchmark
Total CPU | App CPU | Memory Usage | Hits | |
---|---|---|---|---|
PHP | 121 % | 37 % | 276 MiB | 283 |
Rust | 212 % | 57 % | 31 MiB | 11,297 |
We can see that Rust outperforms PHP by nearly 4000 % in the number of hits, that is, the number of pages loaded during the 20 seconds. Additionally, we see that PHP requires nearly nine times as much memory compared to the Rust application.
Limited CPU
You could argue that this is a somewhat unfair comparison, as Rust was able to make use of more CPU resources. Therefore, we limited the CPU usage for the app containers to 20 % each and performed the same test.
Total CPU | App CPU | Memory Usage | Hits | |
---|---|---|---|---|
PHP | 44 % | 20 % | 96.3 MiB | 69 |
Rust | 55 % | 20 % | 10.8 MiB | 2,275 |
The total CPU usage is still higher on the Rust application, which has to do with the fact that Rust was able to handle 33 times more requests and therefore put a higher load on the database. Again, we can see that PHP uses about nine times more memory.
Normal surfing
These, while significant, were very theoretical tests.
In practice, there would not be this many users on the page, and they would also take a few seconds to react to a page load before loading the next.
To test this more realistically, we provided Siege with nine different URLs that are randomly accessed between zero and ten
seconds after the page has been loaded.
To get a better overview, we let this test run for 60 seconds instead of 20.
The corresponding Siege command is siege --file urls.txt --time 60s --concurrent 25 --no-parser --internet --delay 10
Total CPU | App CPU | Memory Usage | Hits | |
---|---|---|---|---|
PHP | 41 % | 13 % | 108.6 MiB | 285 |
Rust | 2.6 % | 1 % | 12.1 MiB | 315 |
Here, we can really see the benefit of Rust in terms of resource consumption. While the PHP application requires 13 % of a CPU to handle the requests, the Rust container only requires a single percent. Also in terms of memory usage, Rust outperforms PHP by a factor of nine, again.
Let’s compare this to the findings in the paper by R. Pereira et al.1 introduced earlier. According to their benchmarks, PHP is 26.6 times slower than Rust and consumes 66.9 % more memory and thus consumes 28.4 times more energy. This is less significant than what we gathered from our quick benchmarks, though still significant. One likely reason for the difference is that we do not benchmark on synthetic tests with highly optimized algorithms, as done in the paper. Another reason is that we changed not only the programming language, but parts of the underlying architecture and SQL queries as well. Nevertheless, our comparison is still interesting, as the provided functionality is exactly the same.
Takeaways
It turns out that Rust is not only much more fun for the developer but also very resource-friendly. We've seen the new Rust application to be up to 40 times faster than the old PHP implementation. In our cluster, we observed a significant decrease in recourses used.
There is scientific literature that rigorously compares various programming languages and it backs this outcome. When scaled big enough, programs written in Rust could help to reduce the growth of electricity usage, for example in data centers. Not only that: Rust will also lower your costs for computing power; you need less to achieve more - and save the planet!
Thinking of (saving the planet) using Rust?
Get help from the experts!
- reduce first-project risk
- interop with existing code
- train your team on the job