GitHub Star 1.6k

Deploying

An app with a data layer deploys like any other Elixir app, as a release that runs next to a PostgreSQL database. This page covers what the data layer adds: migrations at boot, database connections, running several servers, and what happens when the database fails over, is restored or goes down. Database Setup covers connecting to the database.

Running Migrations

There's no migration step to add to a deploy. The app applies the migrations the database hasn't run yet when it boots, before it serves any request. Migrations covers how they run and what makes the app refuse to boot.

Rolling deploys with a migration

For now, old and new servers shouldn't run side by side when a deploy has a new migration or upgrades Hologram. The first new server changes the tables as it boots, and requests to servers still running the old code can then fail, for example on a renamed attribute or a new required one.

This is temporary. The next release will handle rolling deploys automatically, with nothing for you to do. Other deploys can already replace servers one at a time.

Until then, stop the old servers before the new ones start, if your platform lets you:

While no server is up, pages that are already open keep working from what their browser holds, and the writes made in their actions wait in the browser. New pages don't load until a new server is up.

To keep a deploy with a migration short and safe:

The indexes your queries need aren't part of this. The app builds them in the background once it's serving, so right after a deploy a new query on a big table can be slow for a while. Queries covers these indexes.

Rolling Back

If the deploy had no migration, you can go back to the previous release. If it had one that changed the tables, the previous release refuses to boot, since the database no longer matches its model. Migrations don't run backwards, so undo the change in the entity types, generate a new migration and deploy that. Values a migration deleted are gone, and only a backup has them.

Connection Count

PostgreSQL limits how many connections it accepts, 100 by default, and hosted databases often set their own limit by plan. With the default settings, each server keeps 17 open:

After a deploy, each server can also open one more for a while to build indexes. So count both pools plus 4 for each server, which is 18 by default. An Ecto repo's pool comes on top. Leave a few connections free for psql and other tools.

Both pools can be changed:

config :hologram, :database,
  url: System.fetch_env!("DATABASE_URL"),
  pool_size: 20

config :hologram, :sync, pool_size: 8

With these settings each server needs up to 32 connections, so three servers need 96, which leaves almost nothing of PostgreSQL's default 100. If old and new servers overlap during a deploy, count both.

Raise the app's pool when requests wait for a connection, or when many jobs query or write at once. Sync's pool matters most after a deploy, when every browser reconnects. A server lets four browsers catch up at a time whatever the pool's size, so a bigger pool mainly helps apps with many new visitors or many lazy queries.

Connection Poolers

Connect to PostgreSQL directly if you can. Hologram already pools its connections, so a pooler like PgBouncer in between adds little.

If you need one, run it in session mode. Transaction mode and statement mode don't work, because Hologram depends on things that last as long as a connection, like listening for changes and the locks that migrations and jobs hold. Hosted databases often have two addresses, one direct and one through a pooler in transaction mode, so make sure DATABASE_URL has the direct one.

In session mode, also check these settings:

Several Servers

Any number of servers can share one database. They all serve pages, sync browsers and run jobs, and there's no main server to choose. When one of them writes, the others hear about it from the database and send the change to their own browsers.

The servers do have to be connected in an Erlang cluster. A browser keeps a stream open to one server, but its other requests can reach any of them. The servers pass messages to each other to open the stream, to answer lazy queries, and for realtime broadcasts. Without a cluster, these work only when the requests happen to reach the right server.

New Phoenix apps come with dns_cluster, which finds the other servers through DNS. It's in the app's supervision tree:

children = [
  {DNSCluster, query: Application.get_env(:my_app, :dns_cluster_query) || :ignore},
  MyAppWeb.Endpoint
]

The setting comes from the DNS_CLUSTER_QUERY environment variable. Set it to a name that resolves to the addresses of all the servers. The releases also need long node names that end in the server's address, like my_app@10.0.0.5, and the same cookie. To check the result, run Node.list() in a remote console. It should list the other servers.

Keep the servers' clocks in sync with NTP, as most hosts do by default. Hologram compares times from different servers and browsers to decide which of two edits is newer.

If there's a proxy or a load balancer in front of the servers, it has to pass the stream at /hologram/sse on as it comes. Turn off response buffering for that path, like with proxy_buffering off in nginx, and don't let it close connections that are quiet for less than 30 seconds. The server sends something every 15. The proxy also has to let big requests through to /hologram/mutation. Sync and Offline has the numbers.

Open Tabs Across a Deploy

Tabs that were open before a deploy keep running the old code, and they reconnect within seconds once a new server is up. What they get depends on what the deploy changed:

Users can keep writing in old tabs in all of these cases. The server changes the writes to fit the new data model, and leaves out or refuses what it can't fit. Migrations lists what each change does to open tabs and how to use old_clients_get:, and Sync and Offline covers the notice and the writes.

A deploy that makes every browser load its entities again costs more than one that doesn't, mostly in its first minutes. With many users online, deploy such changes at a quiet time.

A deploy also cuts short the jobs that are running. They run again on a new server, and the run counts as a failed attempt. Jobs covers what that means for your code.

Backups and Failover

Everything the data layer stores is in PostgreSQL, so back up the database the way you normally would. Always back up and restore the schemas hologram_data and hologram_system together. The second one holds the migration history and the log sync works from, and neither schema is any use without the other.

To restore a backup, stop the app, restore the database and start the app again. If the backup is from before the last migration, the app applies what's missing as it boots.

After a restore, browsers can hold entities the database no longer has. Hologram notices that the database isn't the one they synced with and sends each browser everything again. Whatever was written after the backup then disappears from the browsers too. Writes that were still waiting in a browser are sent as usual.

The same happens when PostgreSQL crashes, or when a standby takes over from the main database, since either can lose the last few writes. It needs nothing from you, but expect the load of every browser downloading its entities again. A normal restart of PostgreSQL loses nothing, so browsers just catch up.

The app has to connect to the main database, not to a read replica, and it can't send its reads to a replica yet.

Don't rewrite the log

VACUUM FULL, CLUSTER and moving a table to another tablespace give the table a new file. When that table is hologram_system.oplog, Hologram can't tell it from a restore, so every browser downloads everything again. Normal VACUUM and autovacuum are fine.

Database Outages

When the database goes down while the app is running, the app stays up and reconnects once the database is back. You don't need to restart it. In the meantime:

An app that boots while the database is down waits for it and logs a warning on each try. If the connection is lost during a migration, though, the boot fails and the release has to start again. Let your platform restart the app when it exits, as most do.

Sponsored by
Curiosum
Main sponsor
Erlang Ecosystem Foundation
Milestone sponsor
Evo Store
Performance sponsor
Sruplex
Innovation partner