๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Spring

DB ์ปค๋„ฅ์…˜ ํ’€(Connection Pool)๊ณผ Hikari CP

by dkswnkk 2023. 1. 18.

์„œ๋ก 

HikariCP๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐ„์˜ ์—ฐ๊ฒฐ์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ ์žˆ์–ด, ๊ฐ€์žฅ ๋น ๋ฅด๊ณ  ๊ฐ€๋ฒผ์šด Connection Pool ์˜คํ”ˆ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ฒŒ์‹œ๊ธ€์—์„œ๋Š” Connection Pool๊ณผ Hikari CP์— ๋Œ€ํ•ด ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ชฉ์ฐจ

  1. ์ „ํ†ต์ ์ธ WAS์™€ DB ์—ฐ๊ฒฐ ๋ฐฉ๋ฒ•
  2. Connection Pool์ด๋ž€?
  3. Connection Pool์„ ๋ฌด์ž‘์ • ํฌ๊ฒŒ ๋งŒ๋“ค๋ฉด ์„ฑ๋Šฅ์ด ์ข‹์•„์งˆ๊นŒ?
  4. ์ด์ƒ์ ์ธ Connection Pool์˜ ํฌ๊ธฐ๋Š” ์–ผ๋งˆ์ผ๊นŒ?
  5. Hikari CP๋ž€?

 

์ „ํ†ต์ ์ธ WAS์™€ DB ์—ฐ๊ฒฐ ๋ฐฉ๋ฒ•

์ „ํ†ต์ ์ธ WAS์™€ DB ์—ฐ๊ฒฐ ํ”Œ๋กœ์šฐ

์ผ๋ฐ˜์ ์œผ๋กœ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ CRUD์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ์‚ฐ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์œ„ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด DB์— ์ง์ ‘ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋งค๋ฒˆ ๋“œ๋ผ์ด๋ฒ„(Driver)๋ฅผ ๋กœ๋“œํ•˜๊ณ  connection ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๋ฐฉ์‹์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;


public class Test {

    public static void main(String[] args) {
    
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        Connection conn = null;
        Statement statement = null;
        String sql = "";
        
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, "master", "master");
            statement = conn.createStatement();
            sql = "select * from Table";
            statement.execute(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }
}

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์กฐ๊ธˆ ๋” ๋น ๋ฅด๊ฒŒ ์ดํ•ด๊ฐ€ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด sql์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋“œ๋ผ์ด๋ฒ„(Driver)๋ฅผ ๋กœ๋“œํ•˜๊ณ  connection ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ง€๊ธˆ์ด์•ผ ์ฟผ๋ฆฌ๊ฐ€ ํ•˜๋‚˜๋ผ ํฌ๊ฒŒ ๋ฌธ์ œ๊ฐ€ ๋˜๋‚˜ ์‹ถ์ง€๋งŒ ๋งค๋ฒˆ ๋ชจ๋“  ์š”์ฒญ๋งˆ๋‹ค ์œ„ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”?

์ผ๋ฐ˜์ ์œผ๋กœ WAS(Web Application Server)์™€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ ์—ฐ๊ฒฐ์—๋Š” ๋งŽ์€ ๋น„์šฉ์ด ๋“ญ๋‹ˆ๋‹ค. ๊ณต์‹๋ฌธ์„œ๋ฅผ ๋ณด๋ฉด MySQL 8.0 ๋ฒ„์ „์„ ๊ธฐ์ค€์œผ๋กœ INSERT๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜๋Š”๋ฐ ๋“œ๋Š” ๋น„์šฉ์˜ ๋น„์œจ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • Connecting: (3)
  • Sending query to server: (2)
  • Parsing query: (2)
  • Inserting row: (1 × size of row)
  • Inserting indexes: (1 × number of indexes)
  • Closing: (1)

