Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Choosing From the Many Flavors of PostgreSQL Re...

Richard Yen
September 20, 2019

Choosing From the Many Flavors of PostgreSQL Replication

One of the most important tasks of a DBA or sysadmin is to make sure there is a copy of a database's contents available, either in case of a catastrophic failure or to ensure scalability of the entire application stack. Within the PostgreSQL community, there are several options to choose from for replication, and making the choice isn't always trivial. Several factors need to be considered, ranging from how much of the database needs to be replicated, to how much of a performance hit the entire system can afford. This presentation will walk through some of the most reliable replication engines developed over the years, including PostgreSQL's very own built-in Streaming Replication, and discuss the pros and cons among the major players.

Richard Yen

September 20, 2019
Tweet

More Decks by Richard Yen

Other Decks in Technology

Transcript

  1. Why Replication? • Because one copy is not enough! •

    Fault-Tolerance • Scalability • Recoverability
  2. Why Replication? • Because one copy is not enough! •

    Fault-Tolerance • Scalability • Recoverability • People want “real” data to work with
  3. Which one is for me? • Streaming replication • Logical

    replication • Slony • Bucardo • Londiste • DRBD • Corosync • 2ndQ BDR • pg_shard • pglogical • Daffodil • Mammoth • Pgpool • EDB Replication Server • Spectral Core Replicator Pro • And More!
  4. Trigger-based Replication • Relies on PostgreSQL’s trigger mechanism to log

    all DML into event/ shadow tables • Data from shadow tables ingested by another program and replayed to other nodes
  5. Trigger-based Replication • Relies on PostgreSQL’s trigger mechanism to log

    all DML into event/ shadow tables • Data from shadow tables ingested by another program and replayed to other nodes • Very reliable: replicating production environments since 2003
  6. Trigger-based Replication • Main players • Slony — written in

    C • Londiste — written in Python • Bucardo — written in Perl
  7. Trigger-based Pros & Cons • Select which tables to replicate

    • Enables row-based/column-based replication
  8. Trigger-based Pros & Cons • Select which tables to replicate

    • Enables row-based/column-based replication • Major-version upgrades
  9. Trigger-based Pros & Cons • Select which tables to replicate

    • Enables row-based/column-based replication • Major-version upgrades • Master-master replication (Bucardo only)
  10. Trigger-based Pros & Cons • Primary keys required • Limited

    ability to replicate DDL • Large object replication not available
  11. Trigger-based Pros & Cons • Primary keys required • Limited

    ability to replicate DDL • Large object replication not available • Trigger activity generates additional I/O
  12. Trigger-based Pros & Cons • Primary keys required • Limited

    ability to replicate DDL • Large object replication not available • Trigger activity generates additional I/O • Event tracking consumes network bandwidth
  13. Trigger-based Pros & Cons • Primary keys required • Limited

    ability to replicate DDL • Large object replication not available • Trigger activity generates additional I/O • Event tracking consumes network bandwidth • Hard to scale up
  14. Trigger-based Pros & Cons • Primary keys required • Limited

    ability to replicate DDL • Large object replication not available • Trigger activity generates additional I/O • Event tracking consumes network bandwidth • Hard to scale up • Infrastructure is brittle
  15. WAL-based Replication • WAL files are the mechanism to recover

    from crashes • WAL files can be used to replay DML on remote servers
  16. WAL-based Replication • WAL files are the mechanism to recover

    from crashes • WAL files can be used to replay DML on remote servers • Streaming WAL activity effectively creates an up-to-date clone
  17. WAL-based Pros & Cons • It's fast • Easy to

    set up • Requires no additional software, I/O, bandwidth
  18. WAL-based Pros & Cons • It's fast • Easy to

    set up • Requires no additional software, I/O, bandwidth • Exact replica provides assurance for backups
  19. WAL-based Pros & Cons • It's fast • Easy to

    set up • Requires no additional software, I/O, bandwidth • Exact replica provides assurance for backups • Exact replica means all data types are supported
  20. WAL-based Pros & Cons • It's fast • Easy to

    set up • Requires no additional software, I/O, bandwidth • Exact replica provides assurance for backups • Exact replica means all data types are supported • Synchronous replication is possible
  21. WAL-based Pros & Cons • All or nothing • No

    major version upgrades • Query cancellations
  22. WAL-based Pros & Cons • All or nothing • No

    major version upgrades • Query cancellations • Storage impact on primary • wal_keep_segments • replication slots
  23. Middle Ground: Logical Replication • Introduced in v. 10 •

    Based off Logical Decoding framework introduced in v. 9.4
  24. Middle Ground: Logical Replication • Introduced in v. 10 •

    Based off Logical Decoding framework introduced in v. 9.4 • WAL files are reconstructed into SQL statements
  25. Middle Ground: Logical Replication • Introduced in v. 10 •

    Based off Logical Decoding framework introduced in v. 9.4 • WAL files are reconstructed into SQL statements • Paves the way to more granular replication filters
  26. Middle Ground: Logical Replication • Introduced in v. 10 •

    Based off Logical Decoding framework introduced in v. 9.4 • WAL files are reconstructed into SQL statements • Paves the way to more granular replication filters • Happy-medium
  27. Best of Both Worlds? • Like Trigger-based Replication: • Still

    need uniqueness/PK • DDL is not replicated • Sequences are not replicated
  28. Best of Both Worlds? • Like Trigger-based Replication: • Still

    need uniqueness/PK • DDL is not replicated • Sequences are not replicated • Like WAL-based Replication • Still need to monitor storage impact • Can’t create column-level filters (yet)
  29. Additional Gotchas • WAL events are unrolled into individual SQL

    queries • COPY statements are translated into INSERTs • UPDATE on 1000 rows are translated into 1000 individual UPDATEs
  30. Additional Gotchas • WAL events are unrolled into individual SQL

    queries • COPY statements are translated into INSERTs • UPDATE on 1000 rows are translated into 1000 individual UPDATEs • TRUNCATEs are replicated, but not cascaded
  31. Additional Gotchas • WAL events are unrolled into individual SQL

    queries • COPY statements are translated into INSERTs • UPDATE on 1000 rows are translated into 1000 individual UPDATEs • TRUNCATEs are replicated, but not cascaded • Partitioned tables cannot be easily replicated
  32. Other options • DRBD, Corosync, Windows Cluster • Basically, mirrors

    a disk or other block-level device • Doesn't support read-only standby • Use cases for these are somewhat rare
  33. Other options • DRBD, Corosync, Windows Cluster • Basically, mirrors

    a disk or other block-level device • Doesn't support read-only standby • Use cases for these are somewhat rare • Use at your own discretion
  34. Start with built-in Streaming Replication • Super-simple to set up

    • No additional software • Best performance, least impact
  35. Start with built-in Streaming Replication • Super-simple to set up

    • No additional software • Best performance, least impact • Good for most use-cases
  36. Next, try Logical Replication • Pick and choose which tables

    to replicate • WAL-based replication performance
  37. Next, try Logical Replication • Pick and choose which tables

    to replicate • WAL-based replication performance • Zero-downtime major-version upgrades
  38. Next, try Logical Replication • Pick and choose which tables

    to replicate • WAL-based replication performance • Zero-downtime major-version upgrades • Only available in v. 10 and later
  39. Next, try pglogical or EPRS • Replication engines supporting Logical

    Decoding • pglogical • EnterpriseDB Postgres Replication Server 7 (EPRS7 — Limited Availability)
  40. Next, try pglogical or EPRS • Replication engines supporting Logical

    Decoding • pglogical • EnterpriseDB Postgres Replication Server 7 (EPRS7 — Limited Availability) • Row-level and column-level filters
  41. Next, try pglogical or EPRS • Replication engines supporting Logical

    Decoding • pglogical • EnterpriseDB Postgres Replication Server 7 (EPRS7 — Limited Availability) • Row-level and column-level filters • Still WAL-based
  42. Use a Trigger-based Solution • But please upgrade soon! •

    Impacts performance • Often difficult to administration
  43. Use a Trigger-based Solution • But please upgrade soon! •

    Impacts performance • Often difficult to administration • Logical Replication is the future
  44. Multi-Master Replication? • Holy Grail of replication solutions? • First,

    ask yourself if you REALLY need it • Consistency • Performance • Conflict Resolution • Often, the other options are good enough ?
  45. Multi-Master Replication • Trigger-based • Bucardo • XDB by EnterpriseDB

    • WAL-based • Bi-Directional Replication (BDR) by 2nd Quadrant • requires a custom compile/installation of PG • XDB/EPRS7 by EnterpriseDB