This slides illustrates the need for replication in PostgreSQL, why do you need a replication DB topology, terminologies, replication nodes and many more.
11. Terminologies
Write Ahead Log (WAL)
Every transactions -> Transaction Log
Each statement creates a new WAL record
Each WAL record contains a bunch of records
16MB by default
pg_xlog/pg_wal
WAL Records
WAL File
16. Failover
Switching to the backup server(standby) when the primary server is not available
Terminologies
Application
S
Q
L
Primary Server Standby Server
27. Physical Vs Logical Replication
Physical Replication
Primary - at offset 11 of disk page 5 of relation 14312, add a row with hex value
0x234...
Secondaries - at offset 11 of disk page 5 of relation 14312, add a row with hex
value 0x234...
Disk Block Level changes made by PostgreSQL
An exact copy of the Primary
Example:
28. Physical Vs Logical Replication
Logical Replication
Primary - INSERT INTO test1 VALUES (now());
Secondaries - INSERT INTO test1 VALUES (2021-01-31);
Data getting in and is replicated
Example:
30. Physical Replication - Log Shipping
PostgreSQL 8.2
Replication based on the archival
archive_command on the primary
restore_command on the standby
File Based
31. Physical Replication - Log Shipping
pgxlog/
pg_wal
2920514960194996
NFS / Archive Server
Primary Server Standby Server
Archive Command
Restore Command
WAL File
32. Physical Replication - Log Shipping
No direct connection
No replication user
Replica always be in lag
No support for synchronous mode
33. Physical Replication - Streaming Replication
PostgreSQL 9.0
Stream WAL files to the standbys
primary_conninfo in recovery.conf
Block Based
34. Physical Replication - Streaming Replication
WAL Receiver - on Standbys
Startup - on Standbys
WAL Sender - on Primary
35. Physical Replication - Streaming Replication
Start Connection
primary_conninfo
Authentication
Password Response
Authentication Done
Identify System
and start to
stream WAL
System Identified and
send WAL files
Standby
WAL Receiver
Primary
WAL Sender
36. Physical Replication - Streaming Replication
Primary Server Secondary Server
WAL Records
WAL Records
WAL Records
WAL Receiver
WAL Sender
37. Physical Replication - Streaming Replication
Replication Lag can be reduced
Supports Synchronous Replication
Used for Failover
Requires direct connection
Requires DB user
38. Physical Replication - Streaming Replication / Log Shipping
Same PostgreSQL Version
Same Architecture
Entire Dataset
No writes are possible
39. Logical Replication - Publisher/Subscriber Model
table - t1 and t2 on database test
Connection Details
PostgreSQL 10
Publication - Source Server
Publisher
Subscription - Destination Server
Subsciber
41. Logical Replication - Publisher/Subscriber Model
Source/Publisher Destination/Subscriber
WAL Records WAL Sender Logical Replication
Launcher
Publication
Decoding
Logical
Messages
Subscriber
42. Logical Replication - Publisher/Subscriber Model
Granularity in replicating data
can write on the destination
Replicate across different versions
DDL is not supported
Not for failover
50. High Availability Frameworks - Patroni
Continuous Monitoring
Automatic Failover
Easy to add or remove the node
Join back the node
Support manual switchover too
Scheduling maintenance
51. Points to Note
Test the tool for auto-failover
Load Balancing
Frequent DR activity
Maintain Backups
Proper Monitoring