79585568

Date: 2025-04-22 01:39:52
Score: 1.5
Natty:
Report link

I managed to resolve the high CPU usage issue when using WebFlux by configuring the R2DBC connection pool more carefully. Here's what helped in my case:

#in my DB Config class
#The code comments are in Korean

@Bean(name = "arhConnectionFactory")
  public ConnectionFactory arhConnectionFactory() {
    ConnectionFactory conn = ConnectionFactories.get(ConnectionFactoryOptions.builder()
        .option(ConnectionFactoryOptions.DRIVER, "pool") // pool 사용 (proxy로 감싸기 전)
        .option(ConnectionFactoryOptions.PROTOCOL, properties.getDriver()) // mariadb, mssql 등
        .option(ConnectionFactoryOptions.HOST, properties.getHost())
        .option(ConnectionFactoryOptions.PORT, properties.getPort())
        .option(ConnectionFactoryOptions.USER, properties.getUsername())
        .option(ConnectionFactoryOptions.PASSWORD, properties.getPassword())
        .option(ConnectionFactoryOptions.DATABASE, properties.getDatabase())

        // 커넥션 풀 관련 옵션
        .option(Option.valueOf("initialSize"), 1) // 초기 커넥션 수
        .option(Option.valueOf("maxSize"), 2) // 최대 커넥션 수
        .option(Option.valueOf("maxIdleTime"), Duration.ofSeconds(60)) // 유휴 커넥션 유지 시간
        .option(Option.valueOf("maxCreateConnectionTime"), Duration.ofSeconds(5)) // 커넥션 생성 제한 시간
        .option(Option.valueOf("maxAcquireTime"), Duration.ofSeconds(3)) // 커넥션 획득 제한 시간
        .option(Option.valueOf("acquireRetry"), 1) // 커넥션 획득 재시도 횟수
        .option(Option.valueOf("validationQuery"), "SELECT 1") // 유효성 검사 쿼리
        .option(Option.valueOf("backgroundEvictionInterval"), Duration.ofSeconds(120)) // 유휴 커넥션 정리 주기
        .option(Option.valueOf("name"), "arh-r2dbc-pool") // 커넥션 풀 이름
        .build()
    );

    return ProxyConnectionFactory.builder(conn)
        .onAfterQuery(queryInfo -> {
          log.debug("{}", new QueryExecutionInfoFormatter().showQuery().format(queryInfo));
          log.debug("{}", new QueryExecutionInfoFormatter().showBindings().format(queryInfo));
        })
        .build();
  }

BUT,There were two main tasks in my WebFlux server that caused a noticeable increase in CPU usage:

  1. Processing JSON parameters and storing them in the database

  2. Saving multiple files from a Flux<FilePart> into server storage

I was able to solve the first issue (database write logic for JSON data), and CPU usage has improved since then. However, the second issue—saving multiple files to disk—is still causing high CPU load.

Has anyone faced similar problems with Flux and file I/O in WebFlux? I'd appreciate any tips or advice on optimizing file write operations (e.g., writing to disk efficiently in a reactive context).

Reasons:
  • Blacklisted phrase (1): any tips
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: yangjinyoung