Wednesday 10 July 2013

Comparison and cooperate HAProxy and Varnish

Common points between HAProxy and Varnish

Before comparing the differences, we can summarize the points in common:
·         reverse-proxy mode
·         advanced HTTP features
·         no SSL offloading
·         client-side HTTP 1.1 with keepalive
·         tunnel mode available
·         high performance
·         basic load-balancing
·         server health checking
·         IPv6 ready
·         Management socket (CLI)
·         Professional services and training available

Features available in HAProxy and not in Varnish

·         advanced load-balancer
·         multiple persistence methods
·         DOS and DDOS mitigation
·         Advanced and custom logging
·         Web interface
·         Server / application protection through queue management, slow start, etc…
·         SNI content switching
·         Named ACLs
·         Full HTTP 1.1 support on server side, but keep-alive
·         Can work at TCP level with any L7 protocol
·         Proxy protocol for both client and server
·         powerful log analyzer tool (halog)
·         <private joke> 2002 website design </private joke>

Features available in Varnish and not in HAProxy

·         caching
·         grace mode (stale content delivery)
·         saint mode (manages origin server errors)
·         modular software (with a lot of modules available)
·         intuitive VCL configuration language
·         HTTP 1.1 on server side
·         TCP connection re-use
·         Edge side includes (ESI)
·         a few command line tools for stats (varnishstat, varnishhist, etc…)
·         powerful live traffic analyzer (varnishlog)
·         <private joke> 2012 website design </private joke>

Conclusion

Mặc dù HAProxy có thể thực hiện TCP proxying, nhung nó thường được sử dụng phía trước web application, một cách chính xác những gì mà ta có thể tìm thấy ở Varnish.
Chúng hoàn toàn hợp tác với nhau rất tốt: Varnish sẽ làm cho web nhanh hơn bằng cách offloading các static object, trong khi HAProxy đảm bảo việc load-balancing smooth với smart persistence và khả năng giảm DDOS.
Cơ bản, HAProxy và Varnish là bạn với nhau, ngoại trừ một vài đặc tính đối chọi nhau :) Mỗi thứ đều là chuyên gia của mình trên miền riêng của mình: HAProxy is a reverse-proxy Load-Balancer and Varnish is a Reverse-proxy cache.

Why a single domain?

Trong ứng dụng web có 2 loại dữ liệu: static và dynamic.
Dynamic có nghĩa là nội dung được tạo theo yêu cầu request của từng user riêng biệt trên browsing hiện tại trên ứng dụng. Bất cứ thứ gì không nằm trong loại này thì được xem như là static. Mặc dù một trang được tạo ra từ PHP và nội dung của nó thay đổi mỗi phút hay vài giây (như CMS Wordpress hay drupal). Ta gọi những trang đó là “pseudo-statid”.
Điểm mạnh nhất của Varnish là có thể cache static object, phân phát chúng thay mặt server, offloading hầu hết traffic từ server.
Một đối tượng được xác định bởi Host header và URL. Khi bạn có một domain name, một Host header cho tất cả các request: static, pseudo static hay dynamic.
Bạn không thể chia traffice thành: mọi thứ request phải đến một loại của thiết bị: LB, cache…
Một trong những cách để chia nội dung dynamic và static là sử dụng một domain cho mỗi loại đối tượng: dynamic.domain.com cho dynamic và static.domain.com cho static. Thực hiện như vậy bạn có thể chuyển traffic dynamic đến LB và traffic static đến cache một cách trực tiếp.

Available Architecture

1.      CLIENT ==> HAPROXY ==> VARNISH ==> APPSERVER
2.      CLIENT ==> VARNISH ==> HAPROXY ==> APPSERVER

Pro and cons of HAProxy in front of Varnish

PROS:
·         Use HAProxy‘s smart load-balancing algorithm such as uri, url_param to makevarnish caching more efficient and improve the hit rate
·         Make the Varnish layer scalable, since load-balanced
·         Protect Varnish ramp up when starting up (related to thread pool creation)
·         HAProxy can protect against DDOS and slowloris
·         Varnish can be used as a WAF (web application firewall)
CONS:
·         no easy way do do application layer persistence
·         HAProxy queueing system can hardly protect the application hidden by Varnish
·         The client IP will be mandatory forwwarded on the X-Forwarded-For header (or any header you want)

Pro and cons of Varnish in front of HAProxy

