113 deview2013 varnish-day1_track1_session3_1013

Post on 30-Nov-2014

2.459 views 4 download

description

 

Transcript of 113 deview2013 varnish-day1_track1_session3_1013

더 빠르고, 더 안정적인 웹 서비스를 위한 Varnish!

강흠근 / 네이버 윤신주 / 네이버

CONTENTS 1. Varnish 소개����������� ������������������  

2. 실습 영상

3. Varnish 운영 Tip

4. 튜닝

5. 마무리

1. Varnish 소개

1.1 Varnish란 무엇인가

HTTP Proxy Cache Open source (BSD license)

기대 효과 •  성능 향상 •  장애 대응

•  원본 서버에 장애가 발생하면, TTL이 지난 데이터라도 전송

http://www.3scale.net/2012/02/getting-the-right-proxy-for-your-api/

1.2 설정

varnishd -s malloc,4G -a :80 -b 10.1.2.3:80

Varnish Configuration Language •  Domain-specific •  VCL => C program => shared library

varnishd -s malloc,4G -a :80 -f config.vcl

Command Line Interface •  vcl.list •  vcl.load •  vcl.use •  vcl.discard •  vcl.show

1.2 설정 (CLI)

1.3 Request 처리 흐름

https://www.varnish-software.com/static/book/VCL_Basics.html

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 ); }

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 ); }

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 ; } }

1.5 Edge Side Includes

•  small markup language •  dynamic web content assembly

Per Andreas Buer, "Extreme web performance with Varnish," Ez conference 2009, Paris.

<esi:include src='get_nid.html'/>

1.6 Grace mode

•  TTL이 지난 데이터를 전송 •  원본 서버가 down된 경우 •  원본 서버에 이미 요청을 보낸 경우

sb vcl_recv { set req.grace= 1h; } sub vcl_fetch { set beresp.grace = 1h; }

1.7 Saint mode

•  원본 서버가 보낸 답장이 비정상인 경우 •  해당 원본 서버로는 같은 요청을 보내지 않음

sub vcl_fetch { if ( beresp.status == 500 || beresp.http.content-length == 0 ) { set beresp.saintmode = 10s; } set beresp.grace = 1h; }

1.8 Collapsed forwarding

•  동일한 URI에 대한 다수의 요청 •  해당하는 데이터가 캐시에 없는 경우

Varnish

1.9 삭제 (purge)

> curl -X PURGE http://localhost/deleted.html

acl purge { "localhost" ; } sub vcl_recv { if ( req.request == "PURGE" && client.ip ~ purge ) { return( lookup ); } ... }

1.10 삭제 (ban)

정규표현식을 사용하여 다수의 데이터를 일시에 filtering함

> varnishadm ban.url / > varnishadm ban.url ^/css > varnishadm ban.url .gif$ > varnishadm ban "req.http.host == m.bboom.naver.com && req.url ~ .gif$"

1.11 Varnish usage

•  www.facebook.com •  search.twitter.com •  www.weather.com •  www.answers.com •  www.globo.com •  www.nytimes.com

http://ingvar.blog.redpill-linpro.com/2011/06/22/the-usage-of-varnish-revisited-3/

2. 실습 영상

2.1 실습 영상

3. Varnish 운영 Tip

3.1 Cookie

Cookie가 있으면, 기본적으로 cache를 하지 않음 sub vcl_recv { unset req.http.Cookie; } sub vcl_fetch { unset beresp.http.set-cookie; }

3.2 File descriptor exhausting 문제

원본 서버에 장애가 발생한 상황에서 Collapsed Forwarding로 인해 요청이 처리되지 않고 장기간 기다리는 문제 많은 CLOSE-WAIT 연결 생성 장기간 기다린 요청인 경우, 연결을 종료시키는 patch 적용

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"; }

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

3.5 VSM

Varnish Shared Memory log를 저장하는 공간 _.vsm 파일을 mmap()으로 매핑하여 사용 80 MBytes Linux의 tmp filesystem을 mount하여 사용할 것을 권장함

3.6 Memory overhead

지정된 cache의 크기는 데이터 크기만 제한함. 데이터의 메타정보를 저장하는 자료구조를 저장하기 위한 저장 공간이 추가로 필요함. objcore: 120 Bytes objhead: 96 Bytes

3.7 Transient storage

Parameter shortlived default 10 -p shortlived=0 -s Transient=malloc,100M

3.8 client ip

remove req.http.X-Forwarded-For;

set req.http.X-Forwarded-For = client.ip;

3.9 vary header

vary header 값이����������� ������������������  존재하면����������� ������������������  하나의����������� ������������������  hash key에����������� ������������������  여러개의����������� ������������������  caching data

가����������� ������������������  존재

remove beresp.http.Vary;

3.10 access log

varnishncsa����������� ������������������  ­–a����������� ������������������  

����������� ������������������  -F����������� ������������������  "\'%h����������� ������������������  %{X-Forwarded-For}i����������� ������������������  %u����������� ������������������  …⋯…⋯����������� ������������������  "����������� ������������������  ����������� ������������������  

����������� ������������������  -w����������� ������������������  /home1/irteam/logs/varnish_access.log����������� ������������������  -D����������� ������������������  ����������� ������������������  

����������� ������������������  -P����������� ������������������  ~/varnish/varnishncsa.pid����������� ������������������  ����������� ������������������  ����������� ������������������  

����������� ������������������  -m����������� ������������������  "RxURL:.*\.nhn.*"

4. 튜닝

4.1 Varnish parameter 튜닝 (1)

•  캐시����������� ������������������  크기

•  -s malloc,2G

•  -s file,/home/varnish/data/storage1,100G

•  -s persistent,/home/varnish/data/storage1,100G

varnishd -s malloc,4G -p shortlived=0 -a :80 -f config.vcl

•  Transient space

•  shortlived: 10 (0) seconds •  Thread

•  thread_pool: 2

•  thread_pool_min: 5

•  thread_pool_max: 500

4.1 Varnish parameter 튜닝 (2)

•  요청����������� ������������������  및����������� ������������������  답장의����������� ������������������  크기

•  http_req_hdr_len: 8192 bytes

•  http_req_size: 32768 bytes

•  http_resp_hdr_len: 8192 bytes

•  http_resp_size: 32768 bytes

•  Listen Queue

•  listen_depth: 1024 (8192)

4.1 Varnish parameter 튜닝 (3)

•  Time out

•  sess_timeout: 5 (1) seconds

•  send_timeout: 600 (60) seconds

•  idle_send_timeout: 60 (6) seconds

•  connect_timeout: 0.7 seconds

•  first_byte_timeout: 60 (5) seconds

•  between_byte_timeout: 60 (2) seconds

4.2 Linux Kernel 튜닝

•  Listen queue •  net.core.somaxconn •  128 -> 8192

•  TCP send buffer •  net.ipv4.tcp_wmem •  4096 16384 4194304 -> 4096 262144 4194304

5. 마무리

5.1 마무리

웹 서비스에서의 캐싱 •  Ehcache, memcached, CDN •  image, css, js, query results

동적 컨텐츠의 캐싱 •  큰 효과

THANK YOU