์šด์˜ํ™˜๊ฒฝ์— ์ด๋Ÿฐ์ €๋Ÿฐ ๊ฐœ๋ฐœ์ด ๊ณ„์† ์ง„ํ–‰๋˜๊ณ  ๋ฐ์ดํ„ฐ๋„ ์Œ“์ด๊ณ  ํ•˜๋‹ค๋ณด๋‹ˆ ์ ์  ๋ฌด๊ฑฐ์›Œ์ง€๊ณ  ์žˆ๋Š” ์™€์ค‘์— dba์ชฝ์—์„œ slow query ๊ด€๋ จํ•˜์—ฌ ๋ฌธ์˜๊ฐ€ ๋“ค์–ด์™”๊ณ  ํ™•์ธํ•˜๋‹ค๋ณด๋‹ˆ ๋‹ค๋ฅธ ์ด์Šˆ๊ฐ€ ํ™•์ธ๋˜์—ˆ๋‹ค. readOnly ํŠธ๋žœ์žญ์…˜์œผ๋กœ ๋ฌถ์–ด๋†“์€ ์ฟผ๋ฆฌ์ธ๋ฐ reader์ชฝ์—์„œ ์‹คํ–‰๋˜์–ด์•ผ ํ•  ์ฟผ๋ฆฌ๊ฐ€ writer์ชฝ์—์„œ๋„ ์‹คํ–‰์ด ๋˜๊ณ  ์žˆ๋Š” ํ˜„์ƒ์ด ์ง€์†๋˜๊ณ  ์žˆ์—ˆ๋‹ค.

 

ํ˜„์žฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ SpringBoot 2.x + MariaDB connector 2.7.x + HikariPool + MyBatis ์‚ฌ์šฉํ•˜๋„๋ก ๊ตฌ์„ฑ๋œ ์ƒํƒœ.

compile 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1'
compile 'org.springframework.boot:spring-boot-starter-jdbc'
compile 'org.mariadb.jdbc:mariadb-java-client:2.7.7'
compile 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16'

 

db ํด๋Ÿฌ์Šคํ„ฐ๋Š” writer 1๊ฐœ์™€ reader 1๊ฐœ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ ์ฃผ์†Œ๋Š” writer, reader ์šฉ์œผ๋กœ ๊ฐ 1๊ฐœ์”ฉ ๋‘ ๊ฐœ๊ฐ€ ์žˆ๋‹ค.

  • my-cluster.cluster-xxx.ap-northeast-2.rds.amazonaws.com 
  • my-cluster.cluster-ro-xxx.ap-northeast-2.rds.amazonaws.com

 

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด writer์šฉ๊ณผ reader์šฉ ์ฃผ์†Œ๊ฐ€ ๋ณ„๋„๋กœ ์žˆ๋‹ค.

  • my-cluster-01.xxx.ap-northeast-2.rds.amazonaws.com (writer)
  • my-cluster-02.xxx.ap-northeast-2.rds.amazonaws.com (reader)

 

๊ตต๊ฒŒ ํ‘œ์‹œํ•œ ํด๋Ÿฌ์Šคํ„ฐ ์ฃผ์†Œ์™€ ์‹ค์ œ DB์ฃผ์†Œ์˜ ์ฐจ์ด๋Š” ํ™•์ธํ•˜๊ณ  ๋„˜์–ด๊ฐ€์ž.

 

๊ทธ๋ฆฌ๊ณ  jdbc url์€ ์•„๋ž˜์™€ ๊ฐ™์ด writer ํด๋Ÿฌ์Šคํ„ฐ ์ฃผ์†Œ๋งŒ ๋„ฃ์–ด์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ๋‚ด๊ฐ€ ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•œ๊ฑด ์•„๋‹ˆ์—ˆ๊ณ  ์ด๋ ‡๊ฒŒ ํ•ด๋„ aurora ์˜ต์…˜์„ ์“ธ ๊ฒฝ์šฐ ์ž๋™์œผ๋กœ reader์ชฝ์œผ๋กœ readOnly ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋œ๋‹ค๊ณ  ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ๊ทธ๋ž˜์„œ ๋ญ ๊ทธ๋Ÿฌ๋ ค๋‹ˆ ํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ์ด๋Ÿฐ ์ด์ƒํ•œ ํ˜„์ƒ์ด ํ™•์ธ๋œ ๊ฒƒ์ด๋‹ค. ( >,.< )