PROS:
·         Smart layer 7 persistence with HAProxy
·         HAProxy layer scalable (with persistence preserved) since load-balanced by Varnish
·         APPSERVER protection through HAProxy request queueing
·         Varnish can be used as a WAF
·         HAProxy can use the client IP address (provided by Varnish in a HTTP header) to doTransparent proxying (getting connected on APPSERVER with the client ip)
CONS:
·         HAProxy can’t protect against DDOS, Varnish will do
·         Cache size must be big enough to store all objects
·         Varnish layer not scalable

Vậy đâu là kiến trúc tốt nhất?

Không cần phải chọn lựa kiến trúc nào kém hơn! Sẽ tốt hơn nếu quan tâm việc xây dựng platform mà không có điểm yếu.
Hình sau là mô hình chúng ta sẽ thực hiện:

Ghi chú:
·         H: HAProxy Load-Balancers
·         V: Varnish servers
·         S: Web application servers
·         C: Client or end user
Vai trò chính của mỗi layer:
·         HAProxy: Layer 7 traffic routing, first row of protection against DDOS (syn flood, slowloris, etc…), application request flow optimiation
·         Varnish: Caching, compression. Could be used later as a WAF to protect the application
·         Server: hosts the application and the static content
·         Client: browse and use the web application

Traffic Flow

Cơ bản, client sẽ gửi tất cả request đến HAProxy, sau đó HAProxy dựa trên URL hay file extension sẽ quyết định routing:
·         Nếu request giống như cho (pseudo) static object, thì forward nó đến Varnish. Nếu Varnish miss (bỏ qua) object, nó sẽ sử dụng HAProxy để lấy nội dung từ server.
·         Gửi tất các request khác đến appserver. Nếu bạn thực hiện một cách đúng đắn thì ở đây chỉ duy nhất có dynamic traffic.
Ta không sử dụng Varnish như tùy chọn mặc định trong flow này, vì nội dung dynamic có thể bị cache lại, dẫn đến thông tin cá nhân của một ai đó có thể bị leak cho mọi người. Hơn thế nữa, trong trường hợp “massive misses” hoặc các request được xây dựng để bypass cache, I don’t the servers to be hammered by Varnish, so HAProxy protects them with a tight traffic regulation between Varnish and appservers.

Dynammic traffic flow

Hình bên dưới chỉ ra request yêu cầu nội dung dynamic được route qua platform một cách lí tưởng:

Ghi chú:
1.       The client sends its request to HAProxy
2.       HAProxy chooses a server based on cookie persistence or Load-Balancing Algorithm if there is no cookie.
The server processes the request and send the response back to HAPRoxy which forwards it to the client

Static traffic flow

Hình bên dưới chỉ ra request yêu cầu nội dung static được route qua platform một cách lí tưởng

1.       The client sends its request to HAProxy which sees it asks for a static content
2.       HAProxy forward the request to Varnish. If Varnish has the object in Cache (a HIT), it forwards it directly to HAProxy.
3.       If Varnish doesn’t have the object in cache or if the cache has expired, then Varnish forwards the request to HAProxy
4.       HAProxy randomly chooses a server. The response goes back to the client through Varnish.
Trong trường hợp bị miss , luồng xử lí có tỏ ra nặng nề. Ta muốn làm chuyện đó với cách sử dụng các feature qui định của HAProxy để ngăn ngừa Varnish flood server. Hơn thế, vì Varnish chỉ nhìn thấy nội dung static, HIT rate của nó hơn 98% … Do đó overhead thì rất chậm và sự bảo vệ được cải thiện.

Pros of such architecture

·         Use smart load-balancing algorithm such as uri, url_param to make varnish caching more efficient and improve the hit rate
·         Make the Varnish layer scalable, since load-balanced
·         Startup protection for Varnish and APPSERVER, allowing server reboot or farm expansion even under heavy load
·         HAProxy can protect against DDOS and slowloris
·         Smart layer 7 persistence with HAProxy
·         APPSERVER protection through HAProxy request queueing
·         HAProxy can use the client IP address to do Transparent proxying (getting connected on APPSERVER with the client ip)
·         Cache farm failure detection and routing to application servers (worst case management)
·         Can load-balance any type of TCP based protocol hosted on APPSERVER

Cons of such architecture

To be totally fair, there are a few “non-blocking” issues:
·         HAProxy layer is hardly scalable (must use 2 crossed Virtual IPs declared in the DNS)
·         Varnish can’t be used as a WAF since it will see only static traffic passing through. This can be updated very easily

 Configuration

HAProxy configuration - http://pastebin.com/CYFN7kxE
Varnish configuration - http://pastebin.com/GqUWwtc2