5. 1.1 Varnish란 무엇인가
HTTP Proxy Cache
Open source (BSD license)
http://www.3scale.net/2012/02/getting-the-right-proxy-for-your-api/
기대 효과
•
•
성능 향상
장애 대응
• 원본 서버에 장애가 발생하면, TTL이 지난 데이터라도 전송
6. 1.2 설정
varnishd -s malloc,4G -a :80 -b 10.1.2.3:80
varnishd -s malloc,4G -a :80 -f config.vcl
Varnish Configuration Language
• Domain-specific
• VCL = C program = shared library
Command Line Interface
• vcl.list
• vcl.load
• vcl.use
• vcl.discard
• vcl.show
8. 1.3 Request 처리 흐름
https://www.varnish-software.com/static/book/VCL_Basics.html
9. 1.4 VCL 예 (1)
클라이언트로부터 요청을 받았을 때 실행되는 함수
sub vcl_recv {
if ( req.request != GET ) {
return( pass );
}
if ( req.url ~ ^/user/ ) {
return( pass );
}
unset req.http.Cookie;
return( lookup );
}
10. 1.4 VCL 예 (2)
캐시를 검색하기 위한 키를 생성하는 함수
sub vcl_hash {
hash_data( req.url );
if ( req.http.user-agent ~ iPhone ) {
hash_data( iOs );
} else {
hash_data( Android ) ;
}
return( hash );
}
11. 1.4 VCL 예 (3)
원본 서버가 보낸 답장을 받았을 때 실행되는 함수
sub vcl_fetch {
set beresp.grace = 1h;
if ( beresp.status != 200 ) {
set beresp.ttl = 3s;
}
else if ( req.url ~ ^/images/ ) {
set beresp.ttl = 5m;
}
else {
set beresp.ttl = 10s;
set beresp.dp_gzip = true ;
}
}
12. 1.5 Edge Side Includes
• small markup language
• dynamic web content assembly
esi:include src='get_nid.html'/
Per Andreas Buer, Extreme web performance with Varnish, Ez conference 2009, Paris.
13. 1.6 Grace mode
• TTL이 지난 데이터를 전송
• 원본 서버가 down된 경우
• 원본 서버에 이미 요청을 보낸 경우
sb vcl_recv {
set req.grace= 1h;
}
sub vcl_fetch {
set beresp.grace = 1h;
}
14. 1.7 Saint mode
• 원본 서버가 보낸 답장이 비정상인 경우
• 해당 원본 서버로는 같은 요청을 보내지 않음
sub vcl_fetch {
if ( beresp.status == 500 ||
beresp.http.content-length == 0 ) {
set beresp.saintmode = 10s;
}
set beresp.grace = 1h;
}
22. 3.1 Cookie
Cookie가 있으면, 기본적으로 cache를 하지 않음
sub vcl_recv {
unset req.http.Cookie;
}
sub vcl_fetch {
unset beresp.http.set-cookie;
}
23. 3.2 File descriptor exhausting 문제
원본 서버에 장애가 발생한 상황에서 Collapsed Forwarding로 인해
요청이 처리되지 않고 장기간 기다리는 문제
많은 CLOSE-WAIT 연결 생성
장기간 기다린 요청인 경우, 연결을 종료시키는 patch 적용
24. 3.3 Keepalive
sess_timeout: 브라우저와 Varnish 사이의 연결 지속 시간 지정
default: 5 seconds
sub vcl_deliver {
set resp.http.connection = close;
}
sub vcl_pipe {
set bereq.http.connection = close;
}
25. 3.4 404: Not Found
sub vcl_fetch {
if ( beresp.status != 200 ) {
set beresp.ttl = 1s;
}
else {
set beresp.ttl = 1m;
}
}
Status code to be cached
200: OK
203: Non-Authoritative Iniformation
300: Multiple Choices
301: Moved Permanently
302: Moved Temporarily
307: Temporary Redirect
410: Gone
404: Not Found
26. 3.5 VSM
Varnish Shared Memory
log를 저장하는 공간
_.vsm 파일을 mmap()으로 매핑하여 사용
80 MBytes
Linux의 tmp filesystem을 mount하여 사용할 것을 권장함
27. 3.6 Memory overhead
지정된 cache의 크기는 데이터 크기만 제한함.
데이터의 메타정보를 저장하는 자료구조를 저장하기 위한 저장 공간이
추가로 필요함.
objcore: 120 Bytes
objhead: 96 Bytes