Replication
===========

At the heart of EDB Postgres Distributed (PGD) is the replication
system, BDR. BDR stands for Bi-Directional Replication, and it is a
multi-master replication system that allows you to create a distributed
Postgres cluster with multiple write nodes. This means that you can
write to any node in the cluster, and the changes will be replicated to
all other nodes in the cluster.

Just because you can write to any node in the cluster, it doesn’t mean
that you should. In most cases, you will want to write to a single node
in the cluster, which is known as the write leader node. This is the
node that is responsible for coordinating the replication of changes to
all other nodes in the cluster. In fact, in PGD Essential, you can only
write to the write leader node, and all other nodes in the cluster are
read-only.

There are though some cases where you may want to write to multiple
nodes in the cluster, such as when you are using a geo-distributed
cluster with multiple write nodes in different locations. In these
cases, you can use the BDR replication system to replicate changes
between the write nodes. This, and other scenarios, are what PGD
Expanded is designed for, and it activates additional features and
functionality to support these use cases.

How Replication Works
---------------------

PGD uses logical replication to replicate changes between nodes in the
cluster. This means that changes are replicated at the logical level,
rather than at the physical level. This allows for more flexibility in
how changes are replicated, and it also allows for more efficient
replication of changes.

When a change is made to a table in the cluster, it is first written to
the write leader node’s write-ahead log (WAL). The WAL is a log of all
changes made to the database, and it is used to ensure durability and
consistency of the database. Once the change is written to the WAL, it
is then replicated to all other nodes in the cluster.

The replication process is asynchronous by default, which means that
changes are not immediately replicated to all nodes in the cluster.
Instead, changes are sent to the other nodes in the cluster in batches,
which allows for more efficient replication and reduces the load on the
network.

Once the changes are received by the other nodes in the cluster, they
are applied to the local copy of the database. This process is known as
replaying the WAL, and it ensures that all nodes in the cluster have a
consistent view of the data.

Commit scopes and replication
-----------------------------

Asynchronous replication is the default mode of replication, but not the
only one. PGD allows for definable replication configuration through
what are called commit scopes. A commit scope can be applied to a
transaction or to all transactions in a group, and it defines how
changes are replicated to other nodes in the cluster. This allows you to
control how the replication process works, and it can be used to
optimize performance, ensure that changes are replicated in a specific
way or to handle adverse network and server conditions gracefully.

- PGD Expanded has fully definable commit scopes, which allow you to
  create custom replication configurations for your cluster. Read about
  the :ref:`commit scopes in PGD Expanded <Commit Scopes>`  for full details.

- PGD Essential has has four pre-defined commit scopes that you can use
  to control how changes are replicated. Read about the :ref:`commit scopes in PGD Essential <Durability in PGD Essential>` 
  for full details.

What is replicated?
-------------------

In PGD, the following types of changes are replicated:

- **Data changes**: Inserts, updates, and deletes to tables are
  replicated to all nodes in the cluster. This is called DML (Data
  Manipulation Language) replication.

- **Schema changes**: Changes to the structure of the database, such as
  creating or dropping tables, are also replicated to all nodes in the
  cluster. This is called DDL (Data Definition Language) replication.
  But not all DDL changes are replicated. For example, adding a column
  to a table is replicated, but dropping a column is not.

- **Configuration changes**: Changes to the configuration of the
  database, such as changing the replication settings, are also
  replicated to all nodes in the cluster.

Currently, PGD only replicates one Postgres database per cluster. This
means that if you have multiple databases in your Postgres instance,
only the database that is configured for replication will be replicated
to the other nodes in the cluster. This is the same for both PGD
Essential and PGD Expanded.
