A resilient three-tier caching system designed to withstand long-duration database outages while maintaining service availability through in-memory cache, repository layer, and persistent disk storage.
┌─────────────────┐
│ In-Memory │ Fast, limited size, configurable TTL (Caffeine)
│ Cache │
└────────┬────────┘
↓ miss
┌─────────────────┐
│ Repository │ Simulated database (100ms-1min latency)
│ Layer │
└────────┬────────┘
↓ miss
┌─────────────────┐
│ Disk Storage │ Persistent storage (RocksDB, compressed)
│ (RocksDB) │
└─────────────────┘
# Run tests
./gradlew test
# Run demonstration
./gradlew run| Test File | Purpose |
|---|---|
CacheStoreTest |
Core tier cache behavior and coordination |
RocksDBDiskStoreTest |
Persistent storage operations |
DatabaseRepositoryTest |
Repository layer with simulated latency |
TierCacheIntegrationTest |
End-to-end integration scenarios |
AppTest |
Application-level smoke tests |
./gradlew test --tests "tier_cache.CacheStoreTest"
./gradlew test --tests "tier_cache.RocksDBDiskStoreTest"
./gradlew test --tests "tier_cache.TierCacheIntegrationTest"- Outage Resilience: Continue serving cached data during extended database outages
- Fast Access: Cache hits typically <1ms
- Automatic Persistence: Evicted items saved to disk for outage recovery
- Thread-Safe: Handles concurrent access
- Graceful Degradation: Falls back through tiers on failure
- Compressed Storage: RocksDB with compression enabled
- Configurable: TTL, cache size, cleanup options
- First Access: Repository → slow (simulated latency)
- Subsequent: Cache → fast (<1ms)
- Cache Full: Evicted items → disk storage
- Cache Miss: Repository → disk → null
- Database Outage: Cache → disk → continues serving stale data
- Caffeine (in-memory cache)
- RocksDB (persistent storage)
- Mockito (testing)
✅ Cache hit/miss behavior
✅ Multi-tier data flow
✅ Concurrent access patterns
✅ Error handling & recovery
✅ Data persistence
✅ Resource management
- RocksDB errors: Ensure native libraries are installed
- Slow tests: Random delays (100ms-60s) are intentional
- Disk space: Tests create temporary databases
Cache speedup demonstrated in integration tests:
- Cache hits: <1ms
- Repository access: 100ms-60s (simulated)
- Disk access: 1-10ms typical
Test Environment: Long-duration database outage simulation (~25 minutes)
| Strategy | 3 min | 5 min | 7 min | 10 min |
|---|---|---|---|---|
| TierCache (Caffeine+RocksDB) | 100.0% | 100.0% | 100.0% | 100.0% |
| EhCache with Disk | 100.0% | 0.0% | 0.0% | 0.0% |
| Caffeine Only (Baseline) | 100.0% | 0.0% | 0.0% | 0.0% |
| Strategy | Cache Hit | Cache Miss |
|---|---|---|
| TierCache (Caffeine+RocksDB) | 2.50 μs | 19.11 μs |
| EhCache with Disk | 6.31 μs | 12,042.11 μs |
| Caffeine Only (Baseline) | 2.74 μs | 12,022.38 μs |
| Strategy | Total Time | Throughput |
|---|---|---|
| TierCache (Caffeine+RocksDB) | 140 ms | 357,143 op/s |
| EhCache with Disk | 201 ms | 248,756 op/s |
| Caffeine Only (Baseline) | 37 ms | 1,351,351 op/s |
🏆 Best for Long Outages: TierCache (Caffeine+RocksDB)
- Maintains 100.0% availability after 25+ minutes of database outage
⚡ Fastest Performance: TierCache (Caffeine+RocksDB)
- 2.50 μs average latency for cache hits
- 19.11 μs for cache misses (vs 12ms+ for alternatives)
💡 Recommendation: TierCache provides the best balance of outage resilience and performance for production systems requiring high availability during database failures.