Save the planet, code in Rust

Marlon
Co-owner & Lead developer
Max
Software engineer
Save the planet, code in Rust
The number of data centers worldwide is constantly increasing, and so is their electricity consumption. One way to become more power-efficient is certainly the constant development of better hardware, but we as developers should do our share. This post shows how coding in Rust can help to use existing resources more efficiently, to help preserve our planet — at least a little bit.

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.

Screenshot old PHP implementation Overview of reservations in the old PHP application (data has been anonymized)

Screenshot new Rust implementation 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 CPUApp CPUMemory UsageHits
PHP121 %37 %276 MiB283
Rust212 %57 %31 MiB11,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 CPUApp CPUMemory UsageHits
PHP44 %20 %96.3 MiB69
Rust55 %20 %10.8 MiB2,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 CPUApp CPUMemory UsageHits
PHP41 %13 %108.6 MiB285
Rust2.6 %1 %12.1 MiB315

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!

(our services)

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

> Contact us

1: R. Pereira et al., “Ranking programming languages by energy efficiency,” Science of Computer Programming, vol. 205, p. 102609, May 2021, doi: 10.1016/j.scico.2021.102609.

Stay up-to-date

Stay up-to-date with our work and blog posts?

Related articles

July 31, 2020

Rust wide web (2/5)

Ruben has experience with a lot (and I mean a lot) of programming languages. When I asked which ones, he could name 21 off the top of his head. He loves experimenting with them, seeing what each can and can’t do. What makes a language unique? What can one language do better than the other? Why was Ruben the one to first evangelize Rust within Tweede golf? Let’s ask him!
In February of 2024, I was invited by Matthias Endler of Corrode to join him on his podcast Rust in Production. We discussed how Tweede golf uses Rust in production, to ensure the safety and security of critical infrastructure software.
Asynchronous programming is pretty weird. While it is straightforward enough to understand in principle (write code that looks synchronous, but may be run concurrently yada yada yada), it is not so obvious how and when async functions actually perform work. This blog aims to shed light on how that works in Rust.