๊ฐ ๊ด„ํ˜ธ์˜ ์ˆซ์ž๋Š” ์ƒ๋Œ€์ ์ธ ๋น„์šฉ์˜ ๋Œ€๋žต์ ์ธ ๋น„์œจ์„ ์˜๋ฏธํ•˜๋ฉฐ, ์ž˜ ๋ณด๋ฉด Connecting ์ฆ‰ ์„œ๋ฒ„์™€ DB๊ฐ€ ์—ฐ๊ฒฐํ•˜๋Š”๋ฐ ๊ฐ€์žฅ ํฐ ๋น„์šฉ์ด ๋“œ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋น„์šฉ ์ค‘์—์„œ๋„ ์—ฐ๊ฒฐ์ด ๊ฐ€์žฅ ๋น„์œจ์ด ํฐ ์ด์œ  ์ค‘ ํ•˜๋‚˜๋Š” Database๋Š” ์ตœ์ดˆ ์ ‘์† ์‹œ TCP ํ†ต์‹ ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.(๋„คํŠธ์›Œํฌ ๋ ˆ๋ฒจ์ด๋ผ ๊ฐ„๋‹จํ•˜๊ฒŒ๋งŒ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.)

3-way-handshake ๊ณผ์ •

TCP๋กœ ํ†ต์‹ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ๋“ค์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๊ธฐ ์ „์— ์ •ํ™•ํ•œ ์ „์†ก์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ์œ„ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด 3-way-handshake๋ผ๋Š” ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค.

4-way-handshake ๊ณผ์ •

๋˜ํ•œ ๋ฐ์ดํ„ฐ์˜ ์ „์†ก์ด ์ข…๋ฃŒ๋˜๋ฉด ์‚ฌ์šฉํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•œ 4-way-handshake๊ณผ์ •๋„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๋ชจ๋“  ๊ณผ์ •์„ ์š”์•ฝํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ์ง์€ DB ๋“œ๋ผ์ด๋ฒ„๋ฅผ ํ†ตํ•ด ์ปค๋„ฅ์…˜์„ ์กฐํšŒํ•œ๋‹ค.
  2. DB ๋“œ๋ผ์ด๋ฒ„๋Š” DB์™€ TCP/IP ์ปค๋„ฅ์…˜์„ ์—ฐ๊ฒฐํ•œ๋‹ค. ๋ฌผ๋ก  ์ด ๊ณผ์ •์—์„œ 3-way-handshake ๊ฐ™์€ TCP/IP ์—ฐ๊ฒฐ์„ ์œ„ํ•œ ๋„คํŠธ์›Œํฌ ๋™์ž‘์ด ๋ฐœ์ƒํ•œ๋‹ค.
  3. DB ๋“œ๋ผ์ด๋ฒ„๋Š” TCP/IP ์ปค๋„ฅ์…˜์ด ์—ฐ๊ฒฐ๋˜๋ฉด, ID, PW์™€ ๊ธฐํƒ€ ๋ถ€๊ฐ€์ •๋ณด๋ฅผ DB์— ์ „๋‹ฌํ•œ๋‹ค.
  4. DB๋Š” ID, PW๋ฅผ ํ†ตํ•ด ๋‚ด๋ถ€ ์ธ์ฆ์„ ์™„๋ฃŒํ•˜๊ณ  ๋‚ด๋ถ€์— DB ์„ธ์…˜์„ ์ƒ์„ฑํ•œ๋‹ค.
  5. DB๋Š” ์ปค๋„ฅ์…˜ ์ƒ์„ฑ์ด ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋Š” ์‘๋‹ต์„ ๋ณด๋‚ธ๋‹ค.
  6. DB ๋“œ๋ผ์ด๋ฒ„๋Š” ์ปค๋„ฅ์…˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ํด๋ผ์ด์–ธํŠธ์— ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ „ํ†ต์ ์ธ ๋ฐฉ๋ฒ•์€ ์ปค๋„ฅ์…˜์„ ์ƒˆ๋กœ ๋งŒ๋“œ๋Š” ๊ณผ์ •๊ณผ DB๋Š” ๋ฌผ๋ก ์ด๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„์—์„œ๋„ TCP/IP ์ปค๋„ฅ์…˜์„ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ๋งค๋ฒˆ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ธฐ์— ์‹ค์ œ ์„œ๋น„์Šค์˜ ์‘๋‹ต์†๋„์— ์˜ํ–ฅ์„ ๋งŽ์ด ๋ผ์น˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ด๋Ÿฌํ•œ ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด Connection Pool์ด๋ž€ ๊ฐœ๋…์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค.

 

