Case study 1 of 5

ZeroTwo: 22 GB to under 100 MB

Memory use fell from 22 GB to under 100 MB — more than 99% less — with no loss of features for 190,000 servers.

Joyn, Backend Engineer.

A team of 2–3 engineers.

About 190,000 servers.

Skills: Backend & APIs, Data & Messaging.

22 GB
<100 MB
Figure 1. Memory before and after the rewrite on one linear scale, with the after-bar drawn at 100 MB, the upper bound.

Problem

#

Joyn acquired ZeroTwo, a Discord bot serving about 190,000 servers. Its gateway-heavy design held a huge in-memory footprint of 22 GB.

Approach

#

Rewrote it onto a webhook-first architecture. Commands moved off the Gateway to Discord’s HTTP interactions: each command arrives as an HTTP request and is answered over HTTP, so commands need no permanent Gateway connection.

A RabbitMQ-backed microservice bridges the webhook services and Discord Gateway events, so the bot kept full event coverage.

Figure 2. Before, dashed: the bot held Discord’s Gateway connections and 22 GB in memory; after, solid: commands arrive as HTTP interactions and a RabbitMQ bridge carries the Gateway events the bot still needs.
$ diff -u before/zerotwo after/zerotwo--- before/zerotwo   gateway-heavy, about 190,000 servers+++ after/zerotwo    webhook-first-memory    22 GB+memory    <100 MB-commands  over the Gateway+commands  HTTP interactions, answered over HTTP+events    RabbitMQ bridge carries the Gateway events the bot still needs