Friday, October 31, 2008

Apache Versus Tomcat

This is a pretty popular question judging from the results on google,so I just wanted to consolidate whatever information I found on the web,which helped me decide.

Apache Http Server comes with other features like Modules configuration, SSL, SSO capabilities etc .
If you use Apache Tomcat,you are restricted to serving jsp's and servlet's.
Clustering: By using Apache as a front end you can let Apache act as a front door to your content to multiple Tomcat instances.so handling the failure of one Tomcat is easier.But you could always use Tomcats Clustering capabilities and a hardware load balancer.Apart from loadbalancing.
Clustering/Security: You can also use Apache as a front door to different Tomcats for different URL namespaces (/app1/, /app2/, /app3/, or virtual hosts). The Tomcats can then be each in a protected area and from a security point of view, you only need to worry about the Apache server. Essentially, Apache becomes a smart proxy server. 2/ (i.e. the capability of reverse proxying, allowing you to 'plug in' one or more web applications running on one or more Tomcat instances in one URL space.)
Security. This topic can sway one either way. Java has the security manager while Apache has a larger mindshare and more tricks with respect to security. But also keep in mind, if you run Apache with Tomcat - you have two systems to defend, not one.
Add-ons. Adding on CGI, perl, PHP is very natural to Apache. Its slower and more of a kludge for Tomcat. Apache also has hundreds of modules that can be plugged in at will.
With Apache in front of Tomcat, you can perform any number of decorators that Tomcat doesn't support or doesn't have the immediate code support.
Speed. Apache is faster at serving static content than Tomcat. But unless you have a high traffic site, this point is useless. But in some scenarios, tomcat can be faster than apache. So benchmark YOUR site. Socket handling/system stability. Apache has better socket handling with respect to error conditions than Tomcat. The main reason is Tomcat must perform all its socket handling via the JVM which needs to be cross platform. The problem is socket optimization is a platform specific ordeal. Most of the time the java code is fine, but when you are also bombarded with dropped connections, invalid packets, invalid requests from invalid IP's, Apache does a better job at dropping these error conditions than JVM based program

WebServers And Containers Basics

Apache Webserver versus Apache Tomcat is a popular question,and being on the brink of choosing between the two,I started googling,but before I blog what i found,I just wanted to go over the bare basics of webservers,containers and HTTP.For the experienced ones,skip this and go directly to the differences :Apache Versus Tomcat
HTTP
Hypertext Transfer Protocol (HTTP) is a communications protocol used to transfer information on the web. HTTP is a request/response protocol between a client and a server. The client i.e. a web browser or some other end-user tool makes an HTTP request(called the User Agent)(http://www.google.com/ .The responding server which stores or creates resources such as HTML files and images—is called the origin server understands this request and returns the html content specified by the URL. There can be proxies, gateways and tunnels in between the client and the server, but ultimately the server receives and understands the request and sends the requested resources to the client.The server can return pure HTML content/HTML content with scripts embedded within which are understood by the client's web browser and executed on the client appropriately/files.
Typically, an HTTP client initiates a request by establishing a Transmission Control Protocol (TCP) connection to a particular port .An HTTP server listening on that port waits for the client to send a request message.
Upon receiving the request, the server sends back a status line, such as "HTTP/1.1 200 OK", and a message of its own, the body of which is perhaps the requested file, an error message, or some other information.
Resources to be accessed by HTTP are identified using Uniform Resource Identifiers (URIs) or Uniform Resource Locators (URL).
Web Server
A webserver is a piece of software that understands these HTTP requests,and is able to locate the resource that was requested,or atleast some component that will handle the request and return the resource.This is the basic functionality provided by any webserver.But on top of this,the webserver can also provide some services like: Authentication,handling static content and dynamic content(CGI,JSP,PHP,ASP e.t.c),HTTPS support(SSL or TLS) to allow secure encrypted connections to the server on the port 443 instead of 80,Content Compression,Virtual Hosting(Serve many websites using one IP address),Large File Support,Bandwidth throttling.

A basic HTTP webserver only understands HTTP,If we are programming in JSP's and servlets,we need something that understands Servlets and JSP's,the job of a Servlet Container is just that.Consider Apache HTTP Server and Apache Tomcat.The Apache HTTP Server like the name suggests acts as a webserver,Apache Tomcat acts as a webserver and can also understands servlets(A servlet container).So in order to use the Apache Webserver and let it serve jsp's and servlet's we can plug the Apache Tomcat Container into the Apache HTTP Server.