The story of switching from Amazon RDS for MySQL to Amazon Aurora Serverless

Introduction

From Amazon RDS for MySQL to Amazon Aurora Serverless I had the opportunity to switch to, so I would like to introduce something like a clue. This article is based on Java, SpringBoot, but the essence is regardless of language or framework, so your environment is appropriate. I would appreciate it if you could read it as.

What is Amazon Aurora Serverles?

For details, please check the official document etc., but the major feature is that it does not have an instance to manage. It may be a bit rough analogy, but with a mechanism like the database version of AWS Lambda, the database server starts when the database is used. When you finish using it, it will stop, and you will be charged for the amount of Aurora Capacity Unit (ACU) used during that time. (To be exact, there is an additional charge for database storage and I / O in addition to ACU)

How to use Amazon Aurora Serverles

In my case, I was using Amazon RDS for MySQL on my internal system, but I switched to Amazon Aurora Serverles. Since this system is basically used only during business hours and it is assumed that a very severe response time is not required, you do not have to worry too much about the time until the database starts, and outside business hours It was a suitable system for using Amazon Aurora Serverles because I wanted to stop it for cost reasons.

On the other hand, it does not seem to be very suitable for a system that requires a quick response 24 hours a day, 365 days a year, so it is important to understand the characteristics of the system and select the appropriate service. That's right.

The highlight of Amazon Aurora Serverles

It may be a bit confusing to be addicted to, but you need to be a little careful if you are using a database connection pool. Depending on the framework you're using and its settings, Amazon Aurora Serverles will last forever because connection pools often keep you connected to your database on a permanent basis. Will fall into a situation where it will not stop. To avoid this situation, it is necessary to review the connection pool settings or stop the connection pool.

In my case, SpringBoot 2.0 used a connection pool called HikariCP, so I responded by changing this setting. Only the following properties have been changed.

spring.datasource.hikari.minimum-idle=0
spring.datasource.hikari.idle-timeout=180000

spring.datasource.hikari.minimum-idle is the minimum number of idle connections maintained in the connection pool, and spring.datasource.hikari.idle-timeout is until the connection is idle in the connection pool. Time (milliseconds).

By making these settings, the connection will be idle after 180 seconds, and the connection will be dropped from the connection pool from the setting that the idle connection is not maintained. This allowed us to drop the connection from the connection pool when not in use, creating a state where Amazon Aurora Serverles could be stopped.

Compatibility with Spring Session

In a niche, if you use Spring Session (https://spring.io/projects/spring-session) to manage your application's sessions and use Amazon Aurora Serverles for that session store. Also needs a little attention. Spring Session periodically issues a query that deletes expired session information, but by default this query is issued every minute. When a query is issued every minute, Amazon Aurora Serverles naturally falls into a situation where it is not stopped.

The issuance interval of this query can be changed with the following properties. (The default value is 0 * * * * *)

spring.session.jdbc.cleanup-cron=0 0 0 * * 0

With the above settings, the delete query will be issued every Sunday at 00:00:00. (Note that this property is "seconds, minutes, hours, days, months, days of the week" unlike cron)

Impact on application

In my case, except for the above configuration changes, I didn't need to change anything other than the JDBC connection string. It may depend on the size of the database, but at least on the systems I support, Amazon Aurora Serverles starts in about 20 to 25 seconds, and once started, it has the same performance as before. The application is running.

Summary

I was really surprised to be able to switch to a serverless database in the same way as a traditional relational database, except around the connection pool. The connection pool part can be guessed by those who have knowledge, so the expression addictive in the article may not be honestly appropriate. As long as the assumptions are met, I think it is a very effective service in terms of cost and operation, so why not consider it once.

Recommended Posts

The story of switching from Amazon RDS for MySQL to Amazon Aurora Serverless
Util to use for some reason
The story of switching from Amazon RDS for MySQL to Amazon Aurora Serverless
How to use binding.pry for view files
[Ruby] How to use slice for beginners
The story of migrating from Paperclip to Active Storage
The story of raising Spring Boot from 1.5 series to 2.1 series part2
The story of Collectors.groupingBy that I want to keep for posterity
The story of RxJava suffering from NoSuchElementException
The story of releasing the Android app to the Play Store for the first time.
Connect to Aurora (MySQL) from a Java application
The story of introducing Ajax communication to ruby
The story of raising Spring Boot 1.5 series to 2.1 series
The story of adding the latest Node.js to DockerFile
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
From the introduction of devise to the creation of the users table
How to write Scala from the perspective of Java
The story of migrating a stray batch without an owner from EC2 to a Docker environment
Get to the abbreviations from 5 examples of iterating Java lists
20190803_Java & k8s on Azure The story of going to the festival
The story of throwing BLOB data from EXCEL in DBUnit
How to get the longest information from Twitter as of 12/12/2016
From the habit of creating Value Objects for object-oriented understanding
The idea of C # (lambda expression, for statement) to chew
When you want to change the MySQL password of docker-compose
The story of pushing Java to Heroku using the BitBucket pipeline
Reintroduction to Java for Humanities 0: Understanding the Act of Programming
[Apache Tomcat] The story of using Apache OpenWebBeans to enable CDI
The story of making a binding for libui, a GUI library for Ruby that is easy to install
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
How to get the contents of Map using for statement Memorandum
The story of toString () starting with passing an array to System.out.println
[Rails] How to change the page title of the browser for each page
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
How to check for the contents of a java fixed-length string
A story about changing skills from COBOL cultivated for 5 years in the late 20s to a Web language