Java time-based map/cache with expiring keys
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In Java, managing data with time constraints can be very useful, especially in environments like caching where old data becomes irrelevant after a certain timeframe. An effective way to manage such data is through a time-based map or cache, where keys are set to expire after a specified duration. This concept is not natively supported in the standard Java collections framework but can be implemented using various techniques or third-party libraries. A time-based map or cache ensures efficient memory usage and data freshness, thus enhancing application performance.
Understanding Time-Based Expiration
Time-based expiration in a map or cache involves setting an expiry time for each key-value pair. When the pair expires, it is no longer valid and should be automatically removed from the collection. There are generally two types of expirations:
- Fixed Expiration: Each key in the map expires after a fixed duration.
- Variable Expiration: The expiration time may vary between different key-value pairs based on specific conditions or usage patterns.
Implementation Strategies
Implementation of expiring keys can be done in several ways:
- Lazy Expiration: Check and remove expired entries on each access. This approach is simple but can potentially lead to using outdated data.
- Active Expiration: Use a separate thread or scheduler to remove expired entries at regular intervals. This method ensures that the data in the map is mostly up to date, though it may impose additional resource overhead.
DIY Expiring Map
Although Java doesn’t have a built-in expiring map, one can implement it using a combination of a HashMap and a PriorityQueue. The HashMap holds the data while the PriorityQueue keeps track of the expiration times. Here’s a basic implementation outline:
In this implementation, the ExpiringMap class wraps a HashMap and PriorityQueue. The PriorityQueue tracks keys by expiration time, which helps efficiently identify and remove expired keys when necessary.
Third-Party Libraries
For production-grade applications, using well-tested third-party libraries is generally recommended over building a custom solution. Libraries such as Google Guava and Caffeine provide sophisticated caching mechanisms including expirable entries. For instance, here’s how you can create a cache with Guava:
Table: Comparison of Expiration Techniques
| Technique | Complexity | Memory Efficiency | Data Freshness | Use Case |
| Lazy Expiration | Low | Medium | Low | Minimal resource usage |
| Active Expiration | High | High | High | High freshness requirement |
Summary
Time-based maps or caches are crucial for managing data that has a temporal relevance. Whether you implement your own using core Java classes or utilize third-party libraries, proper handling of expiring keys can drastically impact the efficiency and effectiveness of your data management strategy. Choosing the right strategy depends on specific application needs, including factors like data volume, freshness requirements, and system resources.
By understanding the available techniques and tools, developers can ensure that their applications handle time-sensitive data in an efficient and effective manner.
Related reading
- Jobs in the queue(pub-sub) distributed systems with dependencies?
- Julia Distributed, failed to modify the global variable of the worker
- Julia Distributed slow down to half the single core performance when adding process
- Julia Parallel Distributed
- Java using much more memory than heap size or size correctly Docker memory limit
- Java using much more memory than heap size or size correctly Docker memory limit
- Java Timer vs ExecutorService?
- Java URL encoding of query string parameters

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.