Site icon DataFLOQ

Getting Started with PostgreSQL Streaming Replication

In this blog post, we dive into the nuts and bolts of setting up Streaming Replication (SR) in PostgreSQL. Streaming replication is the fundamental building block for achieving high availability in your PostgreSQL hosting, and is produced by running a master-slave configuration.

Read the original: Getting Started with PostgreSQL Streaming Replication

Master-Slave Terminology

Master/Primary Server

Slave/Standby Server

Data is written to the master server and propagated to the slave servers. In case there are an issue with the existing master server, one of the slave servers will take over and continue to take writes ensuring availability of the system.

WAL Shipping-Based Replication

What is WAL?

How is WAL Used For Replication?

Write-ahead log records are used to keep the data in sync between the database servers. This is achieved in two ways:

File-Based Log Shipping

Streaming WAL Records

Both the methods have their pros and cons. Using file-based shipping enables point-in-time recovery and continuous archiving, while streaming ensures the immediate data availability on the standby servers. However, you can configure PostgreSQL to use both methods at the same time and enjoy the benefits. In this blog, we concentrate mainly on streaming replication to achieve PostgreSQL high availability.

How To Set Up Streaming Replication?

Setting up streaming replication in PostgreSQL is very simple. Assuming PostgreSQL is already installed on all the servers, you can follow these steps to get started:

Configuration on Master Node

Configuration on Standby Node(s)

The standby configuration has to be done on all the standby servers. Once the configuration is done and a standby is started, it will connect to master and start streaming logs. This will setup the replication and can be verified by running the SQL statement SELECT * FROM pg_stat_replication; .

By default, streaming replication is asynchronous. If you wish to make it synchronous, then you can configure it using the following parameters:

# num_sync is the number of synchronous standbys from which transactions
# need to wait for replies.
# standby_name is same as application_name value in recovery.conf
# If all standby servers have to be considered for synchronous then set value *’
# If only specific standby servers needs to be considered, then specify them as
# comma-separated list of standby_name.
# The name of a standby server for this purpose is the application_name setting of the
# standby, as set in the primary_conninfo of the
# standby’s WAL receiver.
synchronous_standby_names = num_sync ( standby_name [, …] )’

Synchronous_commit must be set to on for synchronous replication and this is the default. PostgreSQL provides very flexible options for synchronous commit and can be configured at user/database levels. Valid values are as follows:

Setting synchronous_commit to off or local in synchronous replication mode will make it work like asynchronous, and can help you achieve better write performance. However, this will have higher risk of data loss and read delays on standby servers. If set to remote_apply, it will ensure immediate data availability at standby servers, but write performance may degrade since it should be applied on all/mentioned standby servers.

You can enable the archive mode if you’re planning to use continuous archiving and point-in-time recovery. While it’s not mandatory for streaming replication, enabling archive mode has extra benefits. If archive mode is not on, then we need to use the replication slotsfeature or ensure that wal_keep_segments value is set high enough based on load.

Refer to this excellent presentation to go into more details of high availability in PostgreSQL. In our next blog post, we’ll introduce you to the world of tools used to manage high availability for PostgreSQL using streaming replication.

Exit mobile version