Connection Pool์ด๋ž€?

WAS(์›น ์ปจํ…Œ์ด๋„ˆ)๊ฐ€ ์‹คํ–‰๋  ๋•Œ DB์—ฐ๊ฒฐ์„ ์œ„ํ•ด ๋ฏธ๋ฆฌ ์ผ์ •์ˆ˜์˜ connection ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด Pool์— ๋‹ด์•„ ๋’€๋‹ค๊ฐ€ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์ด ๋ฐœ์ƒํ•˜๋ฉด Pool์—์„œ ์ƒ์„ฑ๋˜์–ด ์žˆ๋Š” Connection ๊ฐ์ฒด๋ฅผ ๋„˜๊ฒจ์ฃผ๊ณ  ์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚˜๋ฉด Connection ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ Pool์— ๋ณด๊ด€ํ•˜๋Š” ๋ฐฉ์‹์„ ๋œปํ•ฉ๋‹ˆ๋‹ค.

Connection Pool ํ”Œ๋กœ์šฐ

์ฆ‰, ์ปค๋„ฅ์…˜ ํ’€์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐ๋œ ์ปค๋„ฅ์…˜์„ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด pool์— ๋ณด๊ด€ํ•œ ๋‹ค์Œ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ์ปค๋„ฅ์…˜ ํ’€์˜ ์ปค๋„ฅ์…˜์„ ์ด์šฉํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. 

Connection ํ’€์„ ์‚ฌ์šฉํ•ด์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ด์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1. ์„œ๋ฒ„์˜ ๋ถ€ํ•˜๋ฅผ ์ค„์—ฌ์ค€๋‹ค.

Java์—์„œ๋Š” DB Connection์„ ๋งบ๋Š” ๊ณผ์ •์ด ๋ถ€ํ•˜๊ฐ€ ๋งŽ์ด ๊ฑธ๋ฆฌ๋Š” ์ž‘์—…์ด๋ผ ๋™์‹œ์— ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด DB ์ปค๋„ฅ์…˜์„ ์š”๊ตฌํ•œ๋‹ค๋ฉด ์ตœ์•…์˜ ๊ฒฝ์šฐ ์„œ๋ฒ„๊ฐ€ ์ฃฝ์–ด๋ฒ„๋ฆฌ๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋ฏธ๋ฆฌ Connection์„ ์ƒ์„ฑํ•˜๊ณ  ์žฌํ™œ์šฉํ•˜์—ฌ ์„œ๋ฒ„์— ๋ถ€ํ•˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2. ์„œ๋ฒ„์˜ ํ•œ์ •์ ์ธ ์ž์›์„ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์„œ๋ฒ„๋Š” ํ•œ์ •์ ์ธ ์ž์›์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ ์š”์ฒญ์ด ์˜ฌ ๋•Œ๋งˆ๋‹ค ๋ฌด์ œํ•œ์˜ Connection์„ ์ƒ์„ฑํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ์ „์ฒด ์‹œ์Šคํ…œ์— ์„ฑ๋Šฅ์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ปค๋„ฅ์…˜ํ’€์— ๋ฏธ๋ฆฌ ์ •ํ•ด์ง„ ์ˆซ์ž์˜ Connection๋งŒ ์ƒ์„ฑํ•˜๋„๋ก ์ œํ•œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ Connection Pool์„ ๋ฌด์ž‘์ • ํฌ๊ฒŒ ๋งŒ๋“ค๋ฉด ์„ฑ๋Šฅ์ด ์ข‹์•„์งˆ๊นŒ? ํ•˜๋Š” ์˜๋ฌธ์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Connection Pool์„ ๋ฌด์ž‘์ • ํฌ๊ฒŒ ๋งŒ๋“ค๋ฉด ์„ฑ๋Šฅ์ด ์ข‹์•„์งˆ๊นŒ?

