Skip to content

Commit 63b4879

Browse files
committed
Add system design cache
1 parent 6536ef8 commit 63b4879

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

SystemDesign/cache.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Cache
2+
3+
When talking about cache, here we are talking about web development related cache.
4+
5+
[Here is a good article to start with](https://www.digitalocean.com/community/tutorials/web-caching-basics-terminology-http-headers-and-caching-strategies)
6+
7+
Here we are focusing on database cache.
8+
9+
- Cache Usage Pattern
10+
- **Cache Aside**: application is responsible for reading and writing from the database and the cache doesn't interact with the database at all
11+
- Application queries data from cache first
12+
- if cache contains data return directly bypasses database
13+
- if not fetch from database, then stores in cache
14+
- The most common cache-aside systems are Memcached and Redis
15+
- **Cache-as-SoR (system-of-record)**: the application treats cache as the main data store and reads data from it and writes data to it
16+
- **Read through**
17+
- the cache is configured with a loader component that knows how to load data from the database
18+
- if an entry does not exist within the cache, the cache invokes the loader to retrieve the value from the database, then caches the value, then returns it to the caller.
19+
- **Write through**
20+
- the cache is configured with a writer component that knows how to write data to database
21+
- When the cache is asked to store a value for a key, the cache invokes the writer to store the value in the SoR, as well as updating the cache.
22+
- **Write behind**
23+
- Similar to write behind, rather than writing to the database while the thread making the update waits (as with write-through), write-behind queues the data for writing at a later time.
24+
- Cache Eviction Policies
25+
- LRU: least recently used
26+
- LFU: least frequently used
27+
- FIFO: first in first out
28+
- LIFO: last in first out
29+
- FILO: first in last out
30+
- and many many more
31+
32+
33+
## Redis
34+
35+
### Memory management
36+
37+
- Redis only caches all the key information. Not all data storage occurs in memory
38+
- When the physical memory is full, Redis may swap values not used for a long time to the disk.
39+
- When the memory usage exceeds the threshold value, Redis will trigger the swap operation.
40+
- Redis calculates the values for the keys to be swapped to the disk based on *“swappability = age\*log(size_in_memory)”*
41+
- The machine memory must keep all the keys and it will not swap all the data.
42+
- When Redis swaps the in-memory data to the disk, the main thread that provides services, and the child thread for the swap operation will share this part of memory.
43+
- So, if you update the data you intend to swap, Redis will block this operation, preventing the execution of such a change until the child thread completes the swap operation.
44+
45+
### Multi-threading
46+
47+
- Redis was known to be single threaded, but now its changed
48+
- After Redis 4.0, it also has background threads to process slow operations such as clean up, releasing useless connections, bulk delete, etc
49+
- Redis 6.0 version supporting multi-threading was finally released on 2020-05-02
50+
- There are two main directions for optimization:
51+
- To improve network IO performance, typical implementations such as using DPDK to replace the kernel network stack.
52+
- Use multi-threading to make full use of multi-core, typical implementations such as Memcached.
53+
54+
55+
56+
## Redis vs. Memcached: In-Memory Data Storage Systems
57+
58+
| Comparison | Redis | Memcached |
59+
| :---: | :--------: | :---: |
60+
| Data Types Supported | string, hash, list, set, sorted set | Hash Table with string and integers |
61+
| Server-end data operations | owns more data structures and supports richer data operations | need to copy the data to the client end for similar changes and then set the data back thus increases IO counts and data sizes |
62+
| Memory Management | Encapsulated malloc/free | Slab Allocation mechanism |
63+
| Memory use efficiency | Lower | higher memory utilization rate |
64+
| Data Persistence | RDB snapshot and AOF log | None |
65+
| Performance | single core so higher performance in small data storage | multiple cores so outperforms for storing data of 100k or above |
66+
67+
68+
Reference:
69+
70+
- [Caching for Resiliency](https://medium.com/the-cloud-architect/patterns-for-resilient-architecture-part-4-85afa66d6341#:~:text=There%20are%20two%20basic%20caching,%E2%80%94%20also%20called%20inline%2Dcache.)
71+
- [Database Caching](https://aws.amazon.com/caching/database-caching/)
72+
- [Cache Usage Patterns](https://www.ehcache.org/documentation/3.3/caching-patterns.html)
73+
- [Using Read-Through and Write-Through in Distributed Cache - DZone Database](https://dzone.com/articles/using-read-through-amp-write-through-in-distribute)
74+
- [Cache replacement policies](https://en.wikipedia.org/wiki/Cache_replacement_policies)
75+
- [Redis vs. Memcached: In-Memory Data Storage Systems](https://medium.com/@Alibaba_Cloud/redis-vs-memcached-in-memory-data-storage-systems-3395279b0941)
76+
- [Redis 6.0, which supports multi-threading, is finally released New features serial 13 questions - Programmer Sought](https://www.programmersought.com/article/30635498543/)

0 commit comments

Comments
 (0)