Features 02

Go vs Rust: Why Discord Switched Gears

A deep dive into performance, concurrency, and memory safety.

Written by ShyankApril 2025
Shyank
img

SHARE

In the world of modern software development, choosing the right programming language for your infrastructure can make or break your application. When Discord, a leading voice and text communication platform, decided to shift from Go to Rust, the tech community took notice. This change wasn’t just about convenience; it was a move driven by the need for better performance, improved concurrency, and memory safety — key factors that are becoming more critical in high-traffic, real-time applications.

In this blog, we will break down why Discord made the switch from Go to Rust, diving into:

  • Performance improvements
  • Concurrency benefits
  • Memory safety guarantees
  • The pros and cons of Go vs Rust
  • Real-world use cases for both languages

By the end of this post, you'll have a deeper understanding of the trade-offs between Go and Rust and why Discord chose to prioritize Rust's safety and efficiency in its quest for scalability.

🚀 Go vs Rust: The Basics

🛠️ What is Go?

Go, often referred to as Golang, was designed by Google to be simple, efficient, and scalable. It is a statistically typed language known for its fast compilation, concurrency support, and simplicity in building microservices.

Key Features of Go:

  • Garbage collection for automatic memory management
  • Strong support for concurrency via goroutines
  • High performance for networking and web-based apps
  • Simple syntax with quick learning curve
  • Designed for cloud-native apps, often used in Kubernetes and microservices

🦀 What is Rust?

Rust, designed by Mozilla, prioritizes performance and memory safety. It is a systems programming language that ensures no runtime errors through its unique ownership model, preventing issues like null pointer dereferencing, race conditions, and memory leaks.

Key Features of Rust:

  • Zero-cost abstractions: Rust ensures performance without sacrificing abstraction.
  • Memory safety without garbage collection via its ownership model.
  • Concurrency without data races, thanks to strict compile-time checks.
  • Ideal for systems programming, including embedded systems, WebAssembly, and high-performance applications.

💡 Discord’s Journey: Why Switch from Go to Rust?

When Discord first built its real-time messaging and voice chat service, Go was a natural choice. Its lightweight nature and superb concurrency handling through goroutines allowed them to quickly scale their microservices. However, as Discord grew, the demands on their system increased. Issues related to performance and memory efficiency started to become more prominent, prompting the need for a language that could offer more control over the system’s resources.

Why Rust?

Discord’s shift to Rust was motivated by several core requirements:

  • Performance: As Discord's scale increased, they found that Go's garbage collection could sometimes lead to unpredictable latency. This was especially problematic for real-time voice and video communication, where low-latency performance is crucial.
  • Concurrency: Go’s goroutines made concurrency easier to implement, but managing a huge number of goroutines could result in performance bottlenecks, especially when scaling their infrastructure.
  • Memory Safety: The garbage collector in Go adds overhead, and the language doesn’t have as strict a memory model as Rust. This made it difficult to meet their performance benchmarks.

🛠️ Discord’s Experience with Go

Discord initially implemented its Read States service using Go. This service tracks which channels and messages a user has read and is crucial for the responsiveness of the app. However, as the service scaled to handle more users, they began to encounter performance issues with the Go implementation. These issues arose because Go’s garbage collector would periodically trigger, which would cause latency spikes that disrupted real-time performance.

To address these latency spikes, Discord attempted to tweak the garbage collection settings and optimize memory allocation. However, these efforts were only partially successful, as the latency spikes still occurred intermittently. The cost of optimizing the memory allocations resulted in an undesirable performance hit when dealing with cache misses.

🦀 The Move to Rust

Discord’s decision to transition to Rust was based on a few important factors:

  • Memory management without garbage collection: Unlike Go, Rust doesn’t rely on garbage collection. Rust’s ownership model ensures memory safety and eliminates the need for a garbage collector, which allows for more predictable performance in high-load situations.
  • Performance improvements: Discord hoped to achieve better performance and lower latency by re-writing the service in Rust. Rust’s zero-cost abstractions ensure that performance isn’t compromised when using higher-level constructs, unlike Go, which requires manual memory management adjustments.
  • Concurrency model: Rust’s concurrency model, through its ownership and borrowing system, avoids data races and other concurrency issues that Go’s goroutines can sometimes cause.

In particular, the switch to Rust led to significant performance gains. The new Rust implementation showed reduced CPU usage and much lower latency compared to the Go implementation. Discord was able to improve the performance of its Read States service with more predictable memory management and reduced operational overhead.

The Rewriting Process

Rewriting the entire service in Rust wasn’t trivial. Discord had to carefully port the functionality, paying special attention to the specifics of memory safety and data handling in Rust. They took advantage of Rust’s type system to create a more robust and safe implementation, minimizing runtime errors.

Some optimizations, like using BTreeMap instead of HashMap for the LRU cache, led to better memory efficiency and allowed for more predictable performance. These improvements not only solved the latency issue but also resulted in more efficient CPU usage.

Rust’s Growing Role at Discord

Discord’s decision to adopt Rust went beyond the Read States service. Over time, Rust was gradually integrated into other parts of Discord’s infrastructure, including the game SDK and video encoding pipeline. The company found that Rust was particularly suited for performance-critical systems and services, and they began using it for various internal tools as well.

The success of the initial transition and the improvements observed in performance have led Discord to continue exploring Rust for other mission-critical components of their platform.

📊 Go vs Rust: A Comparison

FeatureGoRust
Memory ManagementGarbage CollectionOwnership Model (No GC)
ConcurrencyGoroutines, ChannelsOwnership & Borrowing (No Data Races)
PerformanceGood for I/O-bound workloadsGreat for performance-heavy tasks
Learning CurveEasy to learnSteeper learning curve due to ownership model
Error HandlingSimple, but sometimes too laxStrong emphasis on type safety and error handling
Use CasesCloud services, web servers, microservicesSystem-level programming, performance-critical services, embedded systems

🧠 Conclusion

Discord’s shift from Go to Rust illustrates the importance of choosing the right tool for the job. While Go’s simplicity and concurrency model made it an excellent choice for Discord initially, as their platform grew, they needed more predictable performance, fine-grained control over memory, and guarantees of safety. Rust offered these advantages, and Discord's successful transition highlights the growing use of Rust in real-time applications.

This transition also underscores the need to constantly evaluate the languages and tools we use in production. While Go is an excellent choice for many modern applications, Rust is proving to be a compelling alternative for performance-critical services, especially where concurrency and memory safety are paramount.

By choosing Rust, Discord not only solved their existing issues but also future-proofed their infrastructure as they continue to scale their platform. The growing adoption of Rust within Discord’s ecosystem is a testament to the language’s robust performance and developer-friendly features.

By adopting Rust, Discord has managed to enhance performance while ensuring memory safety, all while handling the complexities of high-scale, real-time communications.

About Me

---

Testimonial 01
Shyank Akshar

Hi! I'm Shyank, a full-stack Software developer and a call-of-duty enthusiast, I help businesses grow their company with the the help of technology, improve their work, save time, and serve their customers well, I have worked with many global startups and Govt bodies to develop some of the most secure and scaled apps