๊ฒฐ๋ก ๋ถ€ํ„ฐ ๋งํ•˜๋ฉด ๋ฌด์ž‘์ • Connection Pool์„ ๋Š˜๋ฆฐ๋‹ค๊ณ  ์„ฑ๋Šฅ์ด ์ข‹์•„์ง€์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค.

๋จผ์ € WAS์™€ DB์˜ ์—ฐ๊ฒฐ์€ thread๊ฐ€ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  thread ๋˜ํ•œ thread pool์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ Thread Pool์˜ ํฌ๊ธฐ๋ณด๋‹ค Connection Pool์˜ ํฌ๊ธฐ๊ฐ€ ๋” ํฌ๋ฉด ๋ฉ”๋ชจ๋ฆฌ์ƒ์—์„œ ๋‚จ์€ Connection์€ ์ž‘์—…์„ ํ•˜์ง€ ๋ชปํ•˜๊ณ  ๋†€๊ฒŒ ๋˜๊ธฐ์—, ์‹ค์ง์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋งŒ ์ฐจ์ง€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Connection Pool์„ ํฌ๊ฒŒ ํ•˜๋”๋ผ๋„ thead pool์˜ ํฌ๊ธฐ๋ณด๋‹ค ์ปค๋ฒ„๋ฆฌ๋ฉด ๋†€๊ฒŒ ๋˜๋Š” Connection Pool์ด ๋งŽ์•„์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ž์›์„ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

๊ทธ๋ ‡๋‹ค๊ณ  ๋ฌด์ž‘์ • thread pool์˜ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฐ๋‹ค๊ณ  ํ•ด๊ฒฐ๋˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. thread์˜ ์ฆ๊ฐ€๋Š” ๋ฌธ๋งฅ๊ตํ™˜(Context Switching)์œผ๋กœ ์ธํ•œ ํ•œ๊ณ„๊ฐ€ ์กด์žฌํ•˜๊ณ , Connection Pool์„ ๋Š˜๋ ค์„œ ๋งŽ์€ Thread๋ฅผ ๋ฐ›์•˜๋”๋ผ๋„ ๋‹ค์–‘ํ•œ ์›์ธ์œผ๋กœ ์„ฑ๋Šฅ์ ์ธ ํ•œ๊ณ„๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 

๋จผ์ €, Disk ๊ฒฝํ•ฉ ์ธก๋ฉด์—์„œ ์„ฑ๋Šฅ์ ์ธ ํ•œ๊ณ„๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ํ•˜๋“œ ๋””์Šคํฌ ํ•˜๋‚˜๋‹น ํ•˜๋‚˜์˜ I/O๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— Blocking์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. SSD๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ผ๋ถ€ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ด ๋˜ํ•œ ํ•œ๊ณ„๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํŠน์ • ์‹œ์ ๋ถ€ํ„ฐ๋Š” ์„ฑ๋Šฅ์ ์ธ ์ฆ๊ฐ€๊ฐ€ Disk ๋ณ‘๋ชฉ์œผ๋กœ ์ธํ•ด ๋ฏธ๋น„ํ•ด์ง‘๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ๋„ Context Switiching์œผ๋กœ ์ธํ•œ ์„ฑ๋Šฅ์ ์ธ ํ•œ๊ณ„๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

Context Switching์— ๋Œ€ํ•ด ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด๋ฉด ์šฐ๋ฆฌ๋Š” ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ CPU๊ฐ€ ์ˆ˜ ์‹ญ, ์ˆ˜ ๋ฐฑ ๊ฐœ์˜ Thread๋ฅผ ๋™์‹œ์— ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ CPU ์ฝ”์–ด๋Š” CPU์Šค์ผ€์ค„๋ง ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ๋™์‹œ์— ํ•˜๋‚˜์˜ ์ž‘์—…๋งŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋น ๋ฅด๊ฒŒ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž‘์—…์„ context-swithing ํ•˜๋ฉฐ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ‘๋™์‹œ์—’ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” ๊ฒƒ๋ฟ์ž…๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ์˜ ์ž‘์—…์ด ๋๋‚œ ๋‹ค์Œ์˜ Thread์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด context-switching์ด ์ผ์–ด๋‚˜๋ฉฐ ์ด ์ˆœ๊ฐ„ ์ž‘์—…์— ํ•„์š”ํ•œ Thread์˜ Stack ์˜์—ญ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๋“ฑ ์ถ”๊ฐ€์ ์ธ ์ž‘์—…์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

