Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat-be: DataSource Routing을 통한 DB Read/Write 요청 분산 #704

Merged
merged 12 commits into from
Sep 26, 2024

Conversation

github-actions[bot]
Copy link
Contributor

Original issue description

목적

read 트랜잭션, write 트랜잭션에 따른 DB routing 분리

작업 세부사항

  • prod 서버 database yml 정보 변경
  • DB routing 분리를 위한 codebase 변경

참고 사항

아래의 별표줄 밑에 요구사항 ID만 작성해주세요. Prefix 금지!


DB_ROUTING_01

closes #703

@github-actions github-actions bot added backend 백엔드 feature 새로운 기능 labels Sep 24, 2024
@xogns1514 xogns1514 deleted the branch be/develop September 24, 2024 13:20
@xogns1514 xogns1514 closed this Sep 24, 2024
@xogns1514 xogns1514 reopened this Sep 24, 2024
@Dobby-Kim Dobby-Kim assigned Dobby-Kim and unassigned xogns1514 Sep 25, 2024
@Primary
@DependsOn(ROUTE_DATASOURCE)
public DataSource defaultDataSource() {
return new LazyConnectionDataSourceProxy(routeDataSource());
Copy link
Contributor

Choose a reason for hiding this comment

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

LazyConnectionDataSourceProxy은 실제로 커넥션이 필요한경우가 아니라면 데이터베이스 풀에서 커넥션을 점유하지 않고 실제로 필요한 시점에만 커넥션을 점유하게 할 수 있게 합니다.

@Dobby-Kim Dobby-Kim assigned xogns1514 and unassigned Dobby-Kim Sep 25, 2024
dataSourceMap.put(DataSourceRouter.READ_DATASOURCE_KEY, readDataSource);
dataSourceMap.put(DataSourceRouter.WRITE_DATASOURCE_KEY, wrtieDataSource);
dataSourceRouter.setTargetDataSources(dataSourceMap);
dataSourceRouter.setDefaultTargetDataSource(readDataSource);
Copy link
Contributor

Choose a reason for hiding this comment

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

determineCurrentLookupKey() 를 통해 반환받은 Key로부터 DataSource를 반환받을 수 없는 경우에 default DataSource를 사용합니다.

Copy link
Member

Choose a reason for hiding this comment

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

default를 readDataSource로 정한 이유가 있나요? read DB는 read-only여서 write DB로 설정하는게 맞지 않나 싶어서요!

Copy link
Contributor

Choose a reason for hiding this comment

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

slave DB가 죽었을때는 master를 바라보게 하는것이 writre/read 둘다 가능하니 좋겠네요.

@xogns1514 xogns1514 marked this pull request as ready for review September 25, 2024 02:50
Copy link
Contributor Author

1727232638.558739

Copy link
Contributor Author

📌 Test Coverage Report

Overall Project 76.84% -0.66%
Files changed 15%

File Coverage
DataSourceRouter.java 100% 🍏
DataSourceConfig.java 0%

Copy link
Member

@Chocochip101 Chocochip101 left a comment

Choose a reason for hiding this comment

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

처음 도입해보는 기술이라 학습이 많이 필요했을텐데, 수고하셨습니다, 러쉬!

관련해서 리뷰 남겼습니다. 확인해주세요!

dataSourceMap.put(DataSourceRouter.READ_DATASOURCE_KEY, readDataSource);
dataSourceMap.put(DataSourceRouter.WRITE_DATASOURCE_KEY, wrtieDataSource);
dataSourceRouter.setTargetDataSources(dataSourceMap);
dataSourceRouter.setDefaultTargetDataSource(readDataSource);
Copy link
Member

Choose a reason for hiding this comment

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

default를 readDataSource로 정한 이유가 있나요? read DB는 read-only여서 write DB로 설정하는게 맞지 않나 싶어서요!

import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;

@Configuration
@Profile("prod")
Copy link
Member

Choose a reason for hiding this comment

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

profile을 설정하지 않고, 매 profile에 read db와 write db의 url을 같게하면 될 것 같아요. profile을 한정한 이유가 있나요?

Comment on lines +15 to +17
if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
return READ_DATASOURCE_KEY;
}
Copy link
Member

Choose a reason for hiding this comment

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

복제 구성은 주로 writer:read의 관계가 1:N입니다. 현재 구조에서 read db가 늘어나도 코드를 그대로 재사용할 수 있나요?

Copy link
Contributor

Choose a reason for hiding this comment

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

현재 구조에서는 read data가 늘어났을 때 코드를 재사용할 수 없죠.
read data가 늘어난다면 round-robin 형식으로 datasource를 지정하도록 변경할 수 있을 것 같습니다.
해당 사항은 나중에 반영해보도록 하겠습니다. 적용해보려 했는데 properties를 다루면서 에러를 많이 만날 것 같습니다..

Copy link
Member

@cutehumanS2 cutehumanS2 left a comment

Choose a reason for hiding this comment

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

👍
간단한 코멘트 남겼습니다.
확인 부탁드려요~

Comment on lines 43 to 44
DataSource wrtieDataSource = writeDataSource();
DataSource readDataSource = readDataSource();
Copy link
Member

@cutehumanS2 cutehumanS2 Sep 25, 2024

Choose a reason for hiding this comment

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

이 포스팅(참고로 코틀린)의 구현 방식을 보고 제안합니다.

@Qualifier를 활용하여 readDataSource와 writeDataSource를 빈으로 주입하는 건 어떠신가요?
(반영 필수 아님)

Copy link
Contributor

Choose a reason for hiding this comment

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

반영해보려했으나, 밑에 부분에서도 주입을 해주어야해서 관리포인트가 늘어나는 것 같네요ㅠㅠ

   @Bean
    @Primary
    @DependsOn(ROUTE_DATASOURCE)
    public DataSource defaultDataSource() {
        return new LazyConnectionDataSourceProxy(routeDataSource());
    }

Base automatically changed from be/develop to be/main September 25, 2024 07:24
@Dobby-Kim Dobby-Kim changed the base branch from be/main to be/develop September 25, 2024 07:24
Copy link
Member

@Chocochip101 Chocochip101 left a comment

Choose a reason for hiding this comment

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

수고하셨습니다~ PR 제목도 수정해주세요!

@xogns1514 xogns1514 changed the title feat-be: DataSource Routing을 통한 DB Read/Write 요청 분산 #702 feat-be: DataSource Routing을 통한 DB Read/Write 요청 분산 Sep 26, 2024
@xogns1514 xogns1514 merged commit bb12b49 into be/develop Sep 26, 2024
30 checks passed
@xogns1514 xogns1514 deleted the be-703-DB_ROUTING_01 branch September 26, 2024 02:09
Dobby-Kim pushed a commit that referenced this pull request Sep 26, 2024
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Leetaehoon <66353672+xogns1514@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend 백엔드 feature 새로운 기능
Projects
Status: 완료
Development

Successfully merging this pull request may close these issues.

4 participants