spring:
  datasource:
    url: jdbc:mariadb:aurora://my-cluster.cluster-xxx.ap-northeast-2.rds.amazonaws.com

 

์›์ธ์ถ”์ ์„ ํ•ด๋ณด๊ธฐ ์œ„ํ•ด ์ผ๋‹จ log4jdbc์˜ audit ์„ค์ •์„ ์ข€ ๋ณ€๊ฒฝํ–ˆ๋‹ค. @Transactional(readOnly=true) ์„ค์ •์ด ์ œ๋Œ€๋กœ ์•ˆ๋จนํžˆ๋Š”๊ฑด๊ฐ€?? ์‹ถ์–ด์„œ ๋ง์ด๋‹ค. 

logging:
  level:
    jdbc:
      audit: debug

 

ํ™•์ธํ–ˆ์„ ๋•Œ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ readOnly ์„ค์ •์€ ์ •์ƒ์ ์œผ๋กœ ๋˜๊ณ  ์žˆ์—ˆ๋‹ค.

DEBUG jdbc.audit : 1. Connection.setReadOnly(true) returned   com.zaxxer.hikari.pool.ProxyConnection.setReadOnly(ProxyConnection.java:423)
DEBUG jdbc.audit : 1. Connection.getAutoCommit() returned true  com.zaxxer.hikari.pool.HikariProxyConnection.getAutoCommit(HikariProxyConnection.java:-1)
DEBUG jdbc.audit : 1. Connection.setAutoCommit(false) returned   com.zaxxer.hikari.pool.ProxyConnection.setAutoCommit(ProxyConnection.java:414)
DEBUG jdbc.audit : 1. Connection.getAutoCommit() returned false

 

๊ทธ๋Ÿผ ์™œ ์ด๋Ÿด๊นŒ? ๋‹ค์Œ์œผ๋กœ๋Š” jdbc url์— ์•„๋ž˜์™€ ๊ฐ™์ด log ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋กœ๊ฑฐ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ mariaDB ์ชฝ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•ด๋ณด์•˜๋‹ค.

spring:
  datasource:
    url: jdbc:mariadb:aurora://my-cluster.cluster-xxx.ap-northeast-2.rds.amazonaws.com?log=true

 

<logger name="org.mariadb.jdbc" level="info">
    <appender-ref ref="stdout"/>
</logger>

 

DEBUG 88239 --- [nio-8080-exec-1] org.mariadb.jdbc.MariaDbConnection       : conn=817636(M) - set read-only to value true
DEBUG 88239 --- [nio-8080-exec-1] o.m.j.i.protocol.AbstractQueryProtocol   : System variable change :  autocommit = OFF
{{์ฟผ๋ฆฌ ์‹คํ–‰}}
DEBUG 88239 --- [nio-8080-exec-1] o.m.j.i.protocol.AbstractQueryProtocol   : System variable change :  autocommit = ON
DEBUG 88239 --- [nio-8080-exec-1] org.mariadb.jdbc.MariaDbConnection       : conn=53856(S) - set read-only to value false

 

๋กœ๊ทธ์ƒ์œผ๋กœ๋Š” ๋„๋Œ€์ฒด ์‹ค์ œ๋กœ ์–ด๋Š ํด๋Ÿฌ์Šคํ„ฐ ์ฃผ์†Œ์—์„œ ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋˜๋Š”์ง€ ์•Œ ์ˆ˜๊ฐ€ ์—†์—ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์šด์˜ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•˜๊ณ ์žˆ๋Š” mariaDB ์ปค๋„ฅํ„ฐ์™€ ๋™์ผํ•œ ๋ฒ„์ „์˜ ์†Œ์Šค์ฝ”๋“œ์— ๋กœ๊ทธ๋ฅผ ์—ฌ๊ธฐ์ €๊ธฐ ์ถ”๊ฐ€ํ•˜๋ฉด์„œ ์ถ”์ ์„ ์‹œ์ž‘ํ–ˆ๊ณ ,

๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜„์ƒ์„ ํ™•์ธํ–ˆ๋‹ค.

  1. writer ํด๋Ÿฌ์Šคํ„ฐ ์ฃผ์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์‹ ๊ทœ ์ปค๋„ฅ์…˜์„ ์ƒ์„ฑ๋œ๋‹ค. 
  2. readOnly ์ฟผ๋ฆฌ๊ฐ€ writer์ชฝ์—์„œ ์‹คํ–‰๋œ ๋’ค์— failover ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰๋˜๋ฉด์„œ mariaDB FailoverLoop์—์„œ reader์ชฝ ์ปค๋„ฅ์…˜์ด ์ƒ์„ฑ๋˜์–ด pool์— ๋“ค์–ด๊ฐ„๋‹ค.
  3. ์ดํ›„์˜ readOnly ์ฟผ๋ฆฌ๋“ค์€ ์ •์ƒ์ ์œผ๋กœ reader์ชฝ์—์„œ ์‹คํ–‰๋œ๋‹ค.

 

์ด์ œ ๊ถ๊ธˆํ•œ ์ ์ด ์ƒ๊ฒผ๋‹ค.

  1. ์ปค๋„ฅ์…˜์˜ ์ƒ์„ฑ์ฃผ๊ธฐ์— ๋”ฐ๋ผ ์‹ ๊ทœ ์ปค๋„ฅ์…˜์„ ์ƒ์„ฑํ•˜๊ฒŒ๋˜๋ฉด reader์ชฝ ์ปค๋„ฅ์…˜์€ ์‚ฌ๋ผ์ง€๋Š”์ง€?
  2. ์‹ ๊ทœ ์ปค๋„ฅ์…˜ ์ƒ์„ฑ์‹œ reader host๊ฐ€ ํฌํ•จ๋˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์„์ง€..? (์ด๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด 1๋ฒˆ๊ณผ ๊ฐ™์€ ํ˜„์ƒ์€ ์—†์–ด์งˆํ…Œ๋‹ˆ)
  3. ๊ทธ๋ ‡๊ฒŒํ•˜๋ฉด readOnly ์ฟผ๋ฆฌ๊ฐ€ ํ•ญ์ƒ reader๋กœ ๋“ค์–ด๊ฐ€๋Š”์ง€?

 

1๋ฒˆ ์ผ€์ด์Šค์˜ ๊ฒฝ์šฐ maxLifeTime ์„ค์ •๊ฐ’์„ 30์ดˆ๋กœ ์„ค์ •ํ•ด์„œ ํ…Œ์ŠคํŠธํ•ด๋ณด์•˜๊ณ , reader์ชฝ ์ปค๋„ฅ์…˜์€ ์‚ฌ๋ผ์ง€๊ฒŒ๋œ๋‹ค. readOnly ์ฟผ๋ฆฌ๊ฐ€ reader์—์„œ ์‹คํ–‰๋˜๋ ค๋ฉด ์œ„์— ๋งํ•œ failover ๊ณผ์ •์ด ๋‹ค์‹œ ํ•„์š”ํ•˜๋‹ค. ์ฆ‰, maxLifeTime์ด ์ง€๋‚˜ ์ปค๋„ฅ์…˜์„ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ failover ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰๋˜์–ด์•ผ reader๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. (์•„๋ž˜ 2๋ฒˆ ์ผ€์ด์Šค์— ๋Œ€ํ•œ ๋‚ด์šฉ๊นŒ์ง€ ํ™•์ธํ•˜๋ฉด ์•Œ๊ฒ ์ง€๋งŒ ์–ด๋””๊นŒ์ง€๋‚˜ url ์„ค์ •์— ๋ฉ”์ธ ํด๋Ÿฌ์Šคํ„ฐ ์ฃผ์†Œ๋งŒ ๋„ฃ์—ˆ์„ ๊ฒฝ์šฐ์ด๋‹ค.)

 

