Wednesday, March 20, 2024

Using Aurora replicas for read-only queries

 AWS aurora in serverless mode can be configured with replicas to improve failover speeds

Unlike a traditional database replica which are used to switchover when the primary replica. Aurora replica are available for read queries. If we can connect to read url then we can shift the read load to replicas and improve the overall performance.

The process of connecting to a different urls for read and write introduce some context switching in connection pools. Having two separate connection pools is the recommended approach of from aws when using of @Transactional(readOnly = True).  Reference aws repo.

The Gist of the approach is as follows

  1. Create two datasource using ConfigurationProperties annotation


    image-20240228-162958.png
  2. Use the AbstractRoutingDataSource to switch between Reader/Writer



    image-20240228-162459.png

  3. Mark it as primary so that Liquibase uses it.

  4. Create a annotation which can be weaved around methods and flips to reader if enabled



    image-20240228-162705.png

  5. Annotate your calls to use readonly datasource instead of transaction=read-only




No comments:

Post a Comment