Stores the data and programs that are frequently used by the CPU. So any time some data is required it is first checked in the cache memory.
Performance of Cache Use:
Hit ratio = hit/(hit + miss) = no. of hits/ total access.
Types
Type | Desc / Examples |
---|---|
1. In Memory | Redis & Memcached - Uses RAM not Disk |
2. Distributed | Redis Cluster & AWS Elastic Cache - Distributed In Memory Caches |
3. Client Side | Cookies , local storage - Usually for scripts, states, etc |
4. DB Caches | Database Indexes help speed up reads |
5. CDNs | Cache static files (based on proximity) - ref |
Cache Strategies
Usually for In Memory Caches
1. Read Through (Hit / Miss)
- App reads from Cache (return on Hit)
- On Miss:
- Cache requests from DB
- Store in Cache & serves to App
- Good for: Read Heavy Caches (using TTL to store LRU Cache)
2. Cache Aside (Lazy Loading)
- Similar to above
- Difference: App handles control to write on cache
- Good for: High Read to Write Ratio i.e; read >> write
3. Write Through (Logging ACK)
- Every write operation is executed on both the cache and the DB
- Consistency improved