Read Replicas

Ebean supports configuring a second DataSource [connect pool] that points to a "Read replica". The DataSource is readOnly = true and autoCommit=true.


Amazon AWS Aurora

Amazon AWS Aurora provides 2 connection endpoints with one endpoint for the "Master" [writer] and a second endpoint for the "Read replica" [read only] instance(s).


Using the "Master"

When we have an explicit transaction via @Transactional on a method or using explicitly database.beginTransaction() - then this transaction will use the "Master".

When we use database.save() without an explicit transaction, then a transaction will be used implicitly and this will use the "Master".

Query.useMaster()

If we have a query that is executed outside any explicit transaction, and it will fetch data (not a bulk update query or bulk delete query) then by default it will use the "Read Replica".

To force a query to use the "Master" we use Query.useMaster() like:

var customers =
  new QCustomer()
    .status.eq(Status.NEW)
    .useMaster() // Use the "Master" database
    .findList();

Using the Read Replica

If we have a query that is executed outside any explicit transaction, and it will fetch data (it is not a bulk update query or bulk delete query) then by default it will use the "Read Replica".

// a query executed outside of any explicit transaction
// ... will use the replica
var customers =
  new QCustomer()
    .status.eq(Status.NEW)
    .findList();

@Transaction(readOnly=true)

When a method is annotated with @Transaction(readOnly=true), then this will create a read only transaction using the "Read Replica".

@Transaction(readOnly=true)
void myMethod() {

  // do stuff with a transaction using the Read replica

}