How Rust Delivers Speed and Security for Off-Chain Storage

We're introducing more code written in Rust to Ceramic's codebase.

How Rust Delivers Speed and Security for Off-Chain Storage

Introduction

At 3Box Labs, the core team building Ceramic, we are continually striving to produce the best solution for off-chain storage. This requires a combination of simplifying the user experience, while also delivering on the demanding requirements of users when it comes to performance and safety. To accomplish this, we’ve started to introduce more code written in Rust to our codebase.

For those not familiar with Rust, Rust is a language that focuses on three areas

  • Performance
  • Reliability
  • Productivity

We will dive into what Rust offers for performance and reliability, and how that allows us to produce fast, reliable, and secure off-chain storage on Ceramic. We will not talk about productivity in this blog post, as that is somewhat subjective and leads to language debates (even if we do love the productivity).

1. Performance With Efficient Memory Usage and Systems Programming

One of the main reasons that Rust is able to achieve its performance is that it does not use a virtual machine or garbage collector. When Rust code is built, it produces machine code and binaries at a systems level, like C or C++. To further improve on this, Rust also has zero-cost abstractions. Zero-cost abstractions allow developers to focus on writing clean, performant, and maintainable code, as the compiler will remove the abstractions used and produce optimized machine code.

Source: TechEmpower Round 22 Benchmarks

Rust regularly performs similarly to C and C++ in benchmarks, while often having some measurable performance improvement over Go. In the above image, you will see a Lng column indicating the language a webserver is written in. Rust webservers, denoted with rs and purple in color, outperform C (C - red), C++ (C++ - red), and Go (Go - green).

Go (Purple) vs Rust (Blue) Comparison. Source.

Additionally, Rust regularly produces better 75% and 99% response times due to better memory management. Discord switched from Go to Rust largely because these spikes wreak havoc on systems and user experience. They would have @mention spikes of 2 seconds or higher with Go, while Rust would consistently be in the sub-second range.

2. Reliability: Safety and Uptime

The decentralized nature of Web3 applications demands an unyielding commitment to safety. Additionally, our users demand availability of their data, so uptime is of primary importance. When dealing with systems languages like C and C++, most people are familiar with the multitude of common vulnerabilities and exposures (CVEs) that are filed, with a large number due to issues around memory management.

As mentioned above, Rust performance is a result of its close-to-the-metal code and memory management without a garbage collector. To ensure safety, Rust has introduced a memory ownership system, enforced at compile time, rather than runtime. This ownership system determines who is using data, and when it is free to release that data and, once freed, ensures that no one else can improperly use that data.

fn main() {
    {
        let x = 42;

        println!("x: {}", x);
    }

    println!("x: {}", x); // ERROR: x not in scope
}

This additional security has led to a large number of companies needing secure-by-default software to start implementing code in Rust. Rust was created by Mozilla for use in Firefox to improve performance and security. Google and Microsoft have both been introducing more Rust into systems such as Windows and Android.

3. Performance and Safety: Binaries and Containerization

One final benefit to Rust is our ability to deliver binaries for systems, providing a verifiable supply chain to your running program, and allowing you to choose whether you want to run our software natively on your system, or through a container. For those of you choosing containers, we are also able to deliver much smaller images, usually 100-200mb or less, compared to our Javascript images, which are much closer to 1GB in size.

What’s Next

As we release more software in Rust, we will let you know and share upgrade paths for any software that is a replacement on our blog. Additionally, we will be looking to build more functionality in WASM, so users that require in-browser functionality with performance and safety can leverage libraries that 3Box Labs produces.

We look forward to continuing to provide you with the fastest, most reliable off-chain storage for your Web3 needs.

If you'd like to tell us more about your project or get in touch with the core team please fill out this form.