Hiten
Hiten

Reputation: 53

Hazelcast IMap - What can be the most efficient way to get value for multiple keys?

Use Case: IMap and has 70K entries. Majority of operations are GET call for multiple keys. GET calls (90%) > PUT calls (10%). We are using EP for PUT calls.

Problem: What can be the most efficient way to get the data for multiple keys which can be present across multiple instances?

Possible Solution: 1. EP with ReadOnly, Offloadable and use executeOnKeys method. 2. parallely execute map.get(key) for all keys.

Is there any other efficient way to get the data for multiple keys?

  1. Predicate

    public class ExecuteOnSelectedKeyPredicateTest { public static void main(String[] args) { HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();

            IMap<String, Integer> map = hazelcastInstance.getMap("testMap");
    
            map.put("testKey1", 1);
            map.put("test", 2);
            map.put("testKey3", 3);
    
            // Get entries which ends with a number
            Set<Map.Entry<String, Integer>> entries = map.entrySet(new ExecuteOnSelectedKeyPredicate("^.+?\\d$"));
    
            System.out.println(entries);
        }
    
        public static class ExecuteOnSelectedKeyPredicate implements Predicate<String, Integer> {
    
            private String keyRegex;
            private transient Pattern pattern;
    
            public ExecuteOnSelectedKeyPredicate(String keyRegex) {
                this.keyRegex = keyRegex;
            }
    
    
            @Override
            public boolean apply(Map.Entry<String, Integer> mapEntry) {
                if (pattern == null) {
                    pattern = Pattern.compile(keyRegex);
                }
                Matcher matcher = pattern.matcher(mapEntry.getKey());
                return matcher.matches();
            }
        }
    }  
    

Upvotes: 0

Views: 1779

Answers (1)

tom.bujok
tom.bujok

Reputation: 1642

Getting multiple keys the request has to possibly go to multiple members. executeOnKeys is pretty optimal here, since it calculates the partitions the operations needs to be executed on, and it sends that operation to these partitions only. Offloadable makes sure you don't block the partitionThreads and ReadOnly optimizes the processing further.

Multiple get operations will generate more network traffic, since you have one operation per key.

Upvotes: 1

Related Questions