Browse Clojure Foundations for Java Developers

Atoms, Refs, Agents, and Vars

A quick map of Clojure’s state tools and the coordination model behind each.

Clojure separates values (usually immutable data) from references (places where state can change).

Here are the core tools you will see in real projects:

  • Var: a namespace-level reference (defined with def/defn). Vars support redefinition at the REPL and can be dynamically bound in limited cases.
  • Atom: independent, synchronous updates to a single value via swap! (CAS-style).
  • Ref: coordinated updates across multiple refs using STM transactions (dosync + alter).
  • Agent: asynchronous, serialized updates to an agent’s value via send.

Java mental model: atoms feel like AtomicReference + “update with a function”; refs feel like transactional coordination; agents feel like a single-threaded work queue for state updates.

This section helps you choose a tool based on the kind of state and coordination your problem actually has.

In this section

Revised on Friday, April 24, 2026