[OS] CPU ์Šค์ผ€์ค„๋ง(CPU Scheduling)

CPU ์Šค์ผ€์ค„๋ง(CPU Scheduling) CPU ์Šค์ผ€์ค„๋ง์€ ๋‹ค์ค‘ ํ”„๋กœ๊ทธ๋žจ ์šด์˜์ฒด์ œ์˜ ๊ธฐ๋ณธ์ž…๋‹ˆ๋‹ค. ์šด์˜์ฒด์ œ๋Š” CPU๋ฅผ ํ”„๋กœ์„ธ์Šค ๊ฐ„์— ๊ตํ™˜ํ•จ์œผ๋กœ์จ, ์ปดํ“จํ„ฐ๋ฅผ ๋ณด๋‹ค ์ƒ์‚ฐ์ ์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๊ธฐ๋ณธ์ ์ธ ์Šค์ผ€์ค„

dkswnkk.tistory.com

CPU ์Šค์ผ€์ค„๋ง์— ๋Œ€ํ•ด์„œ๋Š” ์œ„ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•ด ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

Thread Pool๊ณผ Connection Pool์˜ ๊ด€๊ณ„

  1. thread๋Š” Connection Pool์—์„œ ์œ ํœด ์ƒํƒœ์ธ DB Connection์ด ์กด์žฌํ•˜๋ฉด ์‚ฌ์šฉํ•˜๊ณ , ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  2. ๊ทธ๋ฆฌ๊ณ  DB Connection์„ ์ด์šฉํ•ด์„œ DB์—์„œ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•œ ๋’ค ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰, ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • Connection pool์— ๋ฌด์กฐ๊ฑด ๋งŽ์€ Connection๋“ค์„ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋ฉด, Connection ์ž์ฒด๋„ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋งŒํผ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค. (์„ฑ๋Šฅ ์ €ํ•˜)
  • ๋ฐ˜๋Œ€๋กœ Connetion pool์— Connection ๊ฐ์ฒด๊ฐ€ ๋„ˆ๋ฌด ์ ์œผ๋ฉด ๋งŽ์€ ์œ ์ €๋“ค์ด ์‚ฌ์šฉํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฒฝ์šฐ ์‚ฌ์šฉ ์ค‘์ธ Connection์ด ๋ฐ˜๋‚ฉ๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐํ•ด์•ผ ํ•˜๋Š” ์‹œ๊ฐ„์ด ๊ธธ์–ด์ง€๊ฒŒ ๋œ๋‹ค. (์„ฑ๋Šฅ ์ €ํ•˜)
  • ๊ทธ๋ ‡๋‹ค๊ณ  ๋ฌด์ž‘์ • Thread Pool์„ ๋Š˜๋ ค๋„ Context Switiching์œผ๋กœ ์ธํ•œ ํ•œ๊ณ„๊ฐ€ ์กด์žฌํ•œ๋‹ค.

 

์ด์ƒ์ ์ธ Connection Pool์˜ ํฌ๊ธฐ๋Š” ์–ผ๋งˆ์ผ๊นŒ?

๊ทธ๋ ‡๋‹ค๋ฉด ๊ณผ์—ฐ ๊ฐ€์žฅ ์ด์ƒ์ ์ธ Connection Pool์˜ ํฌ๊ธฐ๋Š” ์–ผ๋งˆ์ผ๊นŒ์š”? 

MySQL์˜  ๊ณต์‹๋ฌธ์„œ

