Full stack software engineer, cyclist, shitposter.

Here's how you make Mastodon a dream to host

I help run a Mastodon server called Urbanists.Social. Established back in November 2022, it's now self-sustaining and gets donations double the amount it costs to host the server monthly. But it could be even more self-sustaining if it weren't for one inconvenient fact: self-hosting Mastodon is an absolute nightmare. Unless you're paying a wonderful company like Masto.Host (Hugo runs an amazing service), you're not going to have a fun time.

Hosting Mastodon yourself has a few unique challenges:

The second and third aspects of hosting Mastodon are challenging with how Mastodon is set up currently. Servers like mastodon.social struggle to scale even under moderate pressure with the large user base they have. Smaller instances are easily overwhelmed by a sudden burst of Fediverse traffic if they don't host Mastodon exactly right. And this is due to a few main reasons:

The last time I tried to set up a proper, self-hosted Mastodon setup was with K8S. It was an absolute nightmare to try and get done. This is a graph of what I remember the moving pieces looking like for Mastodon:

Diagram of Mastodon's architecture. It shows the application server, Sidekiq, the websocket process, the mobile notification process, Redis, Postgres, S3, and a CDN.

Every single one of these boxes needs to scale horizontally and / or vertically independent of the other one, and it's very, very hard to predict which one you're going to need to scale under high load. Each of them has different techniques and tricks and things you need to know in order to scale them and maintain them right. You need to back up your Postgres server but you don't need to back up Redis. You can get rid of Redis completely but you have to rebuild users' timelines somehow using a CLI command after you do so. S3 caches remote media and there's a job that will clear old media in it, but if you have a backed up Sidekiq queue it won't run.

In addition to this, Mastodon is just a lot less resource efficient than its peers. It's written in Ruby, and while Ruby is great, it's not known for it's incredible memory-efficiency and ease of scalability. Specifically in Rails, a lot of stuff gets pushed off to background jobs because responses need to be returned quickly (similar to NodeJS being single-threaded). There's also a lot of gems and the CLI commands can take a second to execute.

So usually what you end up with is instance maintainers who don't set things up in a way that makes it easy to horizontally scale each of these pieces, because that also requires much more maintenance. And so when Fediverse traffic increases, instance admins have to do a lot more work in order to keep up.

Fixing it

Fixing this is simple: don't use Mastodon. Use something like Pleroma or one of its forks instead.

Pleroma is written in Elixir. It eliminates a large amount of the items in the previous diagram:

In the end you get a diagram that looks like this:

Diagram of Pleroma's structure. It shows an application server, Postgres, S3, and a CDN.

This eliminates more than half of the moving parts that need to horizontally scale. Instead, you only worry about two pieces: your application servers, and Postgres. Both of these can be automatically scaled and maintained. Managed Postgres DBs are incredibly cheap, and Elixir with cluster support set up properly will just scale up or down automatically as you add and remove more servers.

This is all assuming you don't have full-text search enabled on Mastodon, which a lot of servers do. Even then Pleroma just requires Postgres, as Postgres has full-text search. Sure it sucks a bit, but in my experience I've found it to be better than Mastodon's.

The only thing missing from Pleroma is some of the Mastodon API implementations. Trends and suggestions are unfortunately unimplemented. Some apps might appear broken with this unimplemented.

But anyways, I think that's the solution to fix Mastodon's scaling woes: not using Mastodon. Pleroma has amazing tools for moderation, including the Message Rewrite Facility (MRF). This allows you to write code that hides, deletes, or suspends accounts or posts immediately when they're received. It's stunningly simple.

That's all that. I'll be experimenting a little more with Pleroma (or probably Akkoma, one of it's better forks). I don't know of a migration path from Mastodon that's completely 100% seamless just yet, but we'll see.