1 Reply Latest reply on Jun 29, 2016 4:40 AM by rvansa

    Recommendations on usage of invalidation cache

    stianst

      In Keycloak we use invalidation caches heavily to prevent database traffic. However, we have two related problems that should be a common issue with invalidation caches and I'm looking for some advice and best practices on how to deal with these:

       

      Problem A - Node B loads entity from DB imediately after Node A updates/invalidates the entity

      T1 Node A loads entity from db and adds to cache

      T2 Node B loads entity from db

      T2 Node A updates entity and invalidation message is sent to cluster

      T3 Node B adds entity to cache


      In this case in T2 Node B has loaded the stale data from the database, but misses the invalidation message as it's not been added to the cache yet.


      Problem B - Node A and Node B changes entity at the same time (we're not bothered about one node overwriting changes made by the other, but we need both nodes and database to be consistent)

      T1 Node A loads entity from db and adds to cache

      T1 Node B loads entity from db and adds to cache

      T2 Node A updates entity and invalidation message is sent to cluster

      T2 Node B updates entity and invalidation message is sent to cluster

      T3 Node A adds update entity to cache

      T3 Node B adds update entity to cache


      In this case in T3 Node A and B both adds the value they believe the entity has, but only one matches the db.