๋จผ์ € MySQL์˜ ๊ณต์‹๋ฌธ์„œ์—์„œ๋Š” ์œ„์™€ ๊ฐ™์ด 600๋ช…์˜ ์œ ์ €์— ๋Œ€ํ•ด์„œ 15~20๊ฐœ์˜ connection poolํฌ๊ธฐ๊ฐ€ ์ ๋‹นํ•˜๋‹ค๊ณ  ์–ธ๊ธ‰๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ด์ƒ๋ถ€ํ„ฐ๋Š” ์ ์ ˆํ•˜๊ฒŒ ๋ถ€ํ•˜ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉฐ ์ ๋‹นํ•œ ๊ฐ’์„ ์ง์ ‘ ์ฐพ๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์šฐ์•„ํ•œํ–ฅ์ œ๋“ค ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ

๋‹ค์Œ์œผ๋กœ ์šฐ์•„ํ•œํ˜•์ œ๋“ค์˜ ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ์—์„œ๋Š” ์œ„์™€ ๊ฐ™์€ pool size ๊ณต์‹์„ ์–ธ๊ธ‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  • Tn : ์ „์ฒด Thread ๊ฐœ์ˆ˜
  • Cm : ํ•˜๋‚˜์˜ Task์—์„œ ๋™์‹œ์— ํ•„์š”ํ•œ Connection ์ˆ˜

์œ„ ๊ณต์‹์€ HikariCP wiki๊ฐ€ ์›๋ณธ ์ถœ์ฒ˜์ด๋ฉฐ, ์ด ๊ณต์‹๋Œ€๋กœ Maximum pool size๋ฅผ ์„ค์ •ํ•˜๋ฉด Dead lock์„ ํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

Hikari CP๋ž€?

๊ทธ๋ ‡๋‹ค๋ฉด ์ด์ œ Hikari CP๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?

HikariCP๋Š” 2012๋…„ ์ฒ˜์Œ ๊ฐœ๋ฐœ๋œ, 130kb์˜ ๊ฐ€๋ฒผ์šด ์šฉ๋Ÿ‰๊ณผ ๋น ๋ฅธ ์†๋„๋ฅผ ๊ฐ€์ง€๋Š” JDBC์˜ ์ปค๋„ฅ์…˜ ํ’€ ์˜คํ”ˆ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ JDBC DataSource์˜ ๊ตฌํ˜„์ฒด์ž…๋‹ˆ๋‹ค.

 

DataSource์˜ ์œ„์น˜

๋Œ€ํ‘œ์ ์ธ ์ปค๋„ฅ์…˜ ํ’€ ์˜คํ”ˆ์†Œ์Šค๋กœ๋Š” common-dbcp2, tomcat-jdbc pool, HikariCP ๋“ฑ์ด ์žˆ์œผ๋ฉฐ, Spring Boot 2.0 ์ด์ „ ๋ฒ„์ „๊นŒ์ง€๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ connection pool์„ tomcat-jdbc pool์„ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ 2.0 ๋ฒ„์ „ ์ดํ›„๋ถ€ํ„ฐ๋Š” HikariCP๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

HikariCP์˜ ์ƒ๋Œ€์  ์„ฑ๋Šฅ

HikariCP์˜ ๊นƒํ—ˆ๋ธŒ์— ๋‚˜์™€์žˆ๋Š” ๋‹ค๋ฅธ ์˜คํ”ˆ์†Œ์Šค์™€ ๋น„๊ตํ•œ HikariCP์˜ ์„ฑ๋Šฅ์ž…๋‹ˆ๋‹ค. ๊ต‰์žฅํžˆ ๋‹ค๋ฅธ ์˜คํ”ˆ์†Œ์Šค๋ณด๋‹ค ๋น ๋ฅธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

