Consider the following scenario :
- User sends a request to transfer money.
- Server receives the request and transfers the money but the reply times out.
- The app reports failure.
- So the user tries to transfer money again.
How do we avoid this kind of a scenario ?
0
There are two ways:
- Make the operation idempotent, i.e. doing it twice has no different effect from doing it just once.
- Make it possible to find out status of a request ex-post. That is, have additional query for result of past request given it’s ID.
For protocols between applications the former is preferred, but you’ll have to do the later when the user gets involved.
Basically when you don’t get the response, you retry the request with the same ID and same one-time-password (or whatever authentication token that is different for each request to prevent replay attack) and make sure the server does nothing and repeats the last response if it has already seen the request. That will handle case when the network simply drops the response.
However when the network has longer outage just in the inconvenient moment, the user will eventually give up and close the app. Than when they start it again later you need a way to show them which transactions were received on server so they know whether they need to enter the transaction again or not. Because you’ve lost state by user closing the app, your only chance here is to list the transaction and make sure the user sees it.
2