34.1. Planning a Database Service
The first hour of planning saves the most operational pain: the engine choice fixes your replication and backup vocabulary, the storage layout fixes what failure means, and the recovery objective fixes how much of everything else you must build. This section settles those three decisions for the chapter's example service - a small inventory application - before any package is installed, because every later procedure refers back to them.
34.1.1. Choosing PostgreSQL or MariaDB for an Application
Both servers are fully packaged in Debian 13, both speak SQL well enough for ordinary applications, and neither is a bad default. The choice matters for the operational shape of the service: how connections are served, how replication and failover work, how backups and recovery are done, and what the application already expects. The comparison below reflects the packages this chapter installs and exercises.
| Criterion | PostgreSQL 17.12 (postgresql-17) |
MariaDB 11.8.9 (mariadb-server) |
|---|---|---|
| SQL dialect and types | Strict, standards-oriented; rich types (arrays, ranges, jsonb with indexed operations) |
MySQL-compatible dialect; JSON stored as text with functions; strict mode is a setting, not a given |
| Storage engines | One engine; behavior is uniform and predictable | Pluggable; InnoDB is the default and the one the chapter uses, and options trade off features against layout |
| Connection model | One process per connection; cheap to spawn, costly at thousands of connections - deployments commonly add a pooler such as PgBouncer | Threads inside one server process; many connections are cheap, and thread-level limits bound them |
| Replication | Physical WAL streaming between whole clusters, plus logical row-level replication for selected tables and cross-version paths | Binary-log replication with GTIDs; domains allow multiple independent sources converging on one replica |
| Automatic failover ecosystem | Patroni (this chapter) manages clusters over an etcd lease, including promotion and rejoin via pg_rewind |
The controlled-promotion procedure in this chapter is manual; automation pairs with external orchestrators |
| Backup and recovery | pg_dump logical dumps; pg_basebackup plus archived WAL reaches any point in time |
mariadb-dump logical dumps; mariadb-backup physical copies; binary logs replay to a chosen position |
| Client compatibility | PostgreSQL wire protocol; drivers are first-class in every language | MySQL wire protocol; works with MySQL clients and tooling |
| License | PostgreSQL License (BSD-like, permissive) | GPLv2 for the server; client libraries LGPL |
Pick by workload and by what the application already speaks. A service
written against MySQL idioms - backtick identifiers, REPLACE,
AUTO_INCREMENT semantics, a MySQL driver - should run on MariaDB,
whose compatibility is the point of the project. An application free to
choose, or one that leans on advanced SQL - arrays, jsonb,
geographic types from PostGIS, transactional DDL - is better served by
PostgreSQL, and most of this chapter's
depth - including the automated failover walkthrough - is spent there.
Running both in one environment is common; the decision is per
application, not per site.
34.1.2. Data Directories, Durability, Storage Capacity, and Failure Domains
Everything a database guarantees begins with a directory it fully
controls. Debian places PostgreSQL cluster data under
/var/lib/postgresql/<major>/<cluster> and MariaDB under
/var/lib/mysql, with configuration under /etc/postgresql/... and
/etc/mysql/... respectively. Two properties of these directories
drive the rest of the plan:
- They are write-heavy and latency-sensitive. Every committed transaction must reach disk (or at least the device's durable cache) before the server acknowledges it. The volume holding the data directory should not share I/O budget with logs or build artifacts, and on any server with real load it deserves its own spindles or its own quota. Chapter 13 covered LVM and device management if you need to carve this out; Chapter 28 covered measuring the actual pressure.
- They grow by three routes at once. Live data grows with the application, dead row versions and logs grow with churn, and the write-ahead log (PostgreSQL) or binary log (MariaDB) grows with every transaction until it is archived or expired. A capacity plan that only fits the tables will be wrong; the exercises in this chapter size the WAL archive separately (a dedicated 2 GB disk on db1) for exactly this reason.
Durability is configured, not assumed. PostgreSQL enables checksums
per cluster when it is created (initdb --data-checksums, or the
equivalent bootstrap option used in section 34.6) so that on-disk
corruption is detected rather than propagated; MariaDB's InnoDB
checksums block pages by default. Both servers default to flushing
writes at commit - fsync and full_page_writes on PostgreSQL,
innodb_flush_log_at_trx_commit = 1 on MariaDB - and disabling these
for speed converts crashes into corruption. Treat any tuning guide that
turns them off as a trade of durability for benchmarks.
Failure domains turn individual disks into a story about what can be lost at once. A replica on the same physical host, SAN, rack, or power circuit as its primary protects against software failure only; a backup on the same disk as the data directory is not a backup at all. The rule this chapter follows, and which Chapter 15 stated as the copy ladder: the WAL archive goes on a different device from the data directory, the base backup goes to a different machine, and replicas belong in a different failure domain than the primary whenever the point of having them is availability. The chapter's practice lab puts everything on one hypervisor, which is convenient for learning and wrong for production - the walkthrough says so where it matters.
34.1.3. Service Accounts, Network Listeners, and Recovery Objectives
The server processes never run as root. Debian's packages run
PostgreSQL under the unprivileged postgres account and MariaDB under
mysql, each owning its data directory and nothing else; local
administration reaches them through sudo -u postgres psql or
sudo mariadb, both of which authenticate through the operating
system rather than a database password. Applications get their own
named login roles (section 34.2.2 creates app_owner and
app_reader), so a compromised application password is a database
incident, not a superuser incident.
Network exposure is a decision made twice. First the server binds only
the interfaces it needs - PostgreSQL's listen_addresses and MariaDB's
bind-address default conservatively on Debian, and this chapter sets
them explicitly to the practice addresses. Second, the authentication
file in front of the listener (pg_hba.conf for PostgreSQL, the
grant tables for MariaDB) decides which source addresses may connect
at all and on which terms. Everything remote in this chapter runs
under TLS with certificate verification in both directions, and
Chapter 18 is the backstop for the ports that
should never be reached from outside at all.
Recovery objectives come from Chapter 15 and get database-specific meanings here:
| Term | Meaning for a database service |
|---|---|
| RPO (recovery point objective) | How much acknowledged work may be lost. Asynchronous replication loses whatever had not reached the replica; synchronous replication (34.5.2) can lose nothing but stalls writes when the replica is gone; backups lose everything after the last archived WAL position. |
| RTO (recovery time objective) | How long until writes are accepted again. The recorded lab numbers - about half a minute for automatic failover on one-vCPU guests - appear in section 34.7.3; production clusters measure and tune their own. |
| Consistency | A restored or promoted server must agree with what applications were told: every acknowledged transaction present, no unacknowledged transaction appearing twice. This is checked, not assumed, in sections 34.4.5 and 34.7.3. |
Single-writer ownership is the thread running through the whole
chapter: one primary accepts writes, replicas stay read-only until a
deliberate promotion, and both the manual and automated failover
procedures end with a check that exactly one writable server exists.
Chapter 32 called the same idea
fencing; here it appears as timeline divergence, lease expiry, and
read_only.