연결 풀과 자바에서 스레드 풀 설정

hguser :

히카리 풀을 사용하여 Spring 애플리케이션.

결과 함께 지금은 질의 10 표를 가지고있는 클라이언트에서 단일 요청 (비즈니스 필요), 다음 합성. 그리고 각 테이블에 대해 쿼리은 200 밀리 초에 50ms의 비용을 수 있습니다. 응답 시간을 단축하기 위해, 나는를 만드는 FixedThreadPool다른 스레드 (의사)의 각 테이블을 쿼리 내 서비스 :

class MyService{
    final int THREAD_POOL_SIZE = 20;
    final int CONNECTION_POOL_SIZE = 10;


    final ExecutorService pool = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
    protected DataSource ds;


    MyClass(){
        Class.forName(getJdbcDriverName());
        HikariConfig config = new HikariConfig();
        config.setMaximumPoolSize(CONNECTION_POOL_SIZE);
        ds = new HikariDataSource(config);
    }



    public Items doQuery(){
        String[] tables=["a","b"......]; //10+ tables
        Items result=new Items();
        CompletionService<Items> executorService = new ExecutorCompletionService<Items>(pool);
        for (String tb : tables) {
            Callable<Item> c = () -> {
                Items items = ds.getConnection().query(tb); ......
                return Items;
            };
            executorService.submit(c);
        }


        for (String tb: tables) {
            final Future<Items> future = executorService.take();
            Items items = future.get();
            result.addAll(items);
        }
    }
}

이제 하나의 요청에 대한 평균 응답 시간은 어쩌면은 500ms.

여기에 이미지 설명을 입력

그러나 동시 요청에 대한 평균 응답 시간이 빠른 속도로 더 많은 요청을 증가, 긴 응답 시간이 될 것입니다.

여기에 이미지 설명을 입력

나는 응용 프로그램 작업을 적용하려면 적절한 연결 풀 크기와 스레드 풀 크기를 설정하는 방법을 궁금해?

BTW, the database use RDS in cloud with 4 cpu 16GB mem, 2000 max connections and 8000 max IOPS.

skott :

You might want to think about a few more parameters:
1. Max concurrent request parameter for the database. Cloud providers have different limits of concurrent requests for different tiers, you might want to check yours.

2. When you say 50-200 ms, although it is difficult to say, are there 8 requests of 50ms and 2 requests of 200ms on an average or all of them pretty much the same? Why? Your doQuery might be limited by the query taking maximum time (which is 200ms), but the threads taking 50 ms will get released after it's task is done making them available for next set of requests.

3. What is the QPS you are expecting to receive?

Some calculations: If a single request takes 10 threads, and you have provisioned 100 connections with 100 concurrent query limit, assuming 200ms for each query, you can only handle 10 requests at a time. Maybe a little better than 10 if most queries take 50ms or so (but I wouldn't be optimistic).

Of course, some of these calculations goes for a toss if any of your queries takes >200ms (network latency or anything else) , in which case I recommend you have a circuit breaker, either at the connection end (if you are allowed to abort the query after a timeout) or at the API end.

Note : max connection limit is not the same as max concurrent query limit.

제안 : 당신은 500ms에서 응답을 필요로하기 때문에, 당신은 또한 풀 100-150ms 정도의은 ConnectionTimeout을 가질 수 있습니다. 최악의 경우 : 응용 프로그램 처리 응답에 대한 <500ms로에 대한 150ms의 연결 시간 초과 + 200ms의 쿼리 실행은 + 100ms로. 공장.

추천

출처http://43.154.161.224:23101/article/api/json?id=312920&siteId=1