Skip to content

Commit

Permalink
feat: [DB] DB Connection Pool (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
siyeonSon authored Jan 19, 2025
1 parent 0a2de97 commit 47faa4b
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions DB/DB Connection Pool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
## λ°μ΄ν„°λ² μ΄μŠ€ 컀λ„₯μ…˜ ν’€
- μ• ν”Œλ¦¬μΌ€μ΄μ…˜ λ‘œλ”© μ‹œμ μ— Connection 객체λ₯Ό 미리 μƒμ„±ν•˜κ³ , μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ λ°μ΄ν„°λ² μ΄μŠ€μ— 연결이 ν•„μš”ν•  경우 미리 μ€€λΉ„λœ Connection 객체λ₯Ό μ‚¬μš©ν•œλ‹€
- DB μ—°κ²° μ‹œ TCP/IP ν•Έλ“œμ…°μ΄ν¬μ™€ 같은 μ—¬λŸ¬ 단계λ₯Ό 거쳐야 ν•œλ‹€. λ°μ΄ν„°λ² μ΄μŠ€ 컀λ„₯μ…˜ 풀을 μ‚¬μš©ν•˜μ—¬ DB 연결에 μ†Œμš” μ‹œκ°„μ„ μ ˆμ•½ν•˜κ³  μ„±λŠ₯을 ν–₯μƒμ‹œν‚¬ 수 μžˆλ‹€

### λ°μ΄ν„°λ² μ΄μŠ€ 컀λ„₯μ…˜ ν’€μ˜ λ™μž‘ 방식
1. μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ„œλ²„κ°€ μ‹œμž‘λ  λ•Œ 일정 수의 컀λ„₯μ…˜μ„ 미리 μƒμ„±ν•œλ‹€
2. μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μš”μ²­μ— 따라 μƒμ„±λœ 컀λ„₯μ…˜ 객체λ₯Ό μ „λ‹¬ν•œλ‹€
3. 일정 수 μ΄μƒμ˜ 컀λ„₯μ…˜μ΄ μ‚¬μš©λ˜λ©΄ μƒˆλ‘œμš΄ 컀λ„₯μ…˜μ„ λ§Œλ“ λ‹€
4. μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” 컀λ„₯μ…˜μ„ μ’…λ£Œν•˜κ³  μ΅œμ†Œν•œμ˜ κΈ°λ³Έ 컀λ„₯μ…˜μ„ μœ μ§€ν•œλ‹€

![image](https://github.com/user-attachments/assets/96b03f43-c047-42d8-babd-09c25d81adc6)

### 컀λ„₯μ…˜ ν’€μ˜ μž₯점
- 컀λ„₯μ…˜ 객체λ₯Ό 미리 λ§Œλ“€μ–΄ μ—°κ²°ν•˜μ—¬ λ©”λͺ¨λ¦¬ 상에 등둝해두기 λ•Œλ¬Έμ— ν΄λΌμ΄μ–ΈνŠΈκ°€ λΉ λ₯΄κ²Œ DB에 접속할 수 μžˆλ‹€
- DB 컀λ„₯μ…˜ 수λ₯Ό μ œν•œν•¨μœΌλ‘œμ¨ κ³Όλ„ν•œ 접속을 방지할 수 μžˆλ‹€
- DB 접속 λͺ¨λ“ˆμ„ κ³΅ν†΅ν™”ν•˜μ—¬ DB μ„œλ²„ ν™˜κ²½μ΄ λ³€ν•  경우 비ꡐ적 μ‰¬μš΄ μœ μ§€λ³΄μˆ˜κ°€ κ°€λŠ₯ν•˜λ‹€
- 연결이 λλ‚œ 컀λ„₯μ…˜μ„ μž¬μ‚¬μš©ν•¨μœΌλ‘œμ¨ μƒˆλ‘œ 객체λ₯Ό μƒμ„±ν•˜λŠ” λΉ„μš©μ„ 쀄일 수 μžˆλ‹€

## Hikari CP
- λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²°(Connection)을 관리해 μ£ΌλŠ” 도ꡬ(라이브러리)
- 컀λ„₯μ…˜ ν’€(Connection Pool)μ΄Β μ„€μ •λœ 컀λ„₯μ…˜μ˜ μ‚¬μ΄μ¦ˆλ§ŒνΌμ˜ 연결을 ν—ˆμš©ν•˜λ©° HTTP μš”μ²­μ— λŒ€ν•΄ 순차적으둜 DB 컀λ„₯μ…˜μ„ μ²˜λ¦¬ν•΄ μ£ΌλŠ” κΈ°λŠ₯을 μˆ˜ν–‰ν•œλ‹€

- μžλ°”: 기본적으둜 `DataSource`Β μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•˜μ—¬ 컀λ„₯μ…˜ 풀을 κ΄€λ¦¬ν•œλ‹€
- μŠ€ν”„λ§: SpringBoot 2.0 μ΄μ „μ—λŠ”Β tomcat-jdbcλ₯Ό μ‚¬μš©ν•˜λ‹€κ°€ 2.0 이후 λΆ€ν„°λŠ”Β Hikari CPλ₯Ό κΈ°λ³Έ μ˜΅μ…˜μœΌλ‘œ μ±„νƒν•œλ‹€
- Hikari CP μ„±λŠ₯이 μ›”λ“±νžˆ μ’‹λ‹€

![image](https://github.com/user-attachments/assets/02ea8f4a-20e4-4b9c-966c-b49df0e028bf)

### Hikari CP의 λ™μž‘ 방식
1. Threadκ°€ Connection을 μš”μ²­ν•˜λ©΄ Connection Pool의 각자의 방식에 따라 유휴 Connection을 μ°Ύμ•„μ„œ λ°˜ν™˜ν•œλ‹€
- Hikari CP의 경우, 이전에 μ‚¬μš©ν–ˆλ˜ Connection이 μ‘΄μž¬ν•˜λŠ”μ§€ ν™•μΈν•˜κ³ , 이λ₯Ό μš°μ„ μ μœΌλ‘œ λ°˜ν™˜ν•œλ‹€

![image](https://github.com/user-attachments/assets/3ccc54a0-de34-4bb1-a375-bd8bc8508972)

2. κ°€λŠ₯ν•œ Connection이 μ‘΄μž¬ν•˜μ§€ μ•ŠμœΌλ©΄, HandOffQueueλ₯Ό Pollingν•˜λ©΄μ„œ λ‹€λ₯Έ Threadκ°€ Connection을 λ°˜λ‚©ν•˜κΈ°λ₯Ό κΈ°λ‹€λ¦°λ‹€. λ§Œμ•½ TimeOut μ‹œκ°„μ΄ 만료되면 μ˜ˆμ™Έλ₯Ό λ˜μ§„λ‹€

![image](https://github.com/user-attachments/assets/53123ad7-5d56-426b-9a3b-0678af76e9f2)

3. μ΅œμ’…μ μœΌλ‘œ μ‚¬μš©ν•œ Connection을 λ°˜λ‚©ν•˜λ©΄ Connection Pool이 Connection μ‚¬μš© 내역을 κΈ°λ‘ν•˜κ³ , HandOffQueue에 λ°˜λ‚©λœ Connection을 μ‚½μž…ν•œλ‹€. HandOffQueueλ₯Ό Pollingν•˜λ˜ ThreadλŠ” Connection을 νšλ“ν•˜κ³  μž‘μ—…μ„ μ΄μ–΄λ‚˜κ°„λ‹€

![image](https://github.com/user-attachments/assets/51b8f94a-bb0c-416c-b837-a8657dc14168)

## λ°λ“œλ½ ν”Όν•˜κΈ°
- 컀λ„₯μ…˜ 풀을 크게 μ„€μ •ν•˜λ©΄? β†’ λ©”λͺ¨λ¦¬ μ†Œλͺ¨κ°€ 큰 λŒ€μ‹  λ§Žμ€ μ‚¬λžŒμ˜ λŒ€κΈ°μ‹œκ°„μ΄ 쀄어 λ“ λ‹€
- 컀λ„₯μ…˜ 풀을 μž‘κ²Œ μ„€μ •ν•˜λ©΄? β†’ μ‚¬μš©μžμ˜ λŒ€κΈ°μ‹œκ°„μ΄ 길어진닀

- 컀λ„₯μ…˜ ν’€ μ‚¬μ΄μ¦ˆλ₯Ό 지정할 λ•ŒλŠ” 항상 λ°λ“œλ½μ„ κ³ λ €ν•΄μ•Ό ν•œλ‹€
- [μš°μ•„ν•œ ν˜•μ œλ“€](https://techblog.woowahan.com/2664/)μ—μ„œ μ œμ‹œν•œ 이둠적으둜 ν•„μš”ν•œ μ΅œμ†Œν•œμ˜ 컀λ„₯μ…˜ ν’€ μ‚¬μ΄μ¦ˆλŠ” λ‹€μŒκ³Ό κ°™λ‹€
```
PoolSize = Tn Γ— ( Cm -1 ) + 1
- Tn : 전체 Thread 갯수
- Cm : ν•˜λ‚˜μ˜ Taskμ—μ„œ λ™μ‹œμ— ν•„μš”ν•œ Connection 수
```

- μœ„μ˜ κ²½μš°λŠ” λ°λ“œλ½μ€ ν”Όν•  수 μžˆμ§€λ§Œ, μ—¬μœ  컀λ„₯μ…˜μ΄ +1 개 μ΄λ―€λ‘œ μ„±λŠ₯상 쒋지 λͺ»ν•  수 μžˆλ‹€. 컀λ„₯μ…˜ 풀에 λ”μš± μ—¬μœ λ₯Ό μ£Όκ³  μ‹Άλ‹€λ©΄ μ•„λž˜μ™€ 같이 ν•΄λ³΄μž
```
PoolSize = Tn Γ— ( Cm - 1 ) + ( Tn / 2 )
- thread count : 16
- simultaneous connection count : 2
- pool size : 16 * ( 2 – 1 ) + (16 / 2) = 24
```

## ν™˜κ²½ μ„€μ •
1. μ˜μ‘΄μ„± μΆ”κ°€
```java
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
}
```

2. μ„€μ • μΆ”κ°€
```java
@Configuration
public class DataSourceConfiguration {

@Bean
@Primary
@ConfigurationProperties("spring.datasource")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}

@Bean
@ConfigurationProperties("spring.datasource.hikari")
public HikariDataSource dataSource(DataSourceProperties properties) {
return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
}
```

3. property μ •μ˜
```yaml
spring:
datasource:
url: jdbc:mysql://${MYSQL_URL}:${MYSQL_PORT}/${MYSQL_SCHEMA}
driver-class-name: com.mysql.cj.jdbc.Driver
username: ${MYSQL_USERNAME}
password: ${MYSQL_PASSWORD}
hikari:
connection-timeout: 3000
validation-timeout: 3000
minimum-idle: 5
max-lifetime: 240000
maximum-pool-size: 20
```
- `maximum-pool-size`: μ΅œλŒ€ pool size (defailt 10)
- `connection-timeout`: 컀λ„₯μ…˜μ˜ νƒ€μž„μ•„μ›ƒ
- `validation-timeout`: μœ νš¨ν•œ νƒ€μž„μ•„μ›ƒ
- `minimum-idle`: μ—°κ²° ν’€μ—μ„œ HikariCPκ°€ μœ μ§€ κ΄€λ¦¬ν•˜λŠ” μ΅œμ†Œ 유휴 μ—°κ²° 수
- `idle-timeout`: 연결을 μœ„ν•œ μ΅œλŒ€ 유휴 μ‹œκ°„
- `max-lifetime`: λ‹«νžŒ ν›„ pool μ—μžˆλŠ” connection의 μ΅œλŒ€ 수λͺ… (ms)μž…λ‹ˆλ‹€.
- `auto-commit`: auto commit μ—¬λΆ€ (default true)

4. Hikari Pool이 μ œλŒ€λ‘œ λ™μž‘ν•˜κ³  μžˆλŠ”μ§€ λ‘œκΉ… 레벨 μ„€μ •
```yaml
logging:
level:
org.hibernate.SQL: info
root: info
com.zaxxer.hikari.pool.HikariPool: debug
```

5. μ‹€ν–‰ κ²°κ³Ό
![image](https://github.com/user-attachments/assets/ab18fd8c-f5f6-4b7e-9964-0afd0111d2fc)

0 comments on commit 47faa4b

Please sign in to comment.