Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

[#43] Mysql Master/Slave Replication 적용 #60

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from

Conversation

tjdrnr0557
Copy link
Collaborator

@tjdrnr0557 tjdrnr0557 commented Dec 2, 2020

AbstractRoutingDataSource 클래스를 이용하여

TargetDataSources에 slave와 master DataSource를 등록하고
determineCurrentLookupKey 함수를 오버라이드하여
Slave Datasource와 Master DataSource중 선택하여 사용합니다.

어떤 데이터소스를 사용할지는 AOP를 사용하여 구현하였습니다.
메소드(Mapper) 시작전에 AOP를 이용하여 쓰레드로컬 변수에 마스터와 슬레이브중 한개를 넣어놓고
determineCurrentLookupKey함수에서 쓰레드로컬 변수를 가져와 어떤 데이터소스를 사용할지 정합니다.

determineCurrentLookupKey는 JDBC getConnection()함수에서 호출되어 데이터소스가 정해집니다.

자세한 설명은 다음 블로그에 정리하였습니다.
https://tjdrnr05571.tistory.com/14?category=876333

LazyConnectionDataSourceProxy를 사용한 이유
https://tjdrnr05571.tistory.com/15?category=876333

@tjdrnr0557 tjdrnr0557 self-assigned this Dec 2, 2020
@tjdrnr0557 tjdrnr0557 changed the title Feature/43 [#43] Mysql Master/Slave Replication 적용 Dec 2, 2020
} else {
throw new WrongDataSourceException("Wrong DataSource Type : Should Check Exception");
}

Copy link
Collaborator Author

@tjdrnr0557 tjdrnr0557 Dec 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

만약 DataSource Type이 Master나 Slave가 아니라면 개발자들이 어노테이션에 오타를 내거나 이름을 잘못지은 오류일 것입니다.

따라서 아예 컴파일타임에 개발자들이 exception을 알아채고 처리를 해주어야 할 것 같아서 checked Exception인 SQLException을 사용하였습니다.


@Aspect
@Component
public class SetDataSourceAspect {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 처리해줄 필요없이 스프링에서 제공하는 기능이 있을 것 같습니다~ readOnly 트랜잭션이면 slave에서 읽는다거나요~

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Transactional을 서비스에 붙히고 readOnly인지 아닌지에 따라 DataSoruce들을 분기해줄 수 있는 방식과 지금처럼 AOP방식을 고민을 했엇습니다.

하지만 트랜잭션 방식을 사용하여 라우팅해준다면 모든 서비스에 @Transactional을 붙혀야 하고 단일연산에도 트랜잭션 설정을 해줘야 해서 AOP방식으로 데이터소스를 나누도록 구현 하였습니다.

@tjdrnr0557
Copy link
Collaborator Author

성능테스트 결과를 보니 WAS서버 3대로 Scale Out을 적용하여도 Mysql 사용량이 최대 60%정도가 나왔고 슬로우 쿼리도 없었습니다.

또한 레디스 캐싱, 엔진엑스 캐싱등을 이용하여 디비 부하를 분산시켜줬기 때문에 아직까지는 Mysql의 성능이 충분하고 추후 부족하더라도 Scale up으로 충분한 대응이 가능해보입니다.

이에 따라 아직은 Mysql을 이중화할 필요까진 없는 것으로 판단하여 이 PR을 보류중입니다.

@tjdrnr0557 tjdrnr0557 added the complete but merge hold 어떤 이유 때문에 이 PR을 보류합니다. label Jan 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
complete but merge hold 어떤 이유 때문에 이 PR을 보류합니다.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants