views:

79

answers:

4

I don't know if it's simply because page-loads take a little time, or the way servlets have an abstraction framework above the 'bare metal' of HTTP, or just because of the "Enterprise" in Jave-EE, but in my head I have the notion that a servlet-based app is inherently adding overhead compared to a Java app which simply deals with sockets directly.

Forget web-pages, imagine instead a Java server app where you send it a question over an HTTP request and it looks up an answer from memory and returns the answer in the response. You can easily write a Java socket-based app which does this, you can also do a servlet approach and get away from the "bare metal" of sockets.

Is there any measurable performance impact to be expected implementing the same approach using Servlets rather than a custom socket-based HTTP listening app?

And yes, I am hazy on the exact data sent in HTTP requests and I know it's a vague question. It's really about whether servlet implementations have lots of layers of indirection or anything else that would add up to a significant overhead per call, where by significant I mean maybe an additional 0.1s or more.

+1  A: 

Of course there is some overhead. What it's hard to define is how much, not because it's hard to measure, but because it's hard to find an alternative which doesn't introduce the risk of being more fragile, bug ridden and poorly scalable.

In other words, supposing you are really dying for performance... will you roll out you own server? How will it deal with managing concurrent requests (all hitting the same port)? Will you write your own parser for the HTTP request? Will it be able to work correctly with all browsers? How will you recognize a session from a given client?

In other words your question is a bit like asking "do relational DBs introduce a significant overhead when measured against an imaginary custom datastore which nobody has written yet but that would cut out all the unecessary features in favour of raw speed?"

My answer: yes, of course. I would still use a DB, because it works now, and has been tuned for lots of things developers take for granted, but are horribly inefficient to reinvent every time you discover you need them for your application.


I strongly suggest, in case you haven't already, to have a look at Jetty, and specifically at the WebSockets implementation.

p.marino
Please re-read the question, this is not about serving web-pages but a more generic server app, think more about a real-time communication between clients.
John
And I wouldn't _have_ to roll my own server, there are socket-wrapping libraries around for concurrent clients. I'm asking if servlets are appropriate for this kind of thing.
John
I wasn't mentioning "pages". I mentioned http requests, just like you. And the "browsers" reference was for stuff like Ajax, Flex, Silverlight. Not to mention that you could use something like Selenium to test your server.
p.marino
Edited original answer to provide a concrete example of a fast, embeddable servlet container and a specific technology that may serve your needs better.
p.marino
+2  A: 

Of course every abstraction adds overhead.

Judging by the fact that many java based web-applications exist - it's perfectly fine in terms of performance.

So in short - don't worry about it. Chances are that you will create a less optimal solution than javax.servlet. Performance problems come mostly from poor code.

Bozho
Are you talking about overhead that's small in comparison to building and serving a whole web-page, or just small even for a generic client-server communication compared to a simple framework I might write to hide raw sockets?
John
it is negligent wherever you look it from :) Except for real-time, time-sensitive data transfer, perhaps. But that's not what HTTP is for
Bozho
What it's _for_ and what it's _capable_ of can be very different. Just look at what they twisted HTML into :)
John
A: 

I don't think there will be too much overhead. HTTP messages are very tiny, and very simple to parse. Servlet architecture doesn't have a lot of abstraction over bare bone http messages.

Even a naive servlet implementation should only cost a fraction of time it takes for a client to send the http request message to the server. That is, the overhead is unnoticeable.

irreputable
A: 

There is some overhead in terms of what has to be done to initialize a servlet. Although this only happens once when the servlet receives its first request.

Michael Angstadt