Java expiring map and TTL cache with time-based keys
Codemia Knowledge Hub ·
The standard Java collections framework has no map with expiring keys. You can build one yourself (a HashMap for the data plus a PriorityQueue of expiry times), but for production code a library such as Google Guava or Caffeine is the better choice, since both support expirable entries out of the box.
Fixed vs variable expiration
Each key-value pair gets an expiry time, and once it passes the entry is no longer valid and should be removed. There are two common models:
- Fixed expiration: every key expires after the same duration.
- Variable expiration: the expiry time differs per entry, based on specific conditions or usage patterns.
Lazy vs active removal
- Lazy expiration: check for and remove expired entries on each access. It is simple, but stale data can linger until something touches the map.
- Active expiration: a separate thread or scheduler removes expired entries at regular intervals. The map stays mostly up to date at the cost of extra resource overhead.
| Technique | Complexity | Memory Efficiency | Data Freshness | Use Case |
| Lazy Expiration | Low | Medium | Low | Minimal resource usage |
| Active Expiration | High | High | High | High freshness requirement |
A DIY expiring map with HashMap and PriorityQueue
The HashMap holds the data and the PriorityQueue orders keys by expiration time, so the expired ones are always at the head of the queue and can be removed cheaply:
This version uses lazy expiration: get purges expired keys before reading. Each map entry remembers its own expiry time, so when a key is put again, the older queue entry for it is recognised as stale and does not remove the fresh value early.
Using Guava's CacheBuilder
Well-tested libraries are generally the safer option. Here is a Guava cache whose entries expire 10 minutes after being written:
Which approach fits depends on data volume, freshness requirements and available system resources.
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.lang.OutOfMemoryError Java heap space
- Java Timer vs ExecutorService?
- Java URL encoding of query string parameters
System design interview problems on this topic
- Design a Key Value Store: solution and walkthrough
- Design a Distributed Unique Id Generator: solution and walkthrough
- Design a Distributed Locking System: solution and walkthrough
- Design a Distributed Counter: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.