Ephemeral Port

fart man·2026년 3월 25일

k6 로 테스트를 하면서 VU가 많아지면 네트워크가 연결이 죽는 상황이 많이 발생했다.

동시 TCP connection이 많아지면 많아 질수록 더 많이 죽어가는 거 같았기에 혹시 내 local network에서 연결을 할수 있는데에 제한이 걸려있는지 조사하면서 Ephemeral Port 에 대해서 알게 되었다.

실험

간단한 Spring 서버를 준비하고

package com.template;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;

@SpringBootApplication
public class TemplateApplication {
    @RestController
    public static class SimpleController {
        @GetMapping("/hello") 
        public String sayHello() throws Exception {
            Thread.sleep(100);
            return "hello";
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(TemplateApplication.class, args);
    }
}

간단한 K6 script를 작성한 다음에

import exec from 'k6/execution';
import http from 'k6/http';

export const options = {
    setupTimeout: '5m',

    scenarios: {
        ddos : {
            executor: 'per-vu-iterations',
            vus: 10000,
            iterations: 10,
            maxDuration: '30s',
        },
    },
};

export default function(datas) {
    const url = `http://localhost:8080/hello`;
    http.get(url, {});
}

perfmon이라는 윈도우 프로그램을 통해 TCP connection이 얼마나 많이 만들어 지는지, 그리고 얼마나 많이 실패하는지 확인해 보자

현재 connection은 31개 정도이고
실패한 connection은 157615개 이다.

그리고 실험을 한후

connection은 16617개로 올랐고
connection실패도 183594개로 올랐다.

최대 connection이 31 -> 16617 즉 16586 개로 증가했고
connection 실패는 183594 -> 157615 즉 25979 개 증가했다.

왜 이런 현상이 발생하는 걸까?

Ephemeral Port

OS는 TCP connection을 만들때 임의로 connection에 포트를 부여하여 관리한다.
port는 최대 0 - 65535 까지 있으며 이중에서 윈도우즈에서는 49152-65535 까지가 이 용도로 활용된다.

이는 다음과 같은 명령어로 확인해 볼수 있다.

C:>netsh int ipv4 show dynamicport tcp

Protocol tcp Dynamic Port Range
---------------------------------
Start Port      : 49152
Number of Ports : 16384

65535 - 49152+1 = 16,384, 우리가 아까본 connection의 갯수와 거의 유사하다.

더 공부해보아야 할점

근데 왜 살짝 다를까?

0개의 댓글