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.
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:
- Fly.io replaces machines one at a time by default, so an app with one machine has no overlap. With several, set
strategy = "immediate"in the[deploy]section offly.tomlfor that deploy. It updates all the machines at once, which keeps the overlap short but doesn't rule it out. - Gigalixir starts each new replica before it stops the old one, so there's an overlap even with a single replica. Deploy migrations when few people use the app.
- Kubernetes stops the old servers first when the deployment's strategy is
Recreate.
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:
- Run
mix holo.check.migrationin CI, so a deploy doesn't fail at boot because of a model change no migration covers. - Try a migration with a big backfill on a copy of the production database first. You see how long it takes, and whether it fails on the data that's really there.
- Give the app time to start. Platforms often stop a server that doesn't pass its health check soon enough. If that happens during a migration, the database undoes that migration, and the next start runs it from the beginning. Set the limit above the time your longest migration takes.
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:
- 10 for the app - page renders, commands, users' writes and jobs share this pool.
- 4 for sync - first loads, catch-ups and lazy queries use a pool of their own.
- 1 for sync that waits for changes in the database.
- 2 for jobs - one waits for new jobs and the other marks the jobs this server is running.
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:
- Pool size - each connection of the app takes one from the pooler for as long as it's open, so the pooler needs at least as many as you counted above. With fewer, some of the app's connections wait in line.
- Idle transaction timeout - turn it off. A migration can keep a transaction open without sending anything while it works out values, and a pooler that closes the connection then fails the boot.
- TCP keepalives toward PostgreSQL - keep them on and short. When a server disappears without closing its connections, the locks of its jobs stay until PostgreSQL notices, and the jobs can't run again before that. On a direct connection Hologram sets this up itself, and it takes about a minute.
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:
- Only pages, components, actions or commands - the tab gets what changed in the data while it was away and goes on. The next page the user opens runs the new code.
- Access rules - the tab gets the new rules when it reconnects. The server sends every browser its entities again, since what a user may read could have changed.
- Additions and renames in the data model - the tab keeps syncing and doesn't see what's new until it loads the new code. Browsers that load it are sent their entities again.
- Most other changes to the data model, like a deleted attribute - the tab keeps syncing if the migration says with
old_clients_get:what it should get. Otherwise it shows a notice that asks the user to reload. - An entity type made lazy, or no longer lazy - the tab shows the notice.
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.
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:
- Pages and commands that use the database fail.
- Open pages keep showing what their browser holds. Writes made in actions show up at once and wait in the browser, which sends them when the database is back.
- Lazy queries keep the answers they have and get no new ones.
- Jobs that were running are cut short, and no new ones start until the database is back.
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.