Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Developing Modern Database Applications with PostgreSQL

You're reading from   Developing Modern Database Applications with PostgreSQL Use the highly available and object-relational PostgreSQL to build scalable and reliable apps

Arrow left icon
Product type Paperback
Published in Aug 2021
Publisher Packt
ISBN-13 9781838648145
Length 440 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Marcelo Diaz Marcelo Diaz
Author Profile Icon Marcelo Diaz
Marcelo Diaz
Quan Ha Le Quan Ha Le
Author Profile Icon Quan Ha Le
Quan Ha Le
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Section 1 - Introducing PostgreSQL Development and Administration
2. Introduction to PostgreSQL Development and Administration FREE CHAPTER 3. Section 2 - Development in PostgreSQL
4. Setting Up a PostgreSQL RDS for ATMs 5. Using PostgreSQL and Node.js for Banking Transactions 6. Managing Bank ATM Locations Using PostgreSQL and Django 7. Creating a Geospatial Database Using PostGIS and PostgreSQL 8. Managing Banking Transactions using PostgREST 9. Section 3 - Administration in PostgreSQL
10. PostgreSQL with DevOps for Continuous Delivery 11. PostgreSQL High Availability Clusters 12. High-Performance Team Dashboards Using PostgreSQL and New Relic 13. Testing the Performance of Our Banking App with PGBench and JMeter 14. Test Frameworks for PostgreSQL 15. Other Books You May Enjoy APPENDIX - PostgreSQL among the Other Current Clouds

Managing HA in PostgreSQL

Managing HA in PostgreSQL is very important to ensure that database clusters maintain exceptional uptime and strong operational performance so that your data is always available to the application.

Master-slave might be the most basic and easiest HA architecture for developers. It is based on one master database with one or more standby servers. These standby databases will remain synchronized (or almost synchronized) with the master, depending on whether the replication is synchronous or asynchronous.

It is important to understand that PostgreSQL does not have a native mechanism to control failovers, that is, when the master fails automatically, the standby server becomes the new master with downtime as close to 0 as possible. To perform this procedure, there are third-party tools such as repmgr, pgpool-II, or patroni, to name a few.

These tools are placed in a layer above PostgreSQL, and they control the health status of the master; when a problem occurs, these tools fire a series of actions to promote the standby server as the new master.

There are several ways to classify a standby database:

  • By the nature of the replication:
    - Physical standbys: Disk blocks are copied.
    - Logical standbys: The streaming of the data changes.
  • By the synchronicity of the transactions:
    - Asynchronous: There is a possibility of data loss.
    - Synchronous: There is no possibility of data loss; the commits in the master wait for the response of the standby.
  • By usage:
    - Hot standbys: Support read-only connections; the hot standbys are configured for synchronous-commit mode, so their master server must wait for the hot standbys to confirm that they have consolidated the transaction log (when a commit statement is performed on the master, the progress of the commit will only be completed after all of the hot standbys have finished consolidating the transaction log).
    - Async standbys: These kinds of servers are configured by asynchronous-commit mode; therefore, the master server will not wait for the async standbys to consolidate the transaction log (when a commit statement is performed on the master, the progress of the commit will not wait for these async standbys to consolidate).

PostgreSQL uses a stream of WAL records to synchronize the standby databases. They can be synchronous or asynchronous, and the entire database server is replicated.

However, a master-slave setup is not enough to effectively ensure HA, as we also need to handle failures. To handle failures, we need to be able to detect them. Once we know there is a failure, for example, errors on the master, or the master is not responding, then we can select a slave and failover mechanism to it with the smallest amount of delay possible. It is important that this process is as efficient as possible, in order to restore full functionality so that the applications can start functioning again. PostgreSQL itself does not include an automatic failover mechanism, so it will require some custom script or third-party tools for this automation.

After a failover happens, the applications need to be notified accordingly so that they can start using the new master. Additionally, we need to evaluate the state of our architecture after a failover because we can run into a situation where we only have the new master running (for instance, we had a master and only one slave before the issue). In that case, we will need to somehow add a slave so as to recreate the master-slave setup we originally had for HA.

You have been reading a chapter from
Developing Modern Database Applications with PostgreSQL
Published in: Aug 2021
Publisher: Packt
ISBN-13: 9781838648145
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime