4 Replies Latest reply on Jun 27, 2013 9:52 AM by jmrecio

    strategies for consistent reads?

    mikewse

      [I'm just starting to evaluate Infinispan / JBoss Data Grid so please excuse noobness]

       

      What ways are possible to achieve consistent reads across a set of keys?

      Consider this example with two competing transactions:

       

      {code}

      Initial state:

        a=1

        b=1

       

      T1                  T2

      tx.begin()

      cache.get("a")

                          tx.begin()

                          cache.put("a", 2)

                          cache.put("b", 2)

                          tx.commit()                   

      cache.get("b")

      tx.commit()

      {code}

       

      I'd like transaction T1 to see a consistent state where the two keys are either both set to 1, or both set to 2.

      Is this possible?

      Are there both pessimistic and optimistic alternatives?

       

      Thanks

      Mike

        • 1. Re: strategies for consistent reads?
          pruivo

          Hi,

           

          There's no easy way to do it since stronger isolation levels are not currently supported.

           

          However, one possible workaround for your case can be acquiring a lock in each read operation. To achieve this, you need to set the PESSIMISTIC locking mode in your Infinispan configuration:

           

          <transaction lockingMode="PESSIMISTIC" .... />
          

           

          Then, you have to adapt your code to acquire the lock during the read operations. If I'm not mistaken, you have two alternatives:

           

          1) using the FORCE_WRITE_LOCK flag. Example:

           

          cache.getAdvancedCache().withFlags(Flag.FORCE_WRITE_LOCK).get("a");
          

           

          2) explicit locking the key:

           

          cache.getAdvancedCache().lock("a");
          cache.get("a");
          

           

          Notice that both methods are prone to deadlocks between transactions. You can use the second alternative and acquire all the locks required in your transaction in some order to avoid the deadlock.

           

          Cheers,

          Pedro

          • 2. Re: strategies for consistent reads?
            mikewse

            Thanks Pedro,

            Does this locking of keys, and the pessimistic requirement, lead to an RPC being sent for every key locked?

            Is there any way to minimize the number of RPCs? (maybe different for readers and writers?)

            Best regards

            Mike

            • 3. Re: strategies for consistent reads?
              pruivo

              Hi Mike,

              Mike Wilson wrote:

               

              Does this locking of keys, and the pessimistic requirement, lead to an RPC being sent for every key locked?

              yes it does.

              Mike Wilson wrote:

               

              Is there any way to minimize the number of RPCs? (maybe different for readers and writers?)

              you can use the alternative 2) and acquire all the lock with one RPC when you start the transaction. Example:

               

              cache.getAdvancedCache().lock("a", "b" /*you can add more keys if needed*/);
              

               

              Cheers,

              Pedro

              • 4. Re: strategies for consistent reads?
                jmrecio

                Mike,

                 

                Perhaps this explanation on REPEATABLE_READ vs READ_COMMITTED (the default AFAIK) can provide some relief:

                 

                https://community.jboss.org/message/733120#733120

                 

                JM