Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent client from overloading server?

I have a Java servlet that's getting overloaded by client requests during peak hours. Some clients span concurrent requests. Sometimes the number of requests per second is just too great.

Should I implement application logic to restrict the number of request client can send per second? Does this need to be done on the application level?

like image 831
user121196 Avatar asked Feb 09 '11 02:02

user121196


1 Answers

The two most common ways of handling this are to turn away requests when the server is too busy, or handle each request slower.

Turning away requests is easy; just run a fixed number of instances. The OS may or may not queue up a few connection requests, but in general the users will simply fail to connect. A more graceful way of doing it is to have the service return an error code indicating the client should try again later.

Handling requests slower is a bit more work, because it requires separating the servlet handling the requests from the class doing the work in a different thread. You can have a larger number of servlets than worker bees. When a request comes in it accepts it, waits for a worker bee, grabs it and uses it, frees it, then returns the results.

The two can communicate through one of the classes in java.util.concurrent, like LinkedBlockingQueue or ThreadPoolExecutor. If you want to get really fancy, you can use something like a PriorityBlockingQueue to serve some customers before others.

Me, I would throw more hardware at it like Anon said ;)

like image 122
dj_segfault Avatar answered Oct 22 '22 20:10

dj_segfault