A Pgpool 2(v. 4.3.1) is running in the streaming_replication mode with load_balance_mode = on. PostgreSQL is provisioned by AWS Aurora.
According to Pgpool 2 documentation, in streaming_replication mode PostgreSQL is responsible for replicating data in each server. To deal with a possible replication lag a set of additional configuration parameters can be set: sr_check_period, delay_threshold. If the actual delay exceeds the configured value, Pgpool 2 stops routing queries to reader DB instances.
However, in the Amazon Aurora example it is said that sr_check_period should be set to 0 (disabled) as Aurora does not expose the necessary functionality.
So the question is how to deal with the replication lag issue? Should I look to use the Snapshot Isolation mode for Pgpool 2? Or there is a better approach I should consider?
The replication lag issue will be tackled from multiple fronts, including validating if the existing AWS Aurora cluster corresponds to our needs, as well as application-level transaction optimization. In addition, I came up with a workaround that tells
Pgpool 2which queries should not be load balance. As per Pgpool 2 documentationTo work with the database our application uses the
Spring Data JPAwithHibernate. The later one allows to add custom comments via the query hint@QueryHint(name = org.hibernate.jpa.QueryHints.HINT_COMMENT, value = "NO LOAD BALANCE").Though, to make it work the session-wise property
hibernate.use_sql_commentsshould be set totrue. The drawback is that Hibernate will add default comments(which are the actual SQL queries) to every query it sends to DB making it twice as big. Hence, may degrade the performance. To avoid this I decided to override the method responsible for applying the hints and comments:org.hibernate.dialect.Dialect#addSqlHintOrCommentThe updated
ifstatement checks if theNO LOAD BALANCEis present and prepends the query if so even thehibernate.use_sql_commentsproperty isfalse. Setspring.jpa.database-platformproperty to use the extended custom Dialect.The last thing is wrapper
@NoLoadBalanceQueryannotation around the@QueryHint(name = org.hibernate.jpa.QueryHints.HINT_COMMENT, value = "NO LOAD BALANCE")- to make it easier to use for other devs.