I am writing a web service in Java which needs to handle a large number of requests / second. The general flow will be:
- Web service receives a request from client
- Returns a ‘keep polling me’ response to client
- Calls another web service (or
services), and waits for them to
respond (with a timeout) - Client polls our web service, until
it receives a response (with a
timeout)
Researching on the Internet, I have found two general approaches to writing web services:
- Spawn a thread for each request
- Use the Reactor pattern (central dispatcher thread responds to IO events)
Do you have a recommendation for which approach is generally better, and what are the pros/cons of each approach? I would also appreciate pointers to examples.
Don’t think multi-threading. Think asynchronously. I happened to have just coded an async handler that ran 2,000 RPS with <10 threads in IIS. Not sure how java works since I’m a .net guy but I gotta believe they have similar BeginXXX/EndXXX methods. If you ever spawn a thread then you’re not considering all the places your code can block: data base IO, File I/O, web services, etc. These are the places your performance will cause your site to be slow.
Async, Async, Async.
Chant and repeat.