Spring Boot & AWS RDS Part 2 - Read Replicas

In Previous post we discuss, How to use spring boot to access AWS RDS service. This post is continuation of same topic and we will explore, How we can configure & use ReadReplicas.

What is Database Read Replicas?

In General read replica is a copy of the primary database instance and it automatically reflects changes made in primary database in almost real time. Read Replicas can improve the performance of read-heavy database workloads by offloading read workload from primary instance.

How AWS RDS read replica works?

Amazon RDS uses the MariaDB, Microsoft SQL Server, MySQL, Oracle, and PostgreSQL DB engines’ built-in replication functionality to create a read replica. Any Updates made to the primary DB instance are asynchronously copied to the read replica.

AWS allows to create read replicas in same availability zone or in different availability zone. It Also allows to create read replicas in different regions. Creating up to 5 read replicas are allowed.

Why to use read replica?

  1. Read replicas can significantly improve the performance by redirecting read traffic to one or more read replicas.
  2. Read replicas are Ideal for implementing Business reporting or data warehousing work loads, Without impacting normal business flows.
  3. In some cases read replica can be used for implementing disaster recovery. Read replicas can be promoted as primary database instance.

    How to create read replicas in AWS Console?

    While creating a read replicas we need to specify an existing DB instance as the source. Then Amazon RDS takes a snapshot of the source instance and creates a read-only instance from the snapshot. The read replica operates as a DB instance that allows only read-only connections.
    Applications connect to a read replica the same way they do to any DB instance.

On AWS Console choose the DB instance that you want to use as the source for a read replica.
Then Go to action, choose Create read replica. Then Follow the same steps explained In Previous post

All the data from the main table will also be available in replicated instance. We can verify the data by connecting to replicated instance using PGAdmin or any other similar tool.

How to use read replicas with Spring Boot App

In Previous post we used spring-boot-starter-data-jpa, To use the full power for read replicas , In this example we will use Spring Cloud AWS JDBC as it provides some useful features.

  1. Spring cloud aws automatically detects the read-replica instance and If the read replica support is enabled, It will automatically send the requests to e replica instance. As an application developer we do not have to configure multiple data sources.
  2. Spring cloud aws does the Automatic retry incase of database failure. It attempts to send the same request to different availability zone.
  3. As an application developer, We do not need to worry about how many read replicas are configured.

    Setting up Spring Boot App

    Let’s create simple small Spring Boot app that will interact with primary database and read replicas.
    Apart form other needed dependency, We need to add spring-cloud-aws-jdbc as dependency

1
2
3
4
5
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-jdbc</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>

The code for this post is available on Github here

Configuring data source

The data sources can be configured using the Spring Boot configuration files. Because of the dynamic number of data sources inside one application, the Spring Boot properties must be configured for each data source.

data source configuration properties
1
2
3
cloud.aws.rds.<DB-Instance-ID>.username=admin1
cloud.aws.rds.<DB-Instance-ID>.password=Admin123
cloud.aws.rds.<DB-Instance-ID>.databaseName=employee

How to enable read-replica

enable read replica
1
cloud.aws.rds.employee-db.readReplicaSupport=true

How To redirect read traffic to read replica instance

For redirecting traffic to replicated instance we just need to use Transactions and set Transactional property as readOnly = true

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Service
@RequiredArgsConstructor
public class EmployeeService {

private final EmployeeRepository repository;

@Transactional
public void saveEmployeeToDatabase(Employee employee){
repository.save(employee);
}

@Transactional(readOnly = true)
public List<Employee> findAll(){
return repository.findAll();
}
}

Write work load and replication

All Write transactions will be redirected to the primary DB Instance and AWS will handle the replication asynchronously without impacting the performance of primary DB Instance.

Points to keep in mind before using read replicas

Read-replica feature of RDS can increase throughput and performance but replication is not exactly realtime. There will be some lag in coping with data from primary instance to replicated instance and Read replica might return outdated data in some scenarios.

The code for this post is available on Github here

Share Comments