If transactions over REST are unachievable, how can REST ever be really useful? [closed]
Solution 1:
I am going to assume that when you talk about transactions you are talking about a distributed Two Phase Commit protocol.
If I understand correctly you are trying to understand how we could ever use REST to perform operations that span multiple systems if REST cannot support transactions across distinct REST requests. The problem is you are making a potentially flawed assumption that we should be using transactions to achieve consistency. What price are we paying for using them, and what alternatives exist?
Pat Helland who used to work for Amazon and is now at Microsoft, wrote a paper Life beyond Distributed Transactions. In the paper the Author makes the following statement:
Unfortunately, programmers striving to solve business goals like eCommerce, supply-chain-management, financial, and health-care applications increasingly need to think about scaling without distributed transactions. They do this because attempts to use distributed transactions are too fragile and perform poorly.
His paper explores alternative solutions to distributed transactions that do scale and perform well.
Maybe REST will be successful because it does not support transactions. Here is a quote from Roy Fielding, the guy who invented the term REST
If you find yourself in need of a distributed transaction protocol, then how can you possibly say that your architecture is based on REST? I simply cannot see how you can get from one situation (of using RESTful application state on the client and hypermedia to determine all state transitions) to the next situation of needing distributed agreement of transaction semantics wherein the client has to tell the server how to manage its own resources.
...for now I consider "rest transaction" to be an oxymoron.
This is from a message on the REST-discuss list from June 9th, 2009. I can't provide a link because Yahoo groups is useless.
Solution 2:
If you want transactions in a ReST application, at the ReST API function, it's usually because you still have your techie webservice guy googles on.
Let's look at it another way.
Soap googles on: Open a transaction, create a customer record, create an order record, commit transaction.
ReST googles on: Ask server what to do. Server says "create customer resource", so POST /customers. Server says, "now create an order if you want", client creates the order by following the form.
In ReST, the application protocol is expressed in terms of resources being created and manipulated, not in terms of data that has transaction boundaries.
If you want to have a long-running transaction that spans all those operations, it's the server that decides to initiate it, not the client.
You can still implement long running transactions on the server side. If you attempt to want transactions from the client side, then you assume the client already knows all the operations it's going ot execute and the transaction boundaries that exist between those operations. If that's what your expectations are of the client, you've already given up the late binding, hypermedia-driven nature of a rest architecture.
So indeed, if you're not doing ReST and are trying to fit in RPC over http, you'll have a problem with not having transactions.
Solution 3:
I think most actions that normally require transactions can be reworked to occur without them.
For example, the classic bank transfer. Suppose I want to move $100 from account A to B:
Begin Transaction /Debit A, $100 /Credit B, $100 Commit Transaction
This could be reworked as:
/Transfer A, B, $100
In this way, the server might do this in two steps, but the action from the client is a single, atomic operation that makes logical sense.
I'm sure there are lots of examples where it is more convenient to do an all or nothing set of operations (and I'm curious what people can come up with to address them), but I usually rework things in this way.