最近在寻找LVS的替代程序,加上scaner同学的忽悠,我也开始用先进科技haproxy。haproxy表现很不错:支持的并发连接上万没问题;流量上几百M也没问题,出乎意料;更赞的是HAProxy有详细的状况报告页面,有了报告,调优更方便了。当然HAProxy也有问题:并发连接多时CPU占用很高。
对于图片服务器,打开HTTP Keep Alive还是有帮助的:一般页面中会有N多个同一域名的图片,如果不开Keep Alive,浏览器下载图片慢。打开HTTP Keep Alive,连接数一般就会多八九倍。现有的haproxy的task_queue函数(task.c中)时间复杂度是O(n),开销很大。作者已经在实现新的O(log(n))的task_queue,等待合适的机会发布。
lighttpd的maillist在讨论多CPU情况下lighttpd的效率,有人就提到HAProxy。HAProxy首页有段关于Keep Alive的话很好:
"Keep-alive was invented to reduce CPU usage on servers when CPUs were 100 times slower. But what is not said is that persistent connections consume a lot of memory while not being usable by anybody except the client who openned them. Today in 2006, CPUs are very cheap and memory is still limited to a few gigabytes by the architecture or the price. If a site needs keep-alive, there is a real problem. Highly loaded sites often disable keep-alive to support the maximum number of simultaneous clients."
keep alive通俗地讲,就是所谓的常连接。常连接节省了TCP连接过程的开销,有一定的优势。现有操作系统越来越先进,建立连接的开销越来越小,比如linux 2.6的epoll,freebsd的kqueue,可以让程序不fork进程或开线程就能同时服务N多连接。与此同时,客户端比如IE,Firefox却可以同时开多个线程取内容,如果开了Keep alive,反而不好。因此一般情况下不需Keep Alive