Cache
- Overview
- Operations
- Key-Value Mappings
- Persistence
- Eviction Policies
- Invalidation Strategies
- Redis vs Memcached
Temporary data store, typically in RAM, that holds frequently accessed data for faster retrieval.
Core Conceptsโ
- Cache Operations
- Cache Lookup: Application checks the cache for requested data
- Cache Hit: Data is found in the cache and returned directly
- Cache Miss: Data is not found in the cache. The application fetches it from the primary source, updates the cache, and returns the data
Benefitsโ
- Reduced database load
- Faster data access (commonly retrieval from RAM)
- Scalability
- Cost-Effectiveness
GET
: Retrieve the value of a keyPUT
: Create a new key-value pair or update an existing keyDELETE
: Delete a key-value pair
Keysโ
- Unique identifier
- Can be only a string
Valuesโ
Redis Data Structures | Underlying Implementation |
---|---|
String | SDS (Simple Dynamic String) |
Lists |
|
Hashes |
|
Sets |
|
Sorted Sets |
|
Levelsโ
- 1ns (L1 cache)
- 10ns (L2 cache)
- 100ns (RAM access): Redis read
- 10ยตs (send data over network): Memcached send data over 1 Gbps network
- 100ยตs (read from SSD): RocksDB read
- 1ms (database insert): PostgreSQL insert
- 10ms (HDD disk seek): PostgreSQL read
- 100ms (packet CA โ NL โ CA): Remote Zoom call
- 10s (retry/refresh interval): Grafana refresh interval
Options:
- AOF (Append Only File): Acts like a log, continuously recording every write operation. This allows replaying them on restart to rebuild the data
- RDB (Redis Database): Creates point-in-time snapshots of your entire dataset at regular intervals
- No Persistence: Disables data persistence entirely, suitable for caching temporary data
Eviction Policy | Description | Pros | Cons | Use Cases |
---|---|---|---|---|
Least Frequently Used (LFU) | Evicts the least frequently accessed items from the cache. It counts how often an item is accessed |
|
|
|
Least Recently Used (LRU) | Evicts the least recently accessed items from the cache. It tracks the time of the last access for each item |
|
|
|
Size-Based | Evicts items based on the size of the cache. It ensures that the cache does not exceed a predefined size limit |
|
|
|
Time-to-Live (TTL) | Evicts items based on their time since creation or last access. Items are evicted once their predefined lifespan expires |
|
|
|
- Time-Based
- Absolute Timeout: Cached data is invalidated after a fixed period from the time of caching
- Relative Timeout: Cached data is invalidated after a fixed period from the time of last access or update
- Sliding Timeout: Timeout period is extended every time the data is accessed or updated
- Event-Based Invalidation
- Publish/Subscribe Model: Invalidate cache based on events published by the data source or other relevant services
- Webhooks: Trigger cache invalidation based on HTTP callbacks from the data source
- Message Queue Integration: Invalidate cache in response to messages received from a message queue
- Manual Invalidation
- Programmatic Invalidation: Invalidate cache entries programmatically through API calls or direct cache manipulation
- Admin Console: Provide a user interface for administrators to manually invalidate cache entries
Aspect | Redis | Memcached |
---|---|---|
Data Structure |
| Plain string value |
Architecture | Single-thread for read/write keys | Multi-threaded |
Transactions | Support atomic operations | โ |
Snapshots/Persistence |
| โ |
Pub-Sub Messaging | Support Pub-Sub messaging with pattern matching | โ |
Geo-Spatial Support | Geospatial indexes that stores the longitude and latitude data of the location | โ |
Server-Side Script | Support Lua script to perform operations inside Redis | โ |
Support Cache Eviction |
| LRU |
Replication | Leader-Follower replication | โ |
Securityโ
- Overview
- Cache Miss
Cache Miss Attack exploits weaknesses in how data is stored to steal information or overload systems. It targets situations where a web application doesn't consider all input data when storing data in a cache. This lets attackers trick the cache into revealing sensitive information or causing performance issues.
Solutions:
- Cache keys with null value. Set a short TTL (Time to Live) for keys with null value
- Bloom filter to quickly check if key exists before hitting cache/database