Friday, September 18, 2015

Don't be afraid of chinese restaurants ;)

How many times in your life have you shout to a non english speaking Chinese waitress at a restaurant in Soho, something like: "HEY!!! Why are you swapping the card 4 times?!?!?!!"
In this post I will prove that Chinese waitress are honest and often people panic, because they don't understand what Idempotency is.

The debit cards payment gateways are the classic example of idempotency.

In distributed systems terminology, we say that a service is Idempotent, when its clients can repeatedly send the same requests while the service produces the same result.
In other words, multiple identical requests are treaded by the service as a single request.

Important to note that this doesn't mean that the response is identical, it is just the service result that will be identical.
While the state of the response may change between requests, the result will always be the same, for identical requests.
In other words, it is save to call the service multiple times.

Let's have a look at a simple example of an idempotent function in java:

Receipt process(DebitCard debitCard, Amount amount) { throws NoPatienceException 
      if(not(transactionSentFor(debitCard,amount))) {  
           return cardProcessor.process(debitCard, amount);  
      }  
      else {           
           return blankReceiptFor(debitCard, amount);  
      }  
 }  

In the example above, we can clearly see that the conditional statement is safeguarding us from starting probably a distributed transaction across multiple parts of a distributed system. Instead of that, if we try to process again,  a blank/identical receipt with the returned



Come on, stop being affraid of chinese restaurants. 


No comments:

Post a Comment

Share with your friends