能否在Hazelcast中结合OutOfMemoryHandler、MapStore及LRU驱逐策略?
Absolutely—this exact workflow is totally achievable with Hazelcast's built-in tools and a bit of custom wiring. Let's break down how to tie together OutOfMemoryHandler, a 25% LRU eviction strategy, and MapStore to get the behavior you want:
Core Idea
When an OutOfMemoryException is imminent, your custom OutOfMemoryHandler will trigger, calculate 25% of the current map entries, evict the least recently used (LRU) ones, rely on MapStore to persist those evicted entries to your backend storage, and let Hazelcast automatically reload them from the store when those keys are requested later.
Step-by-Step Implementation
1. Set Up Your MapStore for Persistence/Reload
First, implement the MapStore interface to handle saving evicted entries and loading them back on demand. Focus on these key methods:
store(K key, V value)/storeAll(Map<K, V> map): Persist entries to your backend when they're evicted.load(K key)/loadAll(Collection<K> keys): Fetch entries from storage when a client requests an evicted key.
Example snippet:
public class CustomMapStore implements MapStore<String, Object> { private BackendStorage backend = new BackendStorage(); // Replace with your actual storage implementation @Override public void store(String key, Object value) { backend.save(key, value); } @Override public void storeAll(Map<String, Object> map) { backend.saveBatch(map); } @Override public Object load(String key) { return backend.get(key); } @Override public Map<String, Object> loadAll(Collection<String> keys) { return backend.getBatch(keys); } // Implement other required methods (delete, deleteAll, loadAllKeys) based on your storage needs }
Then configure your Hazelcast Map to use this store in write-through mode (ensures immediate persistence when entries are evicted):
<!-- hazelcast.xml --> <map name="your-target-map"> <map-store enabled="true" class-name="com.yourpackage.CustomMapStore"> <write-delay-seconds>0</write-delay-seconds> <!-- Enables write-through behavior --> </map-store> </map>
2. Build a Custom OutOfMemoryHandler
Create a handler that triggers when OOM is detected, calculates the 25% eviction target, and removes the LRU entries using Hazelcast's entry metadata to identify the least recently used items.
public class CustomOOMHandler implements OutOfMemoryHandler { private final HazelcastInstance hazelcastInstance; private final String targetMapName = "your-target-map"; public CustomOOMHandler(HazelcastInstance hazelcastInstance) { this.hazelcastInstance = hazelcastInstance; } @Override public void onOutOfMemory(OutOfMemoryException oom) { IMap<String, Object> map = hazelcastInstance.getMap(targetMapName); int currentSize = map.size(); int evictCount = (int) Math.ceil(currentSize * 0.25); if (evictCount <= 0) return; // Fetch entries sorted by last access time (LRU entries first) List<Map.Entry<String, Object>> lruEntries = map.entrySet().stream() .sorted(Comparator.comparingLong(entry -> map.getEntryView(entry.getKey()).getLastAccessTime())) .limit(evictCount) .toList(); // Evict entries—MapStore will persist them automatically in write-through mode lruEntries.forEach(entry -> map.remove(entry.getKey())); System.out.printf("Evicted %d LRU entries to backend storage to mitigate OOM%n", evictCount); } }
3. Register the Handler with Hazelcast
When initializing your Hazelcast instance, attach the custom handler to activate it:
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(); hazelcastInstance.setOutOfMemoryHandler(new CustomOOMHandler(hazelcastInstance));
Key Notes to Avoid Pitfalls
- Thread Safety: The OOM handler might run concurrently with other map operations—ensure your backend storage calls are thread-safe, or add synchronization where needed.
- Lightweight Handler Logic: Keep operations in
onOutOfMemoryminimal to avoid triggering another OOM during eviction. - Test the Flow: Simulate OOM scenarios (e.g., fill the map with large objects) to verify that eviction triggers correctly, entries are persisted, and reload works as expected.
- Optional Normal Eviction: If you want Hazelcast to handle LRU eviction during regular operations too, add an eviction policy to your map config—but your custom handler will take over for the 25% OOM-specific eviction.
内容的提问来源于stack exchange,提问作者posthumecaver

