简体   繁体   中英

Multi-Region Aurora with write forwarding from Spring Boot Application

I have created a multi-region( eg two region ) Aurora cluster based on MySql engine. It has primary cluster with 1 writer and 1 reader instance, and secondary cluster with only Reader instances.

As per the Aurora documentation here , following command in secondary region on reader instance, can forward any write call to primary cluster writer instance.

SET aurora_replica_read_consistency = 'session';

This works fine, when I do the same via mysql client. And I can use secondary reader instance for write operations too.

Now, I have created an application having separate instance for these two regions. Primary application instance connected with primary Aurora cluster having writer and reader, hence I can do both read and write operation there.

For secondary application instance, which is connected to secondary Aurora cluster having only reader instance, only read operations are working.

As a solution I created writeForward.sql in spring boot application to execute and set aurora_replica_read_consistency during application initialisation on secondary cluster only. For this, I added following property to parameter store in secondary region only:

spring.datasource.data=classpath:writeForward.sql

But this is somehow not working and secondary application is still not able to do any write operation.

I am looking for some help on how to handle this.

After reading through the Aurora documentation again, I realise that write forwarding from secondary region only works when property aurora_replica_read_consistency is set for each session.

Always set the aurora_replica_read_consistency parameter for any session for which you want to forward writes. If you don't, Aurora doesn't enable write forwarding for that session.

To make this possible, each DB connection made by application need to execute this command:

SET aurora_replica_read_consistency = 'session';

For Spring Boot application using Hikari DB Connection pool, I used following property, which automatically executes above SQL command for each connection that is maintain with DB.

spring.datasource.hikari.connection-init-sql= SET aurora_replica_read_consistency = 'session'

Details about Hikari Connection Pool can be found here , which mentions about connectionInitSql property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM