Search code examples
javahadoopservletsapache-phoenix

Preventing duplicate requests to java servlet


I have a java serlvet, which accesses a hadoop cluster and sends a downloadable csv of some data from the hadoop cluster as a response.

My issue is that there appears to be multiple GET requests to this servlet (which I understand to be intentional for chrome + other browsers), which is causing multiple connections to open to my Hadoop cluster. I don't want multiple requests at once. Is there a way to deny multiple requests from the same source and only respond to the first request?


Solution

  • It's unclear to me what the basis is for your claim that the behavior is intentional. The other SO question you cited in comments merely makes the same claim, without citation to any source for it. In the end, however, it probably doesn't matter: if the behavior is common, as opposed to being associated with a small number of specific buggy instances that you can just fix, then you probably need to deal with it regardless.

    With that said, GET requests are not, in principle, supposed to change the state of the server (and it follows that they should be idempotent). This could be taken as justification for all kinds of interesting -- and annoying -- behavior. Inasmuch as there can be no justification for similarly making duplicate or preemptive POST requests, however, I anticipate that you could solve the problem by disabling the GET method for the resource in question, and forcing clients to instead POST requests for it. I do not think clients will then issue duplicate requests except as explicitly directed by users (e.g. via double-clicking the link / button).

    On the other hand, supposing that your web application is performing session tracking -- enabled by default in most servlet containers -- you can detect multiple concurrent requests and handle them. Specifically, you can set a session attribute when you begin handling such a request, clear it when you finish, and have the servlet test that attribute to determine how to handle each request.

    I suggested in comments that you might return an error code for duplicate requests, and indeed you can, but that behavior might surprise clients because they may expect GET requests to be idempotent. As an alternative, you could consider deferring service on the duplicate requests until the computation is finished, and then serving identical responses to all the requests based on the same computation results.

    As far as I know, however, you cannot simply drop duplicate requests. There is simply no mechanism to do so anywhere in the Servlet API.