Using Client Drivers to Implement Failover and Read/Write Splitting
Since PostgreSQL 10 (libpq.so.5.10), libpq has been supporting failover and read/write splitting, and Java Database Connectivity (JDBC) has been supporting read/write splitting, failover, and load balancing.
PostgreSQL client drivers are backward compatible. Even RDS for PostgreSQL 9.5 and 9.6 instances can be connected through the libpq driver of the latest version to implement failover.
In this section, failover refers to the failover of read-only workloads.
- libpq is a C application programming interface (API) to PostgreSQL. libpq is a set of library functions that allow client programs to pass queries to the PostgreSQL backend server and to receive the results of these queries.
- JDBC is an API used in Java to define how client programs access databases. In PostgreSQL, JDBC supports failover and load balancing.
Driver |
Read/Write Splitting |
Load Balancing |
Failover |
---|---|---|---|
libpq |
√ |
× |
√ |
JDBC |
√ |
√ |
√ |
Using libpq for Failover and Read/Write Splitting
You can use libpq functions to connect to multiple databases. If one database fails, workloads are automatically switched to another available database.
postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]
Example: Connect to one primary RDS for PostgreSQL instance and two read replicas. Read requests will not fail as long as there is at least one available instance.
postgres://<instance_ip>:<instance_port>,<instance_ip>:<instance_port>,<instance_ip>:<instance_port>/<database_name>?target_session_attrs=any
Parameter |
Description |
Example Value |
---|---|---|
<instance_ip> |
IP address of the DB instance. |
If you attempt to access the instance from an ECS, set instance_ip to the floating IP address displayed on the Overview page of the instance. If you attempt to access the instance through an EIP, set instance_ip to the EIP that has been bound to the instance. |
<instance_port> |
Database port of the DB instance. |
Set this parameter to the database port displayed on the Overview page. Default value: 5432 |
<database_name> |
Name of the database to be connected. |
The default management database is postgres. You can enter the database name based on the site requirements. |
target_session_attrs |
Type of the database to be connected. |
|
For details about libpq and related parameters, see Connection Strings.
You can use the pg_is_in_recovery() function in your application to determine whether the connected database is a primary instance (indicated by f) or a read replica to implement read/write splitting.
The following is an example of Python code (psycopg2 a wrapper for libpq):
// There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables. // In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed. import psycopg2 import os username = os.getenv("EXAMPLE_USERNAME_ENV") password = os.getenv("EXAMPLE_PASSWORD_ENV") conn = psycopg2.connect(database=<database_name>,host=<instance_ip>, user=username, password=password, port=<instance_port>, target_session_attrs="read-write") cur = conn.cursor() cur.execute("select pg_is_in_recovery()") row = cur.fetchone() print("recovery =", row[0])
Using JDBC for Failover and Read/Write Splitting
You can define multiple databases (hosts and ports) in the connection URL and separate them with commas (,). JDBC will attempt to connect to them in sequence until the connection is successful. If the connection fails, an error message is displayed.
jdbc:postgresql://node1,node2,node3/${database}?targetServerType=preferSecondary&loadBalanceHosts=true
Example:
jdbc:postgresql://<instance_ip>:<instance_port>,<instance_ip>:<instance_port>,<instance_ip>:<instance_port>/<database_name>?targetServerType=preferSecondary&loadBalanceHosts=true
For details about the Java code, see Connecting to an RDS for PostgreSQL Instance Through JDBC.
Parameter |
Description |
Example Value |
---|---|---|
targetServerType |
Type of the database to be connected. |
|
loadBalanceHosts |
Sequence of databases to be connected. |
|
To distinguish between the primary and secondary databases, check whether data can be written to the database. If yes, it is a primary database. If no, it is a secondary database. You can use the pg_is_in_recovery() function to determine whether a database is a primary database. For details, see Using libpq for Failover and Read/Write Splitting.
To implement read/write splitting, you need to configure two data sources. For the first data source, set targetServerType to primary to process write requests. For the second data source:
- If there is only one read replica, set targetServerType to preferSecondary to process read requests. Assume that the IP addresses of the primary instance and read replica are 10.1.1.1 and 10.1.1.2, respectively.
jdbc:postgresql://10.1.1.2:5432,10.1.1.1:5432/${database}?targetServerType=preferSecondary
- If there are two read replicas, set targetServerType to any to process read requests. Assume that the IP addresses of the read replicas are 10.1.1.2 and 10.1.1.3, respectively.
jdbc:postgresql://10.1.1.2:5432,10.1.1.3:5432/${database}?targetServerType=any&loadBalanceHosts=true
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot