This project is a distributed, in-memory cache system implemented in Java.
It provides fast, scalable, and fault-tolerant caching with support for eviction policies, write strategies, request collapsing, and consistent hashing.
It simulates how modern distributed caching systems (like Redis, Memcached, or Hazelcast) work, with support for multiple storage backends and asynchronous client operations.
- In-memory distributed caching with consistent hashing
- Eviction policies: LRU (Least Recently Used) & LFU (Least Frequently Used)
- Write strategies: Write-Through & Write-Back
- Pluggable databases (MySQL, PostgreSQL, or custom backends)
- Request collapsing to avoid duplicate DB fetches
- Striped executor service for per-key ordering with high concurrency
- Asynchronous client API with
Futuresupport - Scalable design for multiple cache nodes
- Class:
DistributedCache<K,V> - Routes client requests to cache nodes using consistent hashing.
- Manages node additions/removals dynamically.
- Class:
CacheNode<K,V> - Stores key-value entries in memory.
- Handles eviction policy & write policy.
- Connects to persistent storage (MySQL/Postgres).
- Classes:
LRUEvictionStrategyLFUEvictionStrategy
- Ensures cache doesn’t exceed capacity.
- Classes:
WriteThroughStrategy→ Writes immediately to DB.WriteBackStrategy→ Defers writes until eviction/update.
- Classes:
MySqlDatabasePostgresDatabase
- Factory:
StorageFactory→ Configures backend dynamically.
- Class:
RequestCollapser<K,V> - Collapses concurrent requests for the same key into one DB/cache fetch.
- Class:
StripedExecutorService - Guarantees per-key task ordering while allowing concurrency across keys.
- Class:
CacheClient<K,V> - Provides async API:
putAsync(key, value)getAsync(key)deleteAsync(key)
- Ensures fault tolerance with consistent hashing.
- Maintains write consistency via configurable strategies.
- Collapses duplicate requests to reduce load on storage.
| Operation | Method Example | Description |
|---|---|---|
| Put | cacheClient.putAsync("user:1", "Alice") |
Adds a key-value entry |
| Get | cacheClient.getAsync("user:1") |
Retrieves value by key |
| Delete | cacheClient.deleteAsync("user:1") |
Removes key from cache |
| Eviction | Automatic (LRU/LFU) | Removes least-used entries |
| Write Policy | Configurable (Write-Through / Write-Back) | Defines how DB writes occur |
- Language: Java 17+
- Concurrency:
ExecutorService,CompletableFuture - Storage: MySQL, PostgreSQL (pluggable)
- Design Patterns: Factory, Strategy, Collapser
- Build Tool: Maven
distributed-cache/
│── src/main/java/com/cache/
│ ├── cache/ # Core cache classes (CacheNode, DistributedCache, Client)
│ ├── evictionpolicies/ # Eviction strategies (LRU, LFU)
│ ├── writepolicies/ # Write strategies (Write-Through, Write-Back)
│ ├── storage/ # Storage backends (MySQL, Postgres, Factory)
│ ├── utils/ # Utility classes (RequestCollapser, StripedExecutorService)
│── README.md # Project documentation
- Java 17+
- Maven 3.8+
- MySQL/Postgres (optional, for persistence)
-
Clone the repository:
git clone <repo-url> cd distributed-cache
-
Build the project:
mvn clean install
-
Run Example (inside
Main.javaor test client):CacheClient<String, String> client = new CacheClient<>(...); client.putAsync("key1", "value1"); client.getAsync("key1").get(); // returns value1