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:
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, 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.
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.
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.
Discord’s shift to Rust was motivated by several core requirements:
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.
Discord’s decision to transition to Rust was based on a few important factors:
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.
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.
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.
Feature | Go | Rust |
---|---|---|
Memory Management | Garbage Collection | Ownership Model (No GC) |
Concurrency | Goroutines, Channels | Ownership & Borrowing (No Data Races) |
Performance | Good for I/O-bound workloads | Great for performance-heavy tasks |
Learning Curve | Easy to learn | Steeper learning curve due to ownership model |
Error Handling | Simple, but sometimes too lax | Strong emphasis on type safety and error handling |
Use Cases | Cloud services, web servers, microservices | System-level programming, performance-critical services, embedded systems |
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.
---
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