System Design Latency and Throughputπ± Beginner
Caching is the ultimate weapon in System Design. It involves storing the result of an expensive, slow computation in ultra-fast memory (RAM) so subsequent users get the answer instantly.
What is Caching?
Imagine a restaurant. Cooking a complex soup takes 4 hours (a slow Database Query). If every customer had to wait 4 hours, the restaurant would fail. Instead, the chef cooks a massive batch in the morning and keeps it in a warm pot on the counter (the Cache). When a customer orders, it is served in 5 seconds.
Why is it the Best Optimization?
A standard SQL database reading from a hard drive might take 50 milliseconds to return complex joined data. A cache like Redis or Memcached reading from RAM returns it in 0.5 milliseconds. Adding a cache can speed up your application by a factor of 100x while simultaneously saving your database from crashing under load.
How to Implement Caching
The most common architecture is the Cache-Aside pattern:
1. User requests a viral news article.
2. The Server checks Redis (the Cache).
3. If found (Cache Hit!): Return the article instantly.
4. If NOT found (Cache Miss):
- The Server queries the slow PostgreSQL Database.
- The Server saves the result into Redis for the next user.
- Return the article to the user.