HikariCP๊ฐ€ ๋‹ค๋ฅธ CP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ณด๋‹ค ๋น ๋ฅธ ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์ ์€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰: HikariCP๋Š” ๋งค์šฐ ์ ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ดˆ๊ธฐํ™” ๋ฐ ์—ฐ๊ฒฐ ์‹œ๊ฐ„์ด ์งง๋‹ค.
  2. ๋†’์€ ์ฒ˜๋ฆฌ๋Ÿ‰: HikariCP๋Š” ๋งค์šฐ ๋น ๋ฅด๊ฒŒ ์—ฐ๊ฒฐ์„ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜๋ฆฌ๋Ÿ‰์ด ๋งค์šฐ ๋†’๋‹ค.
  3. ์ž‘์€ ์ฝ”๋“œ ๋ฒ ์ด์Šค: HikariCP๋Š” ์ฝ”๋“œ ๋ฒ ์ด์Šค๊ฐ€ ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌ์„ฑ์ด ๊ฐ„๋‹จํ•˜๊ณ  ๊ฐ€๋ณ๋‹ค.
  4. ํ’๋ถ€ํ•œ ๊ตฌ์„ฑ ์˜ต์…˜: HikariCP๋Š” ๋‹ค์–‘ํ•œ ๊ตฌ์„ฑ ์˜ต์…˜์„ ์ œ๊ณตํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ตœ์ ํ™”๋œ ์ปค๋„ฅ์…˜ ํ’€์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  5. ์Šค๋ ˆ๋“œ ์•ˆ์ „์„ฑ: HikariCP๋Š” ์Šค๋ ˆ๋“œ ์•ˆ์ „ํ•˜๊ฒŒ ์„ค๊ฒŒ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ์—์„œ ์•ˆ์ „ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Spring์—์„œ Hikari์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

spring: 
    datasource: 
    	hikari: 
            driver-class-name: com.mysql.cj.jdbc.Driver 
            username: {id} 
            password: {password} 
            jdbc-url: jdbc:mysql://{url}:{port}/{db์Šคํ‚ค๋งˆ} 
            minimum-idle: 5 
            maximum-pool-size: 10
            idle-timeout: 30000 
            pool-name: DevLogHikariCP 
            max-lifetime: 200000 
            connection-timeout: 30000 
            connection-test-query: /*CONNECTION TEST QUERY*/SELECT NOW() FROM DUAL

๊ฐ ์„ค์ •๊ฐ’์˜ ์˜๋ฏธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • minimum-idle: Connection Pool์— ์œ ์ง€ ๊ฐ€๋Šฅํ•œ ์ตœ์†Œ ์ปค๋„ฅ์…˜ ๊ฐœ์ˆ˜
  • maximum-pool-size: Connection Pool์— ์œ ์ง€ ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ์ปค๋„ฅ์…˜ ๊ฐœ์ˆ˜
  • idle-timeout: Connection์ด Poll์—์„œ ์œ ํœด์ƒํƒœ(์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ƒํƒœ)๋กœ ๋‚จ์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์‹œ๊ฐ„
  • pool-name: Connction Pool ์ด๋ฆ„
  • max-lifetime: Connection์˜ ์ตœ๋Œ€ ์œ ์ง€ ๊ฐ€๋Šฅ ์‹œ๊ฐ„
  • connection-timeout: Pool์—์„œ Connection์„ ๊ตฌํ•  ๋•Œ ๋Œ€๊ธฐ์‹œ๊ฐ„, ๋Œ€๊ธฐ์‹œ๊ฐ„์•ˆ์— ๊ตฌํ•˜์ง€ ๋ชปํ•˜๋ฉด Exception
  • connection-test-query: Connection์ด ์ž˜ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” TEST SQL

 

์ฐธ๊ณ 

  1. https://programmer93.tistory.com/74
  2. https://velog.io/@rlacksals96/%EB%84%A4%ED%8A%B8%EC%9B%8C%ED%81%AC-TCPUDP%EC%99%80-3way-handshake
  3. https://velog.io/@hyun6ik/%EC%BB%A4%EB%84%A5%EC%85%98-%ED%92%80-%EC%9D%B4%ED%95%B4
  4. https://1-7171771.tistory.com/119
  5. https://hyuntaeknote.tistory.com/12
  6. https://escapefromcoding.tistory.com/712

๋Œ“๊ธ€