Favorite Social Media Timesink
When you take a break from work, where are you going?
Video clips on TikTok/YouTube
X, Bluesky, Mastodon et al...
Web surfing
I do not get distracted by petty amusements
Data / Open Source / Software Development

How Discord Scales up to Millions of Users on a Single Guild (Server)

Scale has become as important a factor as networking, storage, security and data itself.
Nov 28th, 2023 6:00am by
Featued image for: How Discord Scales up to Millions of Users on a Single Guild (Server)
Featured image via Unsplash.

IT systems aren’t only about processing data and moving it from one location to another to complete a production job. Depending upon the use case, they’re also about processing data and distributing it from one location to many locations in order to get the job done.

This is where the term “scale” steps into play. Scale has become as important a factor as networking, storage, security — and, well, the data itself — in getting things done.

Discord knows scale. The seven-year-old free communication platform that combines text, voice and video chat into a simple interface is experiencing huge growth and big-time scale-up requirements. It’s popular among gamers, but it’s also used by people from all over to connect with friends, family and communities.

Discord is a popular tool for a number of reasons: It’s free, easy to use and offers a variety of features that make it a powerful platform for communication and collaboration. They include:

  • Screen sharing: Users can share their screens with others in real time, which is useful for collaboration or troubleshooting.
  • File sharing: Users can share files with others, including images, videos and documents.
  • Bots: Users can create or add bots to their servers to automate tasks, such as moderation or music playback.
  • Integrations: Discord integrates with a number of other services, such as Twitch, YouTube and Spotify, which makes it easy to share content with others.

Pipeline for Midjourney

Discord is the largest connector of the more than 16 million users of Midjourney, the generative artificial intelligence program and service currently in beta and created/hosted by San Francisco-based independent research lab Midjourney, Inc. Midjourney’s functionality is relatively simple: A user inputs an image prompt and receives four images in return. It could be an image of anything: a superhero, landscape, fruit, humans or any number of other items or concepts.

Once a user has generated an image with Midjourney, they can mint it as an NFT (nonfungible token) on any NFT marketplace, such as OpenSea or Rarible. NFTs are a type of cryptocurrency asset linked to a unique digital object, such as an image, video or audio file. NFTs can be used to prove ownership of a digital artwork and to sell it to other people.

Midjourney is similar to other AI art generators, such as DALL-E 2, Bing Image Creator, Dream by WOMBO, NightCafe and OpenArt. But Midjourney has a few unique features, the most notable of which is its ability to generate images in a variety of artistic styles. Because it’s also able to generate images based on multiple prompts, users can create images that are more complex and detailed than what is possible with other AI art generators.

Mark Smith, VP of Core Technology at Discord, told The New Stack that Discord has an open API that allows developers to create their own apps, like Midjourney.

The Midjourney community provides much of the traffic on Discord. Members — often more than 1 million in the same window of time — share their artwork, ask questions and help each other to learn how to use Midjourney. A few other online communities have begun serving Midjourney users, including Reddit and Facebook.

Discord is clearly the growth backbone of this burgeoning community. But it also gets lots of help from some remarkable development tools.

“Where Midjourney started was that they had a website, but Discord was the primary vehicle for people to actually engage with the generative AI experience, and in particular, the social aspect of it,” Smith told The New Stack. “Being able to get together with your friends, generate some AI art, hang out, have fun and then share it back and forth — that was, and still is the goal. Millions of people at any one time are using it.”

How Developers Manage All These Transactions

Discord, available on desktop, web and mobile devices, allows users to create and join servers, which are essentially chat rooms dedicated to specific topics or interests. Servers can be public or private, and they can have different levels of access and moderation. Within servers — also called guilds — users can create channels, which are like sub-rooms dedicated to specific topics or activities. Channels can be text-, voice- or video-based.

It’s a complicated development problem to keep all these channels up and running, separate and secure — not to mention the multiplication of new users and channels by the minute. But it’s only a problem for developers if they don’t know what tools to use to manage the deployment of millions of users — on a single server, yet.

“You need two main pieces in order to do this successfully,” Smith said. “The first is you really have to understand the performance of your system, and what actions your system can take. How much does it consume in terms of resources? The second thing that is really important for doing this well is understanding the product experience that you’re trying to deliver.”

Getting to the Heart of the Development Process

Discord does this by taking full advantage of Elixir, a dynamic, functional programming language that runs on the Erlang Virtual Machine (EVM, commonly called BEAM). Elixir is designed for building scalable and maintainable applications, and is used in a variety of domains, including web development, embedded systems, machine learning, data pipelines and multimedia processing.

What’s old can be new again, Smith said. “Erlang was developed in the 1960s by Sony Ericsson for telecommunication; Elixir is a modern language built on top of Erlang,” he said.

BEAM uses a process-based architecture in which each process runs in its own isolated memory space. BEAM can throttle requests that require slower execution to handle a large number of concurrent requests without blocking or slowing down any of them; this is the secret sauce for handling all those queries.

Some of the key features of Elixir include:

  • Concurrency: Elixir is built on the EVM, known for its ability to handle concurrency efficiently. This makes Elixir a good choice for applications that need to process a large number of requests simultaneously.
  • Scalability: Elixir applications are easy to scale horizontally, meaning that they can be scaled out to multiple servers to handle increased traffic.
  • Maintainability: Elixir code is generally considered to be easy to read and maintain. This is due to its functional programming paradigm, which emphasizes immutable data and pure functions.
  • Tooling: Elixir has a rich ecosystem of tools and libraries, which makes it easy to develop and deploy Elixir applications.

“Whenever something happens on Discord, such as a message being sent, or someone joining a voice channel, we update the UI in the client of everyone who’s online that is in the same server (sometimes referred to as a ‘guild’),” Yuliy Pisetsky, a Discord staff software engineer, wrote in a recent blog post. “We use a single Elixir process per guild as a central routing point for everything happening on that server, and another process (a ‘session’) for each connected user’s client.

“The guild process keeps track of the sessions for users who are members of that guild and is responsible for fanning out actions to those sessions. Once the sessions receive those updates, they’ll forward them over a WebSocket connection to the client.”

These processes, while complicated, are simplified with automation provided by Elixir, and Discord has mastered the use of this scale-out tool.

Some popular Elixir frameworks and libraries include:

  • Phoenix: A web development framework.
  • Nerves: A framework for developing embedded systems.
  • Ecto: A database library.
  • ExUnit: A testing framework.
  • GenServer: A library for developing concurrent and distributed systems.
Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.