Catalogue / mastodon-2017-mastodon-social-scaling

mastodon.social serves over 43,000 users from 2 larger and 6 smaller bare-metal servers, with one database machine and split Sidekiq queues

Mastodon (the mastodon.social instance), 2017. Mastodon.social instance infrastructure.

Organisation
Mastodon (the mastodon.social instance)
Evidence
Reported from production
System
mastodon.social instance infrastructure
Setup
One database

Near: 1 running PostgreSQL (with pgBouncer on top)

As of
2017-04
Source
Scaling Mastodon

Mastodon blog, published 2017-04-12

Architecture

mastodon.social ran on 2 larger and 6 smaller bare-metal servers: one with PostgreSQL behind pgBouncer plus Redis, several running Puma web workers, Sidekiq background workers and the streaming API, one of which also ran the Nginx load balancer, and one running Minio for files. User activity becomes Sidekiq jobs that distribute posts into local followers' timelines and deliver them to other servers.

Near: The current mastodon.social infrastructure looks like this | Near: all generates background jobs that must be processed | Near: responsible for distribution of toots into local

Components

ComponentRoleWhere
PostgreSQL with pgBouncer, and RedisThe database, on one 8-core, 16GB machine shared with Redis. pgBouncer pools connections per transaction so that web workers, background workers and the streaming API do not each hold a real connection while idle.Near: 1 running PostgreSQL (with pgBouncer on top) | Near: provides pooling based on database transactions
Puma web workersAnswer users' HTTP requests; each worker handles a fixed number of requests at once, and a request that waits too long for a free thread times out.Near: Every Puma worker (WEB_CONCURRENCY) can answer MAX_THREADS
Sidekiq workersProcess the background jobs that all site activity generates, from four queues: default (posts into local followers' timelines), push (delivery to and from other servers), pull (downloading conversations, avatars and profile data) and mailers.Near: In Mastodon, these queues are | Near: all generates background jobs that must be processed
Streaming APIHolds a constantly open connection per connected client, passing through nginx, which needs high open-file and connection limits for it.Near: Being connected to the streaming API means
Nginx load balancerBalances web requests across the application servers and carries the streaming connections.Near: 1 running Nginx load balancer | Near: Being connected to the streaming API means
MinioFile storage, on one server with a 150GB volume.Near: 1 running Minio for file storage

Figures, as the source states them

FigureAs statedNoteWhere
Web requests3,000 requests per minuteAbout; at the time of writingNear: with about 3,000 RPM and an average
Registered users on the instance43,000 usersRecently surpassed; registrations then closedNear: has recently surpassed 43,000 users
Open connections6,000 connectionsAbout; at the time of writingNear: is servicing about 6,000 open connections
Average response time200 millisecondsAverageNear: an average response time of 200ms
Users served by the pre-surge setup20,000 usersRegistered; most not active at the same timeNear: before that mastodon.social was serving 20,000 users
Larger servers2 bare-metal servers with 8 cores and 16GB RAMNear: 2x baremetal C2M (8 cores,16GB RAM) servers
Smaller servers6 bare-metal servers with 4 cores and 8GB RAMNear: 6x baremetal C2S (4 cores, 8GB RAM) servers

Records

Decision#d1

Keep the database on one large machine and add application servers as load grows

mastodon-2017-mastodon-social-scaling#d1 at corpus version 26a1b3a7e3dd
Chosen

The database is scaled vertically, on one large machine, while the application code is scaled horizontally across as many machines as needed behind a load balancer. After a surge of attention the instance grew to 2 larger and 6 smaller bare-metal servers, of which one holds PostgreSQL and Redis.

Near: Databases scale vertically | Near: The current mastodon.social infrastructure looks like this

Reasoning

The author says buying a very powerful machine for the database is much easier and more cost efficient than spreading it over several machines with sharding or replication.

Near: it’s a lot easier and more cost efficient

What worked before

Before the surge of attention the instance served 20,000 users, most of them not active at the same time, with only the database server, 2 app servers and 1 Minio server.

Near: before that mastodon.social was serving 20,000 users

Decision#d2

Pool PostgreSQL connections per transaction with pgBouncer in front of the database

mastodon-2017-mastodon-social-scaling#d2 at corpus version 26a1b3a7e3dd
Chosen

On mastodon.social PostgreSQL runs with pgBouncer on top. The author describes pgBouncer as pooling by transaction instead of by session, and tells operators to connect to it in place of PostgreSQL and turn prepared statements off through an environment variable.

Near: 1 running PostgreSQL (with pgBouncer on top) | Near: provides pooling based on database transactions | Near: set the environment variable PREPARED_STATEMENTS=false

Reasoning

Each horizontal scaling step adds database connections, because web workers, background workers and the streaming API each keep a pool for their threads. The author says this easily reaches 200 connections overall, the recommended max_connections for a PostgreSQL server with 16GB of RAM, and that at that point pgBouncer is needed so a real connection is not held by a thread doing nothing with it.

Near: This can go up to 200 connections overall | Near: a real database connection is not needlessly occupied

Reviewer's notes

The post states the 200-connection point as advice to other instance operators; it does not say when mastodon.social itself crossed it.

The reviewer's reading, not the source's claim

Decision#d3

Give job types dedicated Sidekiq processes so a busy higher-priority queue does not starve other work

mastodon-2017-mastodon-social-scaling#d3 at corpus version 26a1b3a7e3dd
Chosen

Queues are split between different Sidekiq processes on different machines: for example a couple responsible only for the default queue, which distributes posts into local followers' timelines, and a couple only for push, pull and so on.

Near: I found it useful to split queues between | Near: responsible for distribution of toots into local

Reasoning

A single Sidekiq process given an ordered list of queues checks the first, then the next. In the author's example, with 100 jobs in the default queue, 100 in push and only 25 threads, there is a huge delay before any push job is reached. He ranks the default queue most important because it directly affects users on the instance, and notes that a backlog becomes noticeable when a post reaches followers only 1 hour later.

Near: if you suddenly have 100 jobs in the | Near: The default queue is the most important | Near: reaches your followers only 1 hour later

Decision#d4

Run more Sidekiq and Puma processes with fewer threads each instead of a few processes with many threads

mastodon-2017-mastodon-social-scaling#d4 at corpus version 26a1b3a7e3dd
Chosen

Several independent Sidekiq processes with lower concurrency are run in place of one process with high concurrency, and likewise more Puma workers with fewer threads. One machine runs 4 Sidekiq processes of between 10 and 25 threads each; the Puma servers run 8 workers of 2 threads each.

Near: spin up a couple independent Sidekiq processes | Near: 1 running 4x Sidekiq processes between 10–25 | Near: 2 running Puma (8x workers, 2x threads each)

Reasoning

The author's explanation is that MRI Ruby has no native threads, so threads cannot truly run in parallel however many CPUs there are.

Near: MRI Ruby does not have native threads

Cost

Separate processes do not share memory the way threads do, so more processes use more RAM.

Near: more separate processes consumes more RAM

Review

Extracted by
Claude (feed 28)
Human review
Not started. Status recorded on 2026-12-02.