2๋ฒˆ ์ผ€์ด์Šค์˜ ๊ฒฝ์šฐ jdbc url ์„ค์ •์— reader ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ฃผ์†Œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๊ธฐ์กด ์„ค์ •๊ณผ ๋ณ€๊ฒฝํ•œ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

spring:
  datasource:
    url: jdbc:mariadb:aurora://my-cluster.cluster-xxx.ap-northeast-2.rds.amazonaws.com

 

spring:
  datasource:
    url: jdbc:mariadb:aurora://my-cluster.cluster-xxx.ap-northeast-2.rds.amazonaws.com,my-cluster.cluster-ro-xxx.ap-northeast-2.rds.amazonaws.com

์ด๋ ‡๊ฒŒ reader cluster์˜ ์ฃผ์†Œ๊ฐ€ ์ถ”๊ฐ€๋œ jdbc url์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ํ•ญ์ƒ reader ํด๋Ÿฌ์Šคํ„ฐ์ชฝ์œผ๋กœ readOnly ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ํ˜น์‹œ๋ผ๋„ url ๋’ค์ชฝ์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’๋“ค์€ ์ œ์ผ ๋งˆ์ง€๋ง‰์— ํ•œ๋ฒˆ๋งŒ ๋ถ™์—ฌ์ฃผ๋ฉด ๋œ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’์„ ์•„๋ž˜์ฒ˜๋Ÿผ ๋„ฃ๊ฒŒ๋˜๋ฉด mariaDB ์ปค๋„ฅํ„ฐ์—์„œ url ํŒŒ์‹ฑํ•  ๋•Œ ๋’ค ์ชฝ url์ด ์‚ฌ๋ผ์ง€๊ฒŒ ๋˜๋‹ˆ ์ฃผ์˜!!! ์ฆ‰, ๋ชจ๋“  ํด๋Ÿฌ์Šคํ„ฐ์— ๋™์ผํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ !!

url: my-cluster.cluster-xxx.ap-northeast-2.rds.amazonaws.com?log=true,my-cluster.cluster-ro-xxx.ap-northeast-2.rds.amazonaws.com?log=true

 

๊ทธ๋ƒฅ ์–ผํ• ๋“ฃ๊ธฐ๋กœ๋Š” ๋ฉ”์ธํด๋Ÿฌ์Šคํ„ฐ ์ฃผ์†Œ๋งŒ ๋„ฃ์–ด์ฃผ์†Œ jdbc url์— aurora ์˜ต์…˜์„ ๋„ฃ์–ด์ค„ ๊ฒฝ์šฐ readOnly ํŠธ๋žœ์žญ์…˜์€ ์ž๋™์œผ๋กœ reader ์ชฝ์œผ๋กœ ๋“ค์–ด๊ฐ„๋‹ค๊ณ  ๋“ค์—ˆ์—ˆ์œผ๋‚˜ ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•œ ๋””๋ฒ„๊น…์ด์—ˆ๋‹ค.

 

๊ฒฐ๊ตญ ๊ฒฐ๋ก ์€ ์ด๋ ‡๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ ์ฃผ์†Œ๊ฐ€ writer, reader๊ฐ€ ๋ณ„๋„๋กœ ์žˆ์„ ๊ฒฝ์šฐ (์•„๋งˆ AWS aurora๋ฅผ ํด๋Ÿฌ์Šคํ„ฐ๋งํ•ด์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋‹ค ์ด๋Ÿด ๊ฒƒ์ด๋‹ค) ๊ฐœ๋ฐœ์ž ์ž…์žฅ์—์„œ๋Š” writer, reader ๋‘ ํด๋Ÿฌ์Šคํ„ฐ ์ฃผ์†Œ๋ฅผ jdbc url์— ๋„ฃ์–ด์ฃผ์–ด์•ผ readOnly ํŠธ๋žœ์žญ์…˜์ด ํ•ญ์ƒ ์ •์ƒ์ ์œผ๋กœ reader ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ์‹คํ–‰๋˜๊ฒŒ ๋œ๋‹ค.

 

์ฐธ๊ณ ๋กœ...mariaDB connector 3.x ๋ฒ„์ „์—์„œ๋Š” aurora ์˜ต์…˜์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹จ๋‹ค...