File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Idempotent methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Idempotent methods" Watch "Idempotent methods" New topic

Idempotent methods

Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 110
I understand that a idempotent method can be invoked multiple number of times without any side effect.

My question is in what scenarios should i give idempotent behavior?

e.g let's say you have a method which debits a saving account by x dollar amount.
Now this method can be called multiple times in 2 scenarios:
1. User does the debit action twice.
Action 1: User debit 100$
Action 2: User again debits 100$

2. The client software invokes the debit method multiple times in response to a single user request. The client would do it in case of unreliable messaging(ie it doesn't get a confirmation that the method invocation was successful)

I think only in scenario 2 we should give idempotent behavior. In scenario 1 those are 2 discrete user actions and thus are separate.

Please comment.

In HeadFirst servlets it says that user may click the submit button multiple times by mistake and we should ensure idempotent behavior so that this multiple clicks doesn't give wrong behavior. I think in this scenario, server should not be responsible for providing idempotent behavior(as it can't make out that multiple request are due to double clicking a submit button. Each HTTP Request is not way related to another and thus server has no way to determine that it needs to give idempotent behavior now)
In this scenario it should be client which should somehow disallow user from multiple clicks(e.g by disabling the button on first click)

Please comment
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
A method which changes a resource can not by definition be idempotent.

See this Wikipedia article.

A GET which returns the current balance of the savings account should certainly be idempotent.

Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 110
Hi Bill,
I agree with you. My question was the scenarioebs in which we should/shouldnot give idempotent behavior. I think you would agree that in real world applications there will always be methods which will change resource state(for example a debit method)
It will be helpful if you can comment on the specific question.
I agree. Here's the link:
subject: Idempotent methods
It's not a secret anymore!