Sobes.tech
Middle+

What method of ensuring fault tolerance would you choose for PostgreSQL?

sobes.tech AI

Answer from AI

To ensure PostgreSQL fault tolerance, I would choose logical replication.

This is a modern and flexible approach based on the Publish/Subscribe technology. The publisher on the main server publishes changes in tables, and the subscriber on the replica receives and applies them.

  • Advantages:

    • Flexibility: You can replicate not the entire database, but only specific tables or schemas.
    • Different PostgreSQL versions: Allows replication between clusters with different major versions of PostgreSQL.
    • Low overhead: Usually has less impact on the master server's performance compared to physical replication.
    • Easier migration: Convenient for horizontal sharding or data migration.
  • Disadvantages:

    • Does not replicate DDL (schema structure changes), except in some cases (e.g., adding a column). Requires manual DDL application on replicas.
    • Does not replicate sequences.
    • Requires more attention to conflict management.

As an alternative, especially for simpler scenarios or strict data synchronization, streaming physical replication can be considered. It copies changes at the WAL (Write-Ahead Log) level, providing an exact copy of the master server.

  • Advantages:

    • Easy to set up and use.
    • Replicates all changes, including DDL and sequences.
    • More reliable in terms of data consistency during failover.
  • Disadvantages:

    • Less flexible: replicates the entire database.
    • Requires the same major PostgreSQL version on master and replicas.
    • May have higher overhead on the master server, especially with synchronous replication.

For automatic failover and cluster management in both cases, additional tools such as Patroni, Stolon, or pgBouncer combined with scripts are necessary. These tools monitor node status, select a new master during failure, and reconfigure replicas.

The choice between logical and physical replication depends on specific project requirements, database size, the need to replicate only part of the data, and tolerance for version discrepancies. In most modern scenarios where flexibility and table-level replication are needed, logical replication is preferable.