Search code examples
clojurestm

One ref or multiple refs in Clojure?


I am developing a clojure application which makes heavy use of STM. Is it better to use one global ref or many smaller refs in general. Each ref will be storing data like in a relational database table, so there will be several refs.


Solution

  • A possible benefit of using fewer refs is that you it will be easier to comprehend what is happening in your presumably multi-threaded app.

    A possible benefit of using more refs is that you will be locking less code at a time and increasing speed.

    If you have a ref per table and you need to maintain data integrity between two tables, then you are going to be responsible for implementing that logic since the STM has no knowledge of how the data relates between the tables.

    Your answer might lie in how often a change in one table will effect another and whether breaking your single ref out into one ref per table even gives you a notable performance increase at all.