Hazelcast
Cache Eviction
Manual Cache Management
Future Timestamp
IT Solutions

how to evict hazelcast caches manually or on a future timestamp

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Hazelcast IMDG (In-Memory Data Grid) is a leading software platform that allows for the storage and management of distributed data across multiple machines efficiently. One of the critical tasks while managing a cache is the capability to evict or clear entries either manually or based on a predefined schedule. Here, we delve into how Hazelcast supports cache eviction, providing both manual and timed eviction strategies.

Understanding Hazelcast Cache Eviction

Cache eviction in Hazelcast is a process that removes entries from the cache, either to free up memory or because the entries are no longer valid or needed. Eviction can be triggered manually or automatically based on various policies. These policies can be specified based on memory constraints or a set time-to-live (TTL) and max idle settings.

Manual Eviction

Manual eviction in Hazelcast can be carried out in several ways, depending primarily on the specific requirements of the application:

  • Evicting Specific Entries: You can evict specific entries from the cache by their keys. This approach is useful when you know exactly which entries are no longer needed.
  • Clearing the Entire Cache: Sometimes, it might be necessary to clear all entries from the cache completely.

Here's how you can implement these strategies using Hazelcast's Java client API:

java
1// Assume cache is an instance of IMap
2IMap<String, String> map = hazelcastInstance.getMap("my-distributed-map");
3
4// Evicting a single entry
5map.evict("key1");
6
7// Clearing all entries
8map.clear();

Scheduled Eviction

Scheduled eviction involves removing entries from the cache at a specific future time or after a certain period has elapsed. Hazelcast provides a way to configure this via TTL and max idle settings.

  • Time-To-Live (TTL): Each entry can be configured to live in the cache for a specific duration. After this time, it will automatically be evicted.
  • Max Idle Time: Entries can be set to expire if they have not been accessed for a predefined idle period.

These settings can be applied at the time of entry insertion or at the map level as default values. Here's how to set these options:

java
1// Setting TTL for a specific entry
2map.put("key2", "value2", 20, TimeUnit.SECONDS);
3
4// Setting default TTL for all entries at the map level
5MapConfig mapConfig = hazelcastInstance.getConfig().getMapConfig("my-distributed-map");
6mapConfig.setTimeToLiveSeconds(1800); // Entries expire after 30 minutes

Advanced Eviction Configurations

For more granular control, Hazelcast offers several advanced configuration options such as Eviction Policies and custom eviction strategies:

  • Eviction Policies: These are predefined strategies determining which entries to evict when a configured size is reached. Common policies include LRU (Least Recently Used), LFU (Least Frequently Used), and others.
  • Custom Eviction: Hazelcast allows implementing custom eviction policies by extending its eviction interface.

Example of Configuring Eviction Policy

java
1MapConfig mapConfig = hazelcastInstance.getConfig().getMapConfig("my-distributed-map");
2mapConfig.getEvictionConfig()
3         .setEvictionPolicy(EvictionPolicy.LRU)
4         .setMaxSizePolicy(MaxSizePolicy.FREE_HEAP_SIZE)
5         .setSize(500);

Summary Table

FeatureFunctionalityConfiguration Method
Manual EvictionEvicts specific or all entries manuallymap.evict() or map.clear()
Scheduled EvictionEvicts entries based on time settingsmap.put() with TTL or Max Idle
Eviction PoliciesApplies policies like LRU, LFUEvictionConfig settings
Custom EvictionAllows custom eviction strategiesImplement MapEvictionPolicy

Conclusion

Effective cache management through eviction strategies is crucial for maintaining optimal application performance and resource utilization. Hazelcast provides versatile tools and configurations allowing developers to manage cache eviction efficiently, whether through manual interventions, preset policies, or scheduled evictions based on time constraints. By understanding and utilizing these capabilities, developers can significantly enhance their Hazelcast implementations.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.