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.
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:
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:
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
Summary Table
| Feature | Functionality | Configuration Method |
| Manual Eviction | Evicts specific or all entries manually | map.evict() or map.clear() |
| Scheduled Eviction | Evicts entries based on time settings | map.put() with TTL or Max Idle |
| Eviction Policies | Applies policies like LRU, LFU | EvictionConfig settings |
| Custom Eviction | Allows custom eviction strategies | Implement 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
- How to exclude some tables from RDS Mysql replication
- How to externalize adapter artifact in Hexagonal Architecture?
- How to extract an assembly from the GAC?
- How to fix jobs conflict in transactional replication
- How to flush all the cache entries in simple spring memcached
- How to force browsers to reload cached CSS and JS files?
- How to get InputStream via Spring-Feign?
- How to get largest number of consecutive integers in a substantially large array (spread across multiple machines)

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.