Fly.io + Marmot + PocketBase = Distributed PocketBase

I’ve been working on creating a developer friendly quickly deployable demo for PocketBase with Fly.io. While there are still some rough edges, I believe it creates a pretty compelling case for how one can create a distributed edge DB with SQLite and have nice a layer like PocketBase on top.

Clone this Gist

Clone or Download the copy of Gist. Once downloaded/extracted, and cd into the directory.

Install Flyctl

  • Follow the installation instructions from Install flyctl · Fly Docs.
  • Run fly auth signup to create a Fly.io account (email or GitHub).
  • Run fly auth login to login.

Deploy and Scale

  • Create Fly app using fly app create, fill in the information on prompts.
  • Deploy on app using fly deploy -a <application-name>, here application-name will be the name of app you created
  • Scale the app to multiple pods you fly scale count 2 -a <application-name>. At least have 2 pods for Marmot to
    start a cluster, otherwise Marmot process won’t come up and wait for more than 1 nodes to come up.

Create Admin

Once cluster is started go to http://<application-name>.fly.dev/_/ to launch admin panel, it will prompt you to create an
admin account. Choose your email and password. Once you hit create, it will create your admin account.

PocketBase might show you an error saying invalid token. If that happens just wait for a second or so to let
changes propagate. Try reloading http://<application-name>.fly.dev/_/ until you see login form. If issue
persists try creating account again.

Use the APIs

Now you can play with your app’s API using http://<application-name>.fly.dev/api/. Checkout
PocketBase Docs for deep dive.

6 Likes

Neat! I was attempting this with LiteFS a few weeks back.

One caveat is that distributed solutions won’t work with events, so reacting to record creation/update/deletion won’t work as soon as you add a second node. See this topic for more details.

It’d be nice to figure out how to get events working with distributed setups, even if that isn’t the primary use case for PocketBase.

2 Likes

It’s true that PocketBase events itself might not work. But Marmot builds on top of NATS which has streams of changelog being published. So one can simply listen to that changelog over websockets. You can then add leaf nodes to distribute it across the globe. It will be way more powerful than regular single core PocketBase subscriptions. I’ve documented the message format here.

1 Like