Understanding Clojure Concurrency Primitives
If you're like me, you got confused when first researching Concurrency in Clojure. Should I use a ref here? How about an atom? Maybe an agent?
Refs
Refs are used when you have more than one piece of mutable state and they all needed to be updated at the same time; if they weren't updated at the same time, the data would be invalid. This happens synchronously.
Atoms
Atoms are used when you have a single piece of mutable state which is independent of any other mutable state. This happens synchronously.
Agents
Agents are used when you have a single piece of mutable state. This happens asynchronously.
Ladder of Control
With refs at the top, atoms mid-way, and agents at the bottom, it is a ladder of control. The higher you climb the ladder, the more control you have.
