Can retractions improve performance?


I have a schema where users can have a session/token. My application is a web-app, so when the app recieves a request with a <appname>_session cookie, it looks up the user which has the provided session/token and considers the user logged in if a user is found.

Now, tokens aren’t valid forever. So what I do is to lookup the transaction that added this token, and check if the token was created/asserted within a reasonable time (a week).

The interesting thing here is that I don’t really need to retrat tokens to avoid logging in users with invalid tokens, as I always check the transaction time. But I’m wondering if there are advantages to periodically retracting invalid tokens anyway?

As an example, say I have a user that has logged in one thousand times (and so has one thousand session tokens), would there be any benefit to retract all the invalid tokens when I always check the validity of the token by its creation time?


It’s a good question. Personally I wouldn’t do that. As I understand it Datomic acts similar to an append only log file where the newest entries are accessed first in any querying. So when you retract you’re adding data to the top to nullify its previous entry. IF that’s true then it’s also possible that performance could be worse[1]. Really, I think the Datomic team can provide a better answer for that, but personally I’d be more interested to see if using the ‘since’ filter [2] is a better option in your case.

  1. theoretically, though practically speaking I doubt it would make any real difference.



Wouldn’t :db/noHistory essentially remove the need for retraction altogether?


If you never do a retraction (or never assert a new value) then :db/noHistory has no effect.


Right. I’m all new to this, so take this with a grain of salt.

I thought, overwriting the session token counts as an implicit retraction, and :db/noHistory would kick in.


Oh. No, you got the right idea. It’s just in my app, a user can have more than one session token :slight_smile:


If you don’t retract datoms, the index grows and data access becomes more expensive. Official datomic people say that after 10 billions of datoms the index may become a bottleneck.

So for performance it’s better to remove unused data.