Search code examples
hazelcast

Embedded Hazelcast backup reads with sync backups


I have a question about backup reads when running embedded hazelcast.

If sync backups are enabled and we also enable backup reads do we still risk stale reads when doing updates inside a lock?

To clarify what I want to know a bit.

If I use the configuration described above.

If all updates are done with the pessimistic locking pattern like:

    map.lock(key);
    try {
        Value value = map.get(key);
        value.amount++;
        map.put(key, value);
    } finally {
        map.unlock(key);
    }

The read within the lock would be safe when using backup reads right? Since every put will make sure all the backups are updated when using sync backup?


Solution

  • Yes, if you use sync backups, then your data is consistent across Hazelcast members, so your code snippet works as expected. This is all assuming the Hazelcast cluster itself is stable (no split-brain).

    Note however that lock is not the most optimal way of achieving your goal. Please take a look at Hazelcast EntryProcessor.