cache.putIfAbsent(key, value);
cache.get(key).method();
Given code produces NPE at second line. Here is how I create cache:
ClientCacheConfiguration cacheConfig = new ClientCacheConfiguration().setCacheMode(CacheMode.REPLICATED) .setName("POSITION");
cache = igniteClient.getOrCreateCache(cacheConfig);
It's possible to come up with a number of edge cases in which
put(1, "foo"); get(1)
returnsnull
. This is confusing, I know - distributed systems often are.The root problem is always the cache settings. Ignite historically has a couple of defaults which lean towards performance instead of consistency:
CacheConfiguration.writeSynchronizationMode=PRIMARY_SYNC
returns to control to the client while the backup nodes haven't been updated yet.CacheConfiuration.readFromBackup=true
allows to read from a backup - which might be lagging behind because of thePRIMARY_SYNC
.While both of these options have their perfectly valid use cases, you may want to set
writeSynchronizationMode=FULL_SYNC
andreadFromBackup=false
to enforce stronger consistency and avoid the mess.