Fork me on GitHub

To play that back my understanding is that I create an empty session (as in (def empty-session (mk-session))) and insert facts on a per customer basis and fire the rules per customer - I hope that’s what you mean. If not, I would like to understand more!


the issue seems to revolve around cases where side effects or where establishing truth is expensive in complex sessions but I don’t have that issue - my case is very much operating like a pure function where Clara is simply used to improve expressivity


dumb question - are the facts from previous sessions GCd? If not, must I retract them? It would be simpler to take the hit on running mksession every time and TBH I have not profiled that so maybe it’s also not such a biggie!


@raymcdermott: Clara sessions are intended to be data structures that obey Clojure idioms, that is, they are value-based and immutable. When you insert a fact into a session, a new session is returned that is independent of the previous session. So, for example, if you create an empty session and insert a fact into it, the original empty session still exists and can be reused. I created a quick demo of this at


The “com/clear-session-cache!” clears a cache of created sessions that Clara automatically creates unless it is explicitly turned off. The sessions without facts are cached by Clara on the set of rules, queries, and session options to save compilation time. However, this cache should never contain sessions with facts. The relevant code for that cache is at


Clara should only maintain references to facts through sessions. Once all sessions containing a given fact are GC’able then that fact itself should be GC’able as far as Clara is concerned.


To clarify too, Clara doesn’t copy the facts between sessions that contain them; if you have a session that contains FactA and then you insert FactB in it you’ll have two sessions that both contain a reference to the same FactA object. Clara expects that facts won’t be mutated after insertion.


thanks - I appreciate the effort put into your answer


it’s also nice to see answers from two people that are not @ryanbrush 😉


seriously, I'm a fan and it’s good to see the community growing