그래도 개발자일 때 좋았다

Java DNS TTL 관리 본문

Development/Java

Java DNS TTL 관리

실러캔스 2018. 3. 19. 23:13

시스템을 설계하고 개발하다보면 흔히 얘기하는 HA (High Availability)를 이루기 위해서 많은 노력들을 한다. 특히나 Cloud 환경에서 Load Balancer 및 Auto Scaling을 이용할 경우 호스트명이 반환하는 IP 주소는 계속해서 변하게 된다. 이럴 때 DNS 캐시가 되어있으면 새로운 주소를 반환하지 못하고 계속해서 오래된 주소를 반환하여서 결국 시스템에서 에러가 발생한다.


이를 방지하기 위해서는 DNS 캐시 TTL을 설정할 필요가 있다. Java에서는 아래의 파라미터를 통해서 JVM의 DNS TTL을 설정할 수 있다 (단위는 초).

networkaddress.cache.ttl # DNS 주소 캐시
networkaddress.cache.negative.ttl # 실패한 DNS 주소 캐시

Java 공식 문서에는 위의 설정값들을 아래와 같이 정의한다.

networkaddress.cache.ttl

Specified in java.security to indicate the caching policy for successful name lookups from the name service.. The value is specified as integer to indicate the number of seconds to cache the successful lookup.

A value of -1 indicates "cache forever". The default behavior is to cache forever when a security manager is installed, and to cache for an implementation specific period of time, when a security manager is not installed.


networkaddress.cache.negative.ttl (default: 10)

Specified in java.security to indicate the caching policy for un-successful name lookups from the name service.. The value is specified as integer to indicate the number of seconds to cache the failure for un-successful lookups.

A value of 0 indicates "never cache". A value of -1 indicates "cache forever".


각 파라미터는 코드상에서 적용할 수 있고, Java가 시작될 때 적용할 수 있으며, 범용적으로 적용할 수도 있다.

1) 코드상에 적용하는 방법

java.security.Security.setProperty("networkaddress.cache.ttl" , "60");

2) Java 실행시 적용하는 방법 - 이 때는 networkaddress.cache.ttl 대신 sun.net.inetaddr.ttl을 사용, 추후에 없어질 수도 있음

java -Dsun.net.inetaddr.ttl=60

3) 범용적으로 적용하는 방법

$JAVA_HOME/jre/lib/security/java.security상에서 각 파라미터값 수정


참고:

[1] https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/java-dg-jvm-ttl.html

[2] https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html

Comments