PHpullh
학습 라이브러리/Java/커넥션 풀 튜닝

JAVA · 성능/JVM

커넥션 풀 튜닝

데이터베이스 커넥션 풀의 최적 크기와 모니터링 전략입니다.

성능/JVM고급ConnectionPool튜닝HikariCP리틀법칙

핵심 설명

데이터베이스 커넥션 풀의 최적 크기와 모니터링 전략입니다.

Java code

// 커넥션 풀 최적화 전략

public class ConnectionPoolTuning {
    public static void main(String[] args) {
        int cpuCores = Runtime.getRuntime().availableProcessors();

        // 최적 풀 크기 공식 (PostgreSQL 권장)
        // connections = (core_count * 2) + effective_spindle_count
        int optimalSize = cpuCores * 2 + 1;
        System.out.printf("CPU 코어: %d, 최적 풀 크기: %d%n", cpuCores, optimalSize);

        // HikariCP 권장 설정
        System.out.println("\n=== HikariCP 권장 설정 ===");
        System.out.println("maximumPoolSize: " + optimalSize);
        System.out.println("minimumIdle: " + optimalSize); // 고정 크기 권장
        System.out.println("connectionTimeout: 30000ms");
        System.out.println("idleTimeout: 600000ms (10분)");
        System.out.println("maxLifetime: 1800000ms (30분)");
        System.out.println("leakDetectionThreshold: 60000ms (1분)");

        // 모니터링 지표
        System.out.println("\n=== 모니터링 지표 ===");
        System.out.println("활성 커넥션 수 (active)");
        System.out.println("유휴 커넥션 수 (idle)");
        System.out.println("대기 스레드 수 (pending)");
        System.out.println("커넥션 획득 시간 (acquisition time)");
        System.out.println("커넥션 사용 시간 (usage time)");
        System.out.println("타임아웃 발생 횟수 (timeout count)");

        // 리틀의 법칙
        double avgResponseTime = 0.1; // 100ms
        double requestRate = 50; // 초당 50 요청
        double requiredConnections = avgResponseTime * requestRate;
        System.out.printf("\n리틀의 법칙: %.0f req/s * %.1fs = %.0f 커넥션 필요%n",
            requestRate, avgResponseTime, requiredConnections);
    }
}

학습 팁

leakDetectionThreshold를 설정하면 반환하지 않은 커넥션을 감지합니다. 개발 환경에서 1분, 운영에서 5분이 적당합니다.

주의할 점

커넥션 풀을 100으로 설정하는 것은 거의 항상 잘못입니다. 데이터베이스 측 최대 연결 수와 맞춰야 하며, 보통 10-20이 최적입니다.

자주 묻는 질문

커넥션 풀 튜닝란 무엇인가요?

데이터베이스 커넥션 풀의 최적 크기와 모니터링 전략입니다.

커넥션 풀 튜닝 학습 시 주의할 점은 무엇인가요?

커넥션 풀을 100으로 설정하는 것은 거의 항상 잘못입니다. 데이터베이스 측 최대 연결 수와 맞춰야 하며, 보통 10-20이 최적입니다.