What is Cache?
-
Cache is temporary storage that stores the result of frequently accessed data in memory so that subsequent requests are served more quickly
What data should be cached? Mostly used, less frequently changing data is stored in cache. (Eg: Metadata, configuration data)
What should not be cached? Data that is frequently/actively changing.
Cache lies near to requester so that requester need not to go hard-disk to retrieve data. This reduces latency.
Types of Cache
Name | Description |
---|---|
1. Write Thru Cache |
Data written to cache and DB both. 1st to cache then to DB. Latency is high Disadvantage: Cache flooding |
2. Write Around Cache |
Data is directly written on DB. Then later cache updates itself from DB Cache flooding is avoided |
3. Write Back Cache |
Data is directly written to cache only. Then later DB updates itself from cache Disadvantage: Risk of Data loss(cache may fail) |
Where Caches can be placed?
Place | Description | ||
---|---|---|---|
Web Browser Cache |
|
||
Between Application Server and Database |
Cache Between AppServer & DB
Cache Eviction Policies
-
No eviction returning an error the memory limit is reached
All keys LRU removing keys by the least recently used first
Volatile LRU removing keys, that have an expiration time set, by the least recently used first.
All keys random removing keys randomly
Volatile random removing keys, that have an expiration time set, randomly
Volatile TTL removing keys, that have an expiration time set, by the shortest time to live first.
Redis vs Memcached
Redis (Open source implemented in C) | Memcached(Open source implemented in C) | |
---|---|---|
In Memory | y | y |
Key-value pair | y (key < 250B, value < 1MB) | y (key < 512MB, value < 512MB) |
Speed | less. It does not have any inbuilt datatypes | More. Bcoz it supports datatypes as(string,hash,list,set,sorted set etc) |
How internally implemented | Slab Allocator | Encapsulated version of the malloc/free |
Scaling | Multi-threaded. Scales vertically. Give more cores, more memory | Single Threaded. Scales horizontally |
Cache Eviction policies | Only 1 LRU | 6 different policies |