-1

A REST client goes through a sequence of steps with a server in a flow. The client would like to cancel the flow and undo all the changes done to the data in that flow.

For example, we have a below method. It has three different steps in it. First two are rest calls where third one is data insertion. Now if restCall1(), restCall2() are success but third step is failed. Everything done in first two steps should be reverted back.

method(){
    restCall1(); // Rest Call to the server, perform DB operations
    restCall2(); // Rest call to the server, perform file operations 
    insertData(); // Perform DB operations
} 

What is the best practice to deal with this transaction problem. One way is to build a custom transaction framework and rollback steps. Is there any framework/tool that can give solution to deal with this problem?

Narendra Verma
  • 2,372
  • 6
  • 34
  • 61

1 Answers1

-1

A REST client can only maintain it's own state in REST call and there is no synchronization between state of REST client and resources managed by server.You will have to maintain the synchronization among the various REST calls to web service which violates the stateless communication.If you intend to do that you have to maintain the synchronization of states among the various REST calls and co-ordinate accordingly.

More here

However there are workarounds.Retro is a model which supports the transactions for REST in an error prone and scalable way. See this as well.Hope it helps.

Community
  • 1
  • 1
Ankit Tripathi
  • 405
  • 4
  • 13
  • I get your point. But this is a common use case. I was thinking, apart from building our own rollback strategy, are there any best practices to handle this? – Narendra Verma Nov 29 '16 at 12:11
  • Doing transactions with REST is not best practice.However there is a workaround.I have edited my answer – Ankit Tripathi Nov 29 '16 at 12:14
  • OK, so how to deal with this problem? Any Idea? – Narendra Verma Nov 29 '16 at 12:16
  • I don't agree completely with your statement that a client can't keep the state of the transaction. Let me quote [Fielding himself](https://www.ics.uci.edu/~fielding/pubs/dissertation/net_arch_styles.htm#sec_3_4_3) : *The client-stateless-server style derives from client-server with the additional constraint that no session state is allowed on the server component. Each request from client to server must contain all of the information necessary to understand the request, and cannot take advantage of any stored context on the server. Session state is kept entirely on the client.* – MaVVamaldo Nov 29 '16 at 12:35