Designing a SaaS that can move servers: stateless apps, object storage and safe schedulers

The rules Forkly, Mesito and Kommo follow so any copy of the app can run on any server: no files on disk, no state in memory, schedulers safe to run twice.

The short answer

Any copy of the app must be able to run on any server. So data lives in PostgreSQL, files in S3-compatible object storage, sessions and rate limits in the database rather than in memory, and every scheduled job is safe when two copies of the app run at once. Then adding a second server or moving to new hardware needs a config change, not a rewrite.

On this page
  1. Rule 1: no state on the app container’s disk
  2. Rule 2: no important state in process memory
  3. Rule 3: scheduled jobs are safe with two copies running
  4. Rule 4: migrations that both versions can live with
  5. Rule 5: config from the environment, images from CI
  6. Rule 6: every service has a health check
  7. Rule 7: fewer stateful services
  8. What does this cost on a single server?

Our products start on one server, like most young SaaS products. One server is cheap and simple. The danger is that the code quietly starts depending on that one server: a folder of uploads next to the app, sessions in memory, a timer that sends the morning emails. Each is harmless until the day you need a second server, or need to move fast because the first one failed.

So Forkly, Mesito and Kommo follow a set of rules from the start: any copy of the app must be able to run on any server. Then adding a second server and a load balancer later needs no code change.

Rule 1: no state on the app container’s disk

  • Data goes in PostgreSQL. No SQLite file next to the app.
  • Files go in object storage, through the S3 API only. Uploads, documents, generated PDFs and images all go there. We run an S3-compatible store ourselves today. Because the code only speaks the S3 API, moving to Cloudflare R2, Backblaze B2 or AWS S3 is a config change.
  • /tmp is for scratch work that may vanish between requests, nothing more.

We learned the cost of breaking this rule: one CRM we run started on SQLite with files on disk, and moving it to PostgreSQL took a rehearsal, a comparison of every page and a planned closure.

Rule 2: no important state in process memory

Anything a second server must also see lives in PostgreSQL (or Redis, if the app already has it): sessions, rate limits, login lockouts, idempotency keys. With two servers, a rate limit kept in memory is two rate limits, and a session kept in memory signs people out whenever the load balancer sends them to the other server.

Kommo keeps its rate limits in one PostgreSQL table, counted with a single atomic INSERT ... ON CONFLICT ... DO UPDATE per request and using the database clock, so every API container counts against the same limit. A test fires 50 requests at once at a limit of 10 and checks that exactly 10 get through. Forkly already runs Redis for its job queues, so its rate limits live there.

In-memory caches are fine only where losing or duplicating them just means data is briefly stale.

Rule 3: scheduled jobs are safe with two copies running

This is the rule that’s easiest to break without noticing. A bare setInterval or a cron inside the web process works perfectly on one server. On two servers, every reminder email goes out twice.

Each product solves it with a queue that knows about schedules:

  • Mesito uses pg-boss, a job queue that lives in PostgreSQL, with singleton schedules: a job scheduled every minute runs once per minute across all servers, not once per server.
  • Forkly uses BullMQ’s job schedulers: one scheduler shared by every worker, so each interval produces one job. Its notification job also locks the rows it sends with SKIP LOCKED, so even two workers running at once never send the same message twice.
  • Kommo uses pg-boss for its background work.

Where a job has no queue, the fallback is a PostgreSQL advisory lock: try to take the lock, and skip the run if another copy already holds it.

Rule 4: migrations that both versions can live with

During a deploy, the old and new versions of the app run against the same database for a short time. So every schema change is expand and contract: add the new column first, backfill it, switch the reads, and drop the old one in a later release. Never rename or drop a column in the same release that stops using it. This is also what makes our zero-downtime deploys possible.

Rule 5: config from the environment, images from CI

Configuration comes only from environment variables. Images are built in CI, tagged with the commit they came from, and pulled by the server. Nothing is built on the server. Any server can run any version by pulling a tag.

Rule 6: every service has a health check

Every HTTP service has a ready endpoint that also checks its database, and every background worker sends a heartbeat. A load balancer, a deploy script or an uptime checker can only route around a broken copy if it can tell that the copy is broken.

Rule 7: fewer stateful services

Before adding Redis, a separate file server, a search engine or a second database, we check whether PostgreSQL already covers it: pg-boss for queues, advisory locks for jobs, full-text search for search. Every extra stateful service is one more thing to back up, restore and replicate later.

What does this cost on a single server?

Very little. PostgreSQL and object storage run fine on one machine. The queue is in the database you already have. The rules mostly stop you from taking shortcuts that would be expensive to undo. If a feature seems to need local state, that’s the moment to stop and design it properly, not to quietly break a rule.

Keep reading

Tell us what you want to build.

A product, a website, a search problem or a system that should work better. We reply with a plain answer and the first step.

A few sentences are enough (at least 20 characters).

We use your details only to answer your inquiry. See our privacy policy.