murtaza52
murtaza52

Reputation: 47441

real world application of clojure STM

As I understand clojure STM can be used for transacting values across refs.

I understand this property is useful in datastores, where 2 or more locations have to be mutated in a single transaction - atomicity.

However in what cases will this be useful in software applications ? I could just store all my state in one map and use an clojure.core/atom if I want shared mutable state.

In what types of applications / scenarios will usage of refs make sense over atoms or other state primitives in clojure.

Upvotes: 1

Views: 138

Answers (1)

Juraj Martinka
Juraj Martinka

Reputation: 4368

Using an atom is indeed what's suggested in the Elements of Clojure book by Zach Tellman (chapter 2 - If you have mutable state, use an atom). They say that until ~60% utilization of the stateful container, an atom is probably a better choice. The advice is summarized at the end of the section as:

  • If you have mutable state, make sure it belongs inside your process.
  • If it does, try to represent it as a single atom.
  • If that causes performance issues, try spreading the work across more processes.
  • If that isn’t possible, see if the atom can be split into smaller atoms that don’t require shared consistency.
  • Finally, if that doesn’t help, you should start looking into Clojure’s STM primitives.

Upvotes: 2

Related Questions