# How to configure Swarmica for high availability
<h2>Symptoms</h2>
<p>You need to deploy all systems with high availability and are considering whether to:</p>
<ul><li>run the Swarmica database on a separate server;</li><li>use a highly available database cluster;</li><li>run Swarmica as an active-active cluster.</li></ul>

<h2>Solution</h2>
<h3>Recommended architecture</h3>
<p>For most on-premises deployments, the best balance of reliability, performance, operational simplicity and recovery objectives (RTO and RPO) is to run Swarmica in one virtual machine while placing its virtual disk on replicated, highly available network storage.</p>
<img height="279" width="493" src="https://support.swarmica.com/attachments/t/L/tL6KrkfKOzFAm7MA_a/swarmica-ha-recommended-en.png" />
<p>This approach minimizes the number of moving parts and potential failure points while allowing the virtualization and storage platform to provide failover and replication.</p>

<h3>Separate database server with replication</h3>
<p><strong>Important:</strong> This architecture is not recommended or officially supported. It is not regularly tested by the Swarmica team. If the primary database server fails, the replica does not become primary automatically; an administrator must promote it manually. A network connection between Swarmica and PostgreSQL can also reduce performance and introduces an additional failure point.</p>
<img height="261" width="462" src="https://support.swarmica.com/attachments/F/F/FFLsO4Y09OiLiawQ_a/swarmica-external-db-en.png" />
<p>This topology requires three Ubuntu 24 servers:</p>
<ul><li><strong>Swarmica</strong></li><li><strong>PostgreSQL-1</strong> — primary</li><li><strong>PostgreSQL-2</strong> — replica</li></ul>
<ol>
<li>Install Swarmica by following the <a href="https://support.swarmica.com/article/en/281-swarmica-installation-guide.html">Swarmica installation guide</a>.</li>
<li>Create a database backup as described in <a href="https://support.swarmica.com/article/en/408-backup-and-restore-of-swarmica-on-your-own-equipment.html">Backup and restore of Swarmica on your own equipment</a>.</li>
<li>Stop Swarmica and back up the Compose file:
<pre><code>cd /root/swarmica
docker compose down
cp /root/swarmica/docker-compose.yml{,.orig}</code></pre></li>
<li>Comment out the built-in <code>postgres</code> service and its <code>depends_on</code> entries in <code>/root/swarmica/docker-compose.yml</code>.</li>
<li>In <code>/root/swarmica/.env</code>, replace <code>POSTGRES_HOST=postgres</code> with <code>POSTGRES_HOST=&lt;IP_ADDRESS&gt;</code>, then start Swarmica:
<pre><code>docker compose pull &amp;&amp; docker compose up -d</code></pre></li>
<li>On both PostgreSQL servers, install PostgreSQL 14:
<pre><code>sudo apt install curl ca-certificates -y
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
sudo sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" &gt; /etc/apt/sources.list.d/pgdg.list'
sudo apt update
sudo apt install postgresql-14 postgresql-contrib-14 -y</code></pre></li>
<li>Create the Swarmica database and user on both servers. Use the database password from the Swarmica <code>.env</code> file:
<pre><code>su - postgres
psql
CREATE USER swarmica_user WITH PASSWORD '&lt;PASSWORD&gt;';
CREATE DATABASE swarmica_app OWNER swarmica_user;
GRANT ALL PRIVILEGES ON DATABASE swarmica_app TO swarmica_user;
quit
exit</code></pre></li>
<li>On PostgreSQL-1, restore the backup:
<pre><code>pg_restore --if-exists --clean -U swarmica_user -d swarmica_app &lt;PATH-TO-BACKUP.sql&gt;</code></pre></li>
<li>On PostgreSQL-1, configure external access and replication in <code>/etc/postgresql/14/main/postgresql.conf</code>:
<pre><code>listen_addresses = '*'
wal_level = replica
max_wal_senders = 10
max_replication_slots = 5
hot_standby = on</code></pre></li>
<li>Add the Swarmica and replica access rules to <code>/etc/postgresql/14/main/pg_hba.conf</code>:
<pre><code>host    swarmica_app    swarmica_user    &lt;IP&gt;/&lt;MASK&gt;                 md5
host    replication     repuser          &lt;REPLICA_IP&gt;/&lt;REPLICA_MASK&gt; md5</code></pre></li>
<li>Create a replication user and restart PostgreSQL:
<pre><code>sudo -u postgres psql -c "CREATE ROLE repuser REPLICATION LOGIN PASSWORD 'your_secure_password';"
systemctl restart postgresql@14-main</code></pre></li>
<li>On PostgreSQL-2, stop PostgreSQL, clear its data directory, and clone the primary:
<pre><code>systemctl stop postgresql@14-main
sudo rm -rf /var/lib/postgresql/14/main/*
sudo -u postgres pg_basebackup -h &lt;PRIMARY_IP&gt; -U repuser -D /var/lib/postgresql/14/main/ -P -R -v
systemctl start postgresql@14-main</code></pre></li>
</ol>

<h3>Separate database server with high availability</h3>
<p><strong>Important:</strong> This architecture is also not recommended or officially supported. It is not regularly tested by the Swarmica team. It adds network latency, increases operational complexity and introduces additional failure points. Use it only when your infrastructure requirements justify the extra complexity and you have the expertise to operate and test the resulting stack.</p>
<img height="335" width="593" src="https://support.swarmica.com/attachments/h/6/h62g3PHwzC-8HPf__a/swarmica-external-db-ha-en.png" />
<p>The example topology uses four Ubuntu 24 servers:</p>
<ul><li><strong>swarmica-master-1</strong></li><li><strong>swarmica-postgres-1</strong></li><li><strong>swarmica-postgres-2</strong></li><li><strong>swarmica-pgbouncer-1</strong></li></ul>
<h4>swarmica-master-1</h4>
<p>Follow steps 1–6 from the previous section. In the <code>.env</code> file, use the PgBouncer address and port:</p>
<pre><code>POSTGRES_HOST=&lt;PGBOUNCER_IP&gt;
POSTGRES_PORT=6432</code></pre>

<h4>swarmica-pgbouncer-1</h4>
<p>PgBouncer and HAProxy provide a stable connection endpoint and route traffic to the PostgreSQL node currently acting as primary.</p>
<ol>
<li>Install Docker:
<pre><code>apt install docker.io docker-compose-v2</code></pre></li>
<li>Create the working directory:
<pre><code>mkdir /root/swarmica-pgbouncer
cd /root/swarmica-pgbouncer</code></pre></li>
<li>Create <code>docker-compose.yml</code> with HAProxy and PgBouncer. Expose HAProxy on port 5433 and PgBouncer on port 6432.</li>
<li>Configure HAProxy health checks against the Patroni <code>/master</code> endpoint and list both PostgreSQL servers as backends.</li>
<li>Configure PgBouncer to connect to HAProxy on port 5433, using transaction pooling and the Swarmica database credentials.</li>
<li>Start the services:
<pre><code>docker compose pull &amp;&amp; docker compose up -d</code></pre></li>
</ol>

<h4>swarmica-postgres-1 and swarmica-postgres-2</h4>
<p>Install PostgreSQL 14, etcd and Patroni on both servers. etcd stores cluster state and Patroni performs PostgreSQL failover: if the primary becomes unavailable, the replica is promoted and the recovered node returns as a replica.</p>
<ol>
<li>Install PostgreSQL 14 and prepare the database on the first node. Do not restore the backup on the second node.</li>
<li>Stop and disable the system PostgreSQL service on both servers because Patroni will manage PostgreSQL:
<pre><code>systemctl stop postgresql@14-main &amp;&amp; systemctl disable postgresql@14-main</code></pre></li>
<li>Install etcd:
<pre><code>apt install etcd-server etcd-client</code></pre></li>
<li>Configure a two-member etcd cluster with the private IP addresses of both PostgreSQL servers, then start it:
<pre><code>systemctl daemon-reload
systemctl enable etcd --now
etcdctl member list</code></pre></li>
<li>Install Patroni and its dependencies:
<pre><code>apt install python3 python3-pip python3-dev pipx -y
su - postgres
pipx ensurepath
source ~/.bashrc
pipx install patroni[psycopg2-binary,etcd]
exit</code></pre></li>
<li>Create <code>/etc/systemd/system/patroni.service</code> and <code>/etc/patroni/patroni.yml</code> on both servers. Configure the node name, REST API address, etcd peers, PostgreSQL address, replication credentials and the appropriate network and subnet mask.</li>
<li>Start Patroni on the first node, wait 5–10 seconds, then start it on the second:
<pre><code>systemctl daemon-reload
systemctl enable patroni --now</code></pre></li>
<li>Verify the cluster:
<pre><code>sudo -u postgres -i patronictl -c /etc/patroni/patroni.yml list</code></pre>
The output should show one <code>Leader</code> in <code>running</code> state and one <code>Replica</code> in <code>streaming</code> state.</li>
</ol>

<h3>Active-active Swarmica cluster</h3>
<p><strong>Active-active Swarmica deployments are not supported.</strong> If you need high availability, use the recommended single-VM architecture with replicated storage, or discuss your requirements with the Swarmica team before designing a custom deployment.</p>