I am trying to find the differences between what Clojure calls an STM and what is implemented in Haskell as STM. Taking the actual language semantic differences aside I am a little confused as Rich Hickey says in his speech that Clojure's implementation of STM is very different from anything else out there, but I don't understand the differences apart from the language choice.
3 Answers
Clojure STM has 3 big unique features:
- Implements MVCC snapshot avoiding transactions restarts on read invalidation.
- Ensures references on read-writes provides a kind of manual control over resource acquisition order.
- Has explicit commute which reduces retries on commutative writes.

- 22,298
- 21
- 125
- 196

- 554
- 4
- 6
-
3might you also add zero overhead for non-transactional reads to this list? – mikera Jan 02 '11 at 09:57
-
It's true, when you use non-transactional context, but if you consider STM and retries on other transactions, I don't have sure if hole system suffer on performance caused by context swap.But JVM is very mature and STM is like a GC...very necessary, so you should think in that way – william gouvea Jan 13 '11 at 17:23
-
1Hi william. reading the Mark volkmann article and presentation , he said "reads only trigger a retry in Clojure when the history list of a Refdoesn’t contain a value committed before the txn began" . The first feature described above is correct ? – CHAPa Mar 23 '11 at 10:29
-
@CHAPa correct, *reducing* is a better word than *avoiding*. But generally only in special cases (slow reads with fast frequent writes) will this even come in to play. – Dax Fohl Jun 08 '13 at 03:21
-
Clojure's STM suffers one particular problem that is well-known and unresolved, where long-running transactions may never complete if they are pre-empted by shorter transactions. I'm not sure if Haskell has a similar problem. – johnbakers Jul 22 '16 at 20:41
For Haskell STM, see SPJ's papers: http://research.microsoft.com/en-us/um/people/simonpj/papers/stm/
Of particular use are "Composable memory transactions" and "Transactional memory with data invariants". GHC's implementation of STM indeed isn't MVCC. I don't recall all the implementation details, but my understanding is that the description in the papers isn't all that different from what currently exists in GHC.
(note that MVCC, in clojure or elsewhere, makes possible write-skew -- see, e.g., here: http://en.wikipedia.org/wiki/Snapshot_isolation)

- 38,665
- 7
- 99
- 204
-
8Also noteworthy is that GHC's implementation provides compile-time guarantees of the safety of a transaction with respect to side effects; and the unique `orElse` combinator for atomically composing transactions. – Don Stewart May 04 '11 at 16:28
-
@DonStewart In addition Haskell `STM` is a `MonadPlus`, which allows you to specify additional failure conditions within the transaction context. i.e. if final account balance is negative, fail/retry the txn. It also allows you to share that logic with other `MonadPlus` classes easily; the same function would show the result of all *potential* valid transactions given a `List` of accounts and purchases. This can be useful if the logic is complex. I don't think either is possible in clojure. – Dax Fohl Jun 08 '13 at 03:13
Mark Volkmann did a very detailed presentation on STMs in general (and Clojure's STM in particular) at Strange Loop 2009 which you can find here (article and slides here). I don't really know of any other resource (other than the code) for understanding how Clojure's STM works.

- 367
- 1
- 3
- 17

- 69,183
- 25
- 122
- 167
-
The presentation link doesn't work. Anyone have an alternative to this talk? – Neil Sep 13 '15 at 18:58
-
1The presentation slides and a detailed article on the topic are available here http://java.ociweb.com/mark/stm/article.html – lorefnon Oct 21 '15 at 12:47
-
1
-
Is there a recording of Mr. Volkmann's talk available somewhere? – Louis Thibault Jan 01 '20 at 22:29