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
Component
Role
Where
PostgreSQL with pgBouncer, and Redis
The 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 workers
Answer 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 workers
Process 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 API
Holds 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 balancer
Balances 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
Minio
File storage, on one server with a 150GB volume.
Near: 1 running Minio for file storage
Figures, as the source states them
Figure
As stated
Note
Where
Web requests
3,000 requests per minute
About; at the time of writing
Near: with about 3,000 RPM and an average
Registered users on the instance
43,000 users
Recently surpassed; registrations then closed
Near: has recently surpassed 43,000 users
Open connections
6,000 connections
About; at the time of writing
Near: is servicing about 6,000 open connections
Average response time
200 milliseconds
Average
Near: an average response time of 200ms
Users served by the pre-surge setup
20,000 users
Registered; most not active at the same time
Near: before that mastodon.social was serving 20,000 users
Keep the database on one large machine and add application servers as load grows
mastodon-2017-mastodon-social-scaling#d1at 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
Pool PostgreSQL connections per transaction with pgBouncer in front of the database
mastodon-2017-mastodon-social-scaling#d2at 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.
Give job types dedicated Sidekiq processes so a busy higher-priority queue does not starve other work
mastodon-2017-mastodon-social-scaling#d3at 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
Run more Sidekiq and Puma processes with fewer threads each instead of a few processes with many threads
mastodon-2017-mastodon-social-scaling#d4at 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.