Cache Management

Purpose

Cache Management addresses the challenge of efficiently maintaining a bounded, in-memory set of processed message IDs for the Kafka-backed idempotent repository. Its primary role is to provide a local cache that tracks seen message IDs to enable fast deduplication decisions without repeatedly querying Kafka. This caching layer prevents unbounded memory growth by enforcing a maximum size and uses an eviction strategy to remove the least recently used entries.

This subtopic ensures that the idempotent consumer can quickly identify duplicates with minimal latency while keeping memory usage under control, which is critical when handling large volumes of messages in distributed environments.

Functionality

At its core, Cache Management uses an LRU (Least Recently Used) cache implementation to hold message IDs that have been processed. Key workflows and characteristics include:

Example snippet showing cache initialization and usage:

// Create an LRU cache with configured max size
this.cache = LRUCacheFactory.newLRUCache(maxCacheSize);

// Add a message ID to cache on successful deduplication
if (!cache.containsKey(key)) {
    cache.put(key, key);
    broadcastAction(key, CacheAction.add);
}

Integration

Cache Management is a foundational component of the Kafka-backed idempotent repository, tightly integrated with other subtopics:

By isolating cache size management and eviction policies here, the idempotent repository can scale effectively while preserving correctness and consistency in distributed Kafka consumer deployments.

Diagram

classDiagram
    class KafkaIdempotentRepository {
        - Map<String, String> cache
        - int maxCacheSize
        - long cacheCounter
        + add(String key) bool
        + contains(String key) bool
        + remove(String key) bool
        + clear()
        + getCacheSize() int
        + getCacheCounter() long
    }

    class LRUCacheFactory {
        + newLRUCache(int maxSize) Map
    }

    KafkaIdempotentRepository --> LRUCacheFactory : uses

    KafkaIdempotentRepository : -populateCache()
    KafkaIdempotentRepository : -addToCache(ConsumerRecord)
    KafkaIdempotentRepository : -broadcastAction(String key, CacheAction action)

This class diagram highlights that the `KafkaIdempotentRepository` relies on an LRU cache instance created via the `LRUCacheFactory` to maintain a bounded set of processed message IDs. Key methods manipulate and query this cache while managing synchronization and broadcasting state changes.