Nextgen Blog
HomeLearnAIQuantumRoboticsSecuritySpaceAbout
Nextgen Blog

By Anubhav — full stack engineer, IIT Mandi CSE. Deep dives into AI, software engineering, and the technologies shaping the next generation of tech.

Navigate

  • Home
  • Learn
  • Blog
  • Archive
  • About

Topics

  • Artificial Intelligence
  • Quantum Computing
  • Robotics
  • Cybersecurity
  • Space Tech

Connect

Built with ❤️ & By - Anubhav

© 2026 Nextgen Blog by Anubhav. All rights reserved.

Privacy PolicyTerms of Service
Phase 03 · Partitioning, Replication & CachingChapter 17 of 33
16.
Phase 03Read time ~26 minDifficulty advanced

Caching Fundamentals

In this chapter
  • 01Introduction
  • 02Why Do We Need Caching?
  • 03What Problems Does Caching Solve?
  • 04Latency
  • 05Throughput
  • 06Scalability
  • 07Cost
  • 08Availability
  • 09The Basic Cache Mental Model
  • 10Cache Hit
  • 11Cache Miss
  • 12Cache Hit Ratio
  • 13Why Hit Ratio Matters
  • 14Hit ratio = 90%
  • 15Hit ratio = 50%
  • 16Cache Latency
  • 17Cache-Aside Pattern
  • 18Cache-Aside Example
  • 19Why Cache-Aside Is Popular
  • 20Read-Through Cache
  • 21Cache-Aside vs Read-Through
  • 22Write-Through Cache
  • 23Write-Back Cache
  • 24Write-Around Cache
  • 25Write Strategies Comparison
  • 26TTL
  • 27Why TTL Is Important
  • 28Choosing TTL
  • 29Cache Invalidation
  • 30Explicit Cache Invalidation
  • 31Update Cache After Database Write
  • 32Delete vs Update Cache
  • 33Delete cache
  • 34Update cache
  • 35Cache Eviction
  • 36LRU
  • 37LFU
  • 38LRU vs LFU
  • 39Cache Key Design
  • 40Cache Key Namespacing
  • 41Cache Serialization
  • 42Local Cache
  • 43Distributed Cache
  • 44Local vs Distributed Cache
  • 45Two-Level Cache
  • 46Cache Stampede
  • 47Preventing Cache Stampede
  • 48Request coalescing
  • 49Locking
  • 50TTL jitter
  • 51Background refresh
  • 52Cache Avalanche
  • 53Cache Penetration
  • 54Negative Caching
  • 55Cache Warming
  • 56Cache Failure
  • 57Cache as a Single Point of Failure
  • 58Cache Consistency
  • 59Strong vs Eventual Cache Consistency
  • 60Cache Invalidation Through Events
  • 61Cache-Control in HTTP
  • 62Browser Cache vs CDN Cache vs Application Cache
  • 63Cache Key Explosion
  • 64Cache Memory Management
  • 65Hot Keys
  • 66Cache Serialization Cost
  • 67Cache Capacity Planning
  • 68Caching and Database Protection
  • 69Cache Placement
  • 70What Should We Cache?
  • 71What Should We Avoid Caching?
  • 72Cache Security
  • 73Sensitive information leakage
  • 74Authorization mistakes
  • 75Shared cache mistakes
  • 76Cache poisoning
  • 77Practical Example: Product Catalog
  • 78Practical Example: User Profile
  • 79Practical Mini Project
  • 80GET
  • 81POST
  • 82PUT
  • 83Interview Questions
  • 84Beginner
  • 85Intermediate
  • 86Advanced
  • 87Interview Scenario
  • 88Question
  • 89Production Caching Checklist
  • 90Cache design
  • 91Consistency
  • 92Reliability
  • 93Performance
  • 94Failure protection
  • 95Security
  • 96Master Mental Model
  • 97Chapter Summary
  • 98Final Interview Checklist
  • 99What's Next?
  • 100Chapter 16 — Advanced Caching
In this chapter (100 sections)
01Introduction02Why Do We Need Caching?03What Problems Does Caching Solve?04Latency05Throughput06Scalability07Cost08Availability09The Basic Cache Mental Model10Cache Hit11Cache Miss12Cache Hit Ratio13Why Hit Ratio Matters14Hit ratio = 90%15Hit ratio = 50%16Cache Latency17Cache-Aside Pattern18Cache-Aside Example19Why Cache-Aside Is Popular20Read-Through Cache21Cache-Aside vs Read-Through22Write-Through Cache23Write-Back Cache24Write-Around Cache25Write Strategies Comparison26TTL27Why TTL Is Important28Choosing TTL29Cache Invalidation30Explicit Cache Invalidation31Update Cache After Database Write32Delete vs Update Cache33Delete cache34Update cache35Cache Eviction36LRU37LFU38LRU vs LFU39Cache Key Design40Cache Key Namespacing41Cache Serialization42Local Cache43Distributed Cache44Local vs Distributed Cache45Two-Level Cache46Cache Stampede47Preventing Cache Stampede48Request coalescing49Locking50TTL jitter51Background refresh52Cache Avalanche53Cache Penetration54Negative Caching55Cache Warming56Cache Failure57Cache as a Single Point of Failure58Cache Consistency59Strong vs Eventual Cache Consistency60Cache Invalidation Through Events61Cache-Control in HTTP62Browser Cache vs CDN Cache vs Application Cache63Cache Key Explosion64Cache Memory Management65Hot Keys66Cache Serialization Cost67Cache Capacity Planning68Caching and Database Protection69Cache Placement70What Should We Cache?71What Should We Avoid Caching?72Cache Security73Sensitive information leakage74Authorization mistakes75Shared cache mistakes76Cache poisoning77Practical Example: Product Catalog78Practical Example: User Profile79Practical Mini Project80GET81POST82PUT83Interview Questions84Beginner85Intermediate86Advanced87Interview Scenario88Question89Production Caching Checklist90Cache design91Consistency92Reliability93Performance94Failure protection95Security96Master Mental Model97Chapter Summary98Final Interview Checklist99What's Next?100Chapter 16 — Advanced Caching

1. Introduction#

Caching is one of the most important techniques in system design.

A cache stores frequently accessed data in a faster storage layer so future requests can be served without repeatedly performing an expensive operation.

The basic idea is:

Without Cache
 
Client
  |
  v
Application
  |
  v
Database
  |
  v
Response

With a cache:

Client
  |
  v
Application
  |
  v
 Cache
  |
  +---- HIT ----> Response
  |
  +---- MISS
        |
        v
     Database
        |
        v
      Cache
        |
        v
     Response

The key idea is:

Do expensive work once, store the result, and reuse it when possible.


2. Why Do We Need Caching?#

Databases and other persistent systems are much slower and more expensive to access than memory.

Consider:

Memory access
    ↓
Very fast
 
Database query
    ↓
Much slower
 
Remote API call
    ↓
Potentially even slower

Suppose an API receives:

100,000 requests/sec

and every request executes the same database query.

100,000 requests
       |
       v
   Database

The database may become the bottleneck.

With caching:

100,000 requests
       |
       v
     Cache
       |
   +---+---+
   |       |
 HIT     MISS
   |       |
   v       v
Response Database

If the cache serves most requests, database traffic can be dramatically reduced.


3. What Problems Does Caching Solve?#

Caching can improve:

1. Latency#

Memory-based cache lookups are typically much faster than database or network operations.

2. Throughput#

The application can serve more requests without increasing database load proportionally.

3. Scalability#

A cache absorbs repeated reads.

4. Cost#

Fewer expensive database or external-service operations may reduce infrastructure cost.

5. Availability#

In some architectures, cached data can remain available during temporary dependency failures, if the system explicitly supports stale/fallback behavior.

Caching does not automatically guarantee availability.


4. The Basic Cache Mental Model#

Think of a cache as:

Fast temporary copy
        +
Key
        +
Value
        +
Expiration policy

Example:

Key:
user:123
 
Value:
{
  "id": 123,
  "name": "Anubhav"
}

The application asks:

GET user:123

If present:

CACHE HIT

If absent:

CACHE MISS

5. Cache Hit#

A cache hit means the requested data exists in the cache.

Application
    |
    | GET user:123
    v
  Cache
    |
    | FOUND
    v
 Response

Example:

Request: user:123
 
Cache:
user:123 -> { ... }
 
Result:
HIT

This is the ideal fast path.


6. Cache Miss#

A cache miss means the requested value is not available in the cache.

Application
    |
    | GET user:123
    v
  Cache
    |
    | NOT FOUND
    v
 Database

The application then retrieves the data from the source and may populate the cache.


7. Cache Hit Ratio#

One of the most important cache metrics is the cache hit ratio.

Formula:

Cache Hit Ratio =
Cache Hits / Total Cache Requests

Example:

Total requests = 1,000,000
Cache hits = 900,000
 
Hit ratio =
900,000 / 1,000,000
= 90%

Therefore:

Cache Hit Ratio = 90%

The miss ratio is:

Miss Ratio = 1 - Hit Ratio

So:

Miss Ratio = 10%

8. Why Hit Ratio Matters#

Suppose:

Application requests = 100,000/sec

Hit ratio = 90%#

Approximately:

90,000 -> cache
10,000 -> database

Hit ratio = 50%#

Approximately:

50,000 -> cache
50,000 -> database

The database load is five times higher in the second case.

Therefore, cache effectiveness is not just about whether a cache exists.

It is about:

How much useful traffic the cache absorbs.


9. Cache Latency#

Suppose:

Cache = 1 ms
Database = 30 ms

A cache hit may avoid most of the database latency.

Without cache:

Request
  |
  v
Database
  |
  v
30 ms

With cache hit:

Request
  |
  v
Cache
  |
  v
1 ms

This can significantly improve user-perceived latency.


10. Cache-Aside Pattern#

The most common caching pattern is cache-aside.

The application manages the cache explicitly.

Flow:

1. Check cache
2. If HIT -> return
3. If MISS -> query database
4. Put result in cache
5. Return result

Diagram:

Application
    |
    v
  Cache
    |
  MISS
    |
    v
 Database
    |
    v
Application
    |
    v
  Cache

11. Cache-Aside Example#

Pseudocode:

async function getUser(userId) {
  const key = `user:${userId}`;
 
  const cached = await cache.get(key);
 
  if (cached) {
    return JSON.parse(cached);
  }
 
  const user = await database.getUser(userId);
 
  await cache.set(key, JSON.stringify(user), {
    ttl: 300
  });
 
  return user;
}

The database remains the source of truth.

The cache is an optimization layer.


12. Why Cache-Aside Is Popular#

Advantages:

  • simple
  • application controls caching
  • cache can be introduced without changing database semantics
  • only requested data is cached
  • easy to invalidate explicitly

Disadvantages:

  • application must implement cache logic
  • cache misses add latency
  • invalidation must be designed carefully
  • multiple services may implement inconsistent caching logic

13. Read-Through Cache#

With a read-through cache, the application asks the cache for data, and the cache itself retrieves missing data from the underlying source.

Conceptually:

Application
    |
    v
Read-Through Cache
    |
    +---- HIT -> return
    |
    +---- MISS -> Database

The application does not directly implement the cache-miss database lookup.

The exact implementation depends on the caching technology.


14. Cache-Aside vs Read-Through#

Property Cache-Aside Read-Through
Application checks cache Yes Usually no
Application handles miss Yes Cache layer
Control High More centralized
Complexity Application-side Cache infrastructure
Common use Very common Depends on platform

The important distinction is who is responsible for loading data after a miss.


15. Write-Through Cache#

In write-through caching, writes go through the cache and are synchronously propagated to the underlying data store according to the cache system's semantics.

Conceptually:

Application
    |
    v
  Cache
    |
    v
 Database

A write updates both layers as part of the write path.

Advantages:

  • cache stays relatively fresh
  • reads can immediately use cached data

Disadvantages:

  • writes may become slower
  • cache may store data that is rarely read
  • more write-path complexity

16. Write-Back Cache#

With write-back caching:

Application
    |
    v
  Cache
    |
    | later
    v
 Database

The cache acknowledges or stores the write first, then persists it to the underlying store later.

Advantages:

  • very fast writes
  • can batch writes
  • can reduce database write pressure

Risks:

  • data may be lost if cache data disappears before persistence
  • much more complex failure handling
  • durability semantics become critical

Write-back caching should only be used when its durability behavior is explicitly understood.


17. Write-Around Cache#

In write-around caching, writes go directly to the database rather than populating the cache.

Write
  |
  v
Database

Later:

Read
  |
  v
Cache MISS
  |
  v
Database
  |
  v
Cache

This can prevent large volumes of write-only data from polluting the cache.


18. Write Strategies Comparison#

Pattern Write Path Main Benefit Main Risk
Write-through Cache → DB Fresh cache Higher write latency
Write-back Cache → later DB Fast writes Durability complexity
Write-around DB only Avoids cache pollution First read misses

The correct choice depends on workload and consistency requirements.


19. TTL#

TTL means:

Time To Live

It specifies how long a cache entry remains valid.

Example:

Key:
product:123
 
TTL:
300 seconds

After 300 seconds, the entry expires according to the cache's expiration semantics.


20. Why TTL Is Important#

Without expiration:

Cache
 |
 +-- old data
 +-- old data
 +-- old data
 +-- old data

The cache can contain stale information indefinitely.

TTL provides automatic expiration.

Example:

Product Price
 
Cache:
₹999
 
TTL = 60 seconds

After expiration, the next request can fetch the latest value.


21. Choosing TTL#

TTL should depend on how quickly data changes and how much staleness is acceptable.

Examples:

Data Possible TTL Strategy
Product catalog Minutes
Weather Short
User profile Minutes
Static configuration Longer
Session data Based on session policy
Real-time stock Very short or explicit invalidation

There is no universal TTL.


22. Cache Invalidation#

One of the classic engineering statements is:

There are only a few hard things in computer science: cache invalidation and naming things.

The underlying problem is simple:

Database:
price = 1200
 
Cache:
price = 1000

The cache is stale.

When data changes, the system needs a strategy to ensure cached data eventually becomes acceptable.

Common approaches:

  • TTL expiration
  • explicit deletion
  • update cache after database write
  • versioned keys
  • event-driven invalidation

23. Explicit Cache Invalidation#

Suppose:

User 123

is cached:

user:123

A profile update happens.

The application can delete:

DELETE user:123

Then the next read:

Cache MISS
   |
   v
Database
   |
   v
New value cached

This is simple and effective for many use cases.


24. Update Cache After Database Write#

Another strategy:

1. Update database
2. Update cache

Example:

await db.updateUser(userId, data);
await cache.set(`user:${userId}`, JSON.stringify(data));

This can reduce stale reads.

But failures can occur:

Database update = SUCCESS
Cache update = FAILURE

Now the cache may remain stale.

Therefore production systems need to decide how to recover from partial failures.


25. Delete vs Update Cache#

After a database update, you can:

Delete cache#

DB update
   |
   v
DELETE cache

Next read repopulates it.

Update cache#

DB update
   |
   v
UPDATE cache

Which is better depends on:

  • object size
  • write frequency
  • read frequency
  • consistency requirements
  • failure handling

Deleting is often simpler because the database remains the source of truth.


26. Cache Eviction#

A cache has finite memory.

Suppose:

Cache capacity = 10 GB

Eventually:

New data arrives

The cache needs to remove something.

This is called eviction.


27. LRU#

LRU means:

Least Recently Used

The cache removes entries that have not been accessed recently.

Example:

A -> used recently
B -> used recently
C -> not used for long time
D -> used recently

If space is needed:

Evict C

LRU is useful when recently accessed data is likely to be accessed again.


28. LFU#

LFU means:

Least Frequently Used

It removes entries with the lowest access frequency.

Example:

A -> 10,000 accesses
B -> 5,000 accesses
C -> 2 accesses

If C is least frequently used:

Evict C

LFU can work well when popularity is stable.


29. LRU vs LFU#

Property LRU LFU
Based on Recent access Frequency
Good for Recency-driven workloads Popularity-driven workloads
Implementation complexity Moderate Higher
Problem Popular old item may leave Recently trending item may initially look cold

Modern caches may use more sophisticated policies.


30. Cache Key Design#

A cache key must uniquely identify the cached value.

Bad:

user

Better:

user:123

For parameterized data:

product:123
product:123:details
search:iphone:page:1

Good keys should be:

  • deterministic
  • unique
  • predictable
  • reasonably compact
  • easy to invalidate

31. Cache Key Namespacing#

Different data types should use clear namespaces.

Example:

user:123
order:987
product:456
session:abc

This prevents collisions.

Without namespaces:

123
456
987

the meaning of a key becomes ambiguous.


32. Cache Serialization#

Complex objects often need serialization.

Example:

const value = JSON.stringify(user);
 
await cache.set("user:123", value);

Then:

const value = await cache.get("user:123");
 
const user = JSON.parse(value);

Serialization has:

  • CPU cost
  • memory overhead
  • compatibility implications

For high-throughput systems, serialization format and object size can matter.


33. Local Cache#

A local cache lives inside the application process.

Application Server
   |
   +---- Local Memory Cache

Advantages:

  • extremely low latency
  • no network hop
  • simple for small workloads

Disadvantages:

  • each application instance has different cache contents
  • memory is limited
  • cache disappears when process restarts
  • invalidation across instances is difficult

Example:

Server A -> cache A
Server B -> cache B
Server C -> cache C

34. Distributed Cache#

A distributed cache is shared across application instances.

                 Application
               /      |      \
              v       v       v
           Server A Server B Server C
               \       |       /
                \      |      /
                 v     v     v
                +-----------+
                |   Cache   |
                +-----------+

Examples include systems such as Redis.

Advantages:

  • shared cache
  • consistent access point
  • easier horizontal scaling
  • cache survives individual application instance restarts

Disadvantages:

  • network latency
  • cache infrastructure becomes another dependency
  • availability and scaling need to be designed

35. Local vs Distributed Cache#

Property Local Cache Distributed Cache
Latency Very low Low, but network-dependent
Shared across servers No Yes
Memory Per application Central/shared cluster
Invalidation More difficult globally Centralized
Failure impact Usually isolated Shared dependency
Scaling Limited per instance Designed for shared workloads

Many production systems use both.


36. Two-Level Cache#

A two-level cache can look like:

Application
    |
    v
L1 Local Cache
    |
   MISS
    |
    v
L2 Distributed Cache
    |
   MISS
    |
    v
Database

This can reduce network traffic to the distributed cache.

Example:

L1 = process memory
L2 = Redis
L3 = database

But multi-level caches increase invalidation and consistency complexity.


37. Cache Stampede#

A cache stampede occurs when many requests simultaneously encounter an expired or missing cache entry.

Suppose:

Popular key

expires at:

12:00:00

At 12:00:01:

10,000 requests
      |
      v
Cache MISS
      |
      +----> DB
      +----> DB
      +----> DB
      +----> DB
      ...

The database suddenly receives a huge burst.

This can overload it.


38. Preventing Cache Stampede#

Common techniques:

1. Request coalescing#

Only one request loads the missing value.

10,000 requests
       |
       v
   One loader
       |
       v
   Database

Other requests wait for the result.

2. Locking#

Use a short-lived distributed lock around cache population.

3. TTL jitter#

Instead of every entry expiring at exactly the same time:

TTL = 300 + random(0, 30)

This spreads expirations.

4. Background refresh#

Refresh data before expiration.


39. Cache Avalanche#

A cache avalanche occurs when many cache entries become unavailable or expire around the same time.

Example:

Cache restart
      |
      v
Millions of misses
      |
      v
Database overload

Mitigation:

  • staggered TTLs
  • cache warming
  • rate limiting
  • request coalescing
  • gradual recovery
  • multiple cache nodes
  • fallback strategies

40. Cache Penetration#

Cache penetration occurs when requests repeatedly ask for data that does not exist.

Example:

GET user:999999999

Database:

NOT FOUND

If the system never caches the negative result:

Request
  |
  v
Cache MISS
  |
  v
Database
  |
  v
NOT FOUND

Repeated malicious or accidental requests can repeatedly hit the database.


41. Negative Caching#

A system can cache "not found" results for a short period.

Example:

user:999999999 -> NOT_FOUND
TTL = 30 seconds

Then repeated requests can be served from the cache.

The TTL should be short enough that newly created data can eventually become visible.


42. Cache Warming#

Cache warming means populating the cache before traffic arrives.

Example:

Deployment
   |
   v
Load popular products
   |
   v
Cache
   |
   v
Traffic arrives

This can reduce cold-start misses.

Useful for:

  • predictable traffic spikes
  • popular content
  • deployments
  • cache restarts
  • scheduled events

43. Cache Failure#

What happens if the cache is unavailable?

A common fallback is:

Application
   |
   v
Cache
   |
  FAIL
   |
   v
Database

But there is a danger.

If millions of requests suddenly bypass the cache:

Cache failure
     |
     v
Database
     |
     v
Overload

This is sometimes called a thundering herd or dependency-failure amplification.

Therefore cache failure handling must include:

  • database protection
  • rate limiting
  • request coalescing
  • circuit breakers
  • graceful degradation

44. Cache as a Single Point of Failure#

A cache should not necessarily become a single point of failure.

Instead of:

Application
     |
     v
One Cache Node

production systems may use:

          Cache Cluster
       /       |       \
      v        v        v
    Node A   Node B   Node C

with appropriate replication/failover behavior.

The exact design depends on the caching system.


45. Cache Consistency#

There is often a trade-off:

Freshness
   vs
Performance

Very aggressive caching:

High performance
+
Potentially stale data

Minimal caching:

Fresh data
+
Higher database load

The correct balance depends on business requirements.


46. Strong vs Eventual Cache Consistency#

For some data:

User profile picture

a few seconds of staleness may be acceptable.

For other data:

Bank balance

stale values may be unacceptable.

Therefore:

Do not ask only "Can we cache this?" Ask "How stale can this data safely be?"


47. Cache Invalidation Through Events#

Suppose:

Order Service
     |
     v
Database

After an update:

Order Updated Event
     |
     v
Cache Invalidation

Other services can subscribe to the event.

Architecture:

Database Update
      |
      v
Event
      |
      +----> Cache Service
      |
      +----> Search Index
      |
      +----> Analytics

This becomes particularly useful in event-driven architectures.


48. Cache-Control in HTTP#

Caching also exists at the HTTP/CDN layer.

Example:

Cache-Control: public, max-age=3600

This tells compatible caches that the response may be reused for the specified period.

Other directives include concepts such as:

no-store
no-cache
private
public
must-revalidate

Application caches and HTTP/CDN caches are different layers, but they use similar caching principles.


49. Browser Cache vs CDN Cache vs Application Cache#

There may be multiple cache layers:

Browser
   |
   v
CDN
   |
   v
Application Cache
   |
   v
Database

A request may be served before reaching the application at all.

This is why cache architecture should consider the complete request path.


50. Cache Key Explosion#

Suppose an API caches:

/search?q=iphone

But every tiny variation creates a new key:

/search?q=iphone&page=1
/search?q=iphone&page=2
/search?q=iphone&sort=price
/search?q=iphone&sort=rating

The cache may contain many low-value entries.

This is called cache fragmentation or key explosion.

Possible solutions:

  • normalize keys
  • cache only valuable query combinations
  • limit cache dimensions
  • use appropriate TTL
  • monitor key cardinality

51. Cache Memory Management#

Cache memory is finite.

Important metrics:

  • memory usage
  • eviction rate
  • hit ratio
  • item count
  • average item size
  • largest keys
  • hot keys
  • expired entries

A cache with high eviction rates may indicate:

  • insufficient capacity
  • poor TTL selection
  • too many unique keys
  • low-value data being cached

52. Hot Keys#

A hot key receives a disproportionate number of requests.

Example:

product:iphone-17

receives:

1 million requests/sec

Even if the cache has many nodes, one key can create a bottleneck depending on the architecture.

Potential solutions:

  • local caching
  • replicate the hot value
  • add randomized replicas
  • request coalescing
  • CDN caching
  • precomputation

53. Cache Serialization Cost#

Suppose a cached object is:

5 MB JSON

and receives:

10,000 requests/sec

Even if the cache lookup is fast, repeatedly:

deserialize 5 MB

can consume significant CPU and bandwidth.

Therefore cache performance is not just:

cache lookup latency

It also includes:

network
+
serialization
+
deserialization
+
memory
+
CPU

54. Cache Capacity Planning#

Suppose:

10 million objects

Average object size:

2 KB

Raw data:

10,000,000 × 2 KB
= approximately 20 GB

But actual cache capacity must account for:

  • metadata
  • fragmentation
  • replication
  • overhead
  • eviction headroom

Therefore you should not provision exactly 20 GB.

Capacity planning needs safety margin.


55. Caching and Database Protection#

One of the biggest benefits of caching is database protection.

Suppose:

Requests = 1,000,000/sec
Hit ratio = 95%

Then approximately:

Cache:
950,000/sec
 
Database:
50,000/sec

Instead of:

Database:
1,000,000/sec

This can completely change the scalability requirements of the database.


56. Cache Placement#

Caching can exist at many layers:

User
 |
 v
Browser Cache
 |
 v
CDN
 |
 v
Reverse Proxy
 |
 v
Application Local Cache
 |
 v
Distributed Cache
 |
 v
Database Buffer Pool
 |
 v
Storage

Different layers have different:

  • latency
  • scope
  • consistency
  • invalidation mechanisms
  • capacity

A strong system designer knows which layer should cache which data.


57. What Should We Cache?#

Good candidates often have:

  • high read frequency
  • expensive computation
  • relatively stable values
  • repeated access
  • predictable keys

Examples:

Product catalog
User profiles
Configuration
Feature flags
Popular content
Computed aggregations
Session data
API responses

58. What Should We Avoid Caching?#

Be cautious with:

  • highly personalized rapidly changing data
  • sensitive data without proper security controls
  • very large low-reuse objects
  • values requiring strict freshness
  • data with extremely low cache hit probability

The correct answer depends on the application's requirements.


59. Cache Security#

Caches can contain sensitive data.

Potential risks:

Sensitive information leakage#

If cache keys do not properly distinguish users:

user A -> cached response
user B -> receives user A data

Authorization mistakes#

Never assume:

"It came from the cache, so authentication is unnecessary."

Authorization still matters.

Shared cache mistakes#

A response intended to be private must not accidentally become publicly cacheable.

Cache poisoning#

Attackers may attempt to manipulate cached responses.

Cache security must be part of API and infrastructure design.


60. Practical Example: Product Catalog#

Suppose:

GET /products/123

is requested 50,000 times/sec.

Without cache:

50,000 requests
      |
      v
Database

With cache-aside:

50,000 requests
      |
      v
Redis
      |
  +---+---+
  |       |
 HIT     MISS
  |       |
  v       v
Response Database
          |
          v
        Redis

If hit ratio is 98%:

Cache = 49,000/sec
Database = 1,000/sec

This dramatically reduces database load.


61. Practical Example: User Profile#

Request:

GET /users/123

Flow:

1. Check user:123
2. If hit, return profile
3. Otherwise query DB
4. Cache profile for 5 minutes

On update:

PUT /users/123

Possible flow:

1. Update database
2. Delete user:123 from cache

Next read repopulates the cache.

This is a simple cache-aside design.


62. Practical Mini Project#

Build a small API with:

GET /products/:id
POST /products
PUT /products/:id

Use:

Application
    |
    +---- Cache
    |
    +---- Database

Implement:

GET#

Cache -> HIT -> return
Cache -> MISS -> DB -> Cache -> return

POST#

DB -> create product

PUT#

DB -> update product
Cache -> invalidate product

Add metrics:

cache_hits
cache_misses
cache_hit_ratio
cache_errors
database_fallbacks

Then simulate:

  • cache hit
  • cache miss
  • expiration
  • cache failure
  • database failure
  • high request volume

This exercise demonstrates the core production behavior of caching.


63. Interview Questions#

Beginner#

  1. What is caching?
  2. Why is caching useful?
  3. What is a cache hit?
  4. What is a cache miss?
  5. What is cache hit ratio?
  6. What is TTL?
  7. What is cache invalidation?
  8. What is LRU?

Intermediate#

  1. Explain cache-aside.
  2. Explain read-through caching.
  3. Explain write-through caching.
  4. Explain write-back caching.
  5. Explain write-around caching.
  6. What is cache stampede?
  7. What is cache penetration?
  8. What is cache avalanche?
  9. What is cache warming?
  10. Local cache vs distributed cache?

Advanced#

  1. How would you design caching for a high-traffic API?
  2. How would you prevent cache stampede?
  3. How would you handle cache failure?
  4. How would you cache personalized responses safely?
  5. How would you handle hot keys?
  6. How would you maintain consistency between database and cache?
  7. How would you design multi-level caching?
  8. How would you calculate required cache capacity?
  9. How would you invalidate millions of cached objects?
  10. How would you design a cache for a globally distributed application?

64. Interview Scenario#

Question#

Your API receives 500,000 requests/sec. The database can safely handle only 20,000 requests/sec. How can caching help?

Suppose:

Traffic = 500,000/sec
Database capacity = 20,000/sec

Required database traffic:

<= 20,000/sec

Maximum miss ratio:

20,000 / 500,000
= 4%

Therefore the target cache hit ratio would need to be approximately:

96%+

under this simplified model.

But a real design must also consider:

  • cache failures
  • uneven traffic
  • expensive queries
  • hot keys
  • write traffic
  • cache capacity
  • network latency

This is a good example of how system-design numbers connect architecture to capacity.


65. Production Caching Checklist#

Cache design#

  • What data is being cached?
  • Why is it cacheable?
  • What is the cache key?
  • What is the TTL?
  • What is the maximum object size?

Consistency#

  • How stale can data be?
  • How is invalidation performed?
  • What happens after a database update?
  • Which reads require fresh data?

Reliability#

  • What happens if cache is unavailable?
  • Can the database survive cache bypass?
  • Is the cache replicated?
  • Is cache recovery tested?

Performance#

  • Cache hit ratio monitored
  • Latency monitored
  • Eviction rate monitored
  • Hot keys monitored
  • Memory usage monitored
  • Network usage monitored

Failure protection#

  • Stampede protection
  • Negative caching where appropriate
  • TTL jitter
  • Request coalescing
  • Rate limiting
  • Graceful fallback

Security#

  • Sensitive data protected
  • User isolation enforced
  • Authorization preserved
  • Public/private cache behavior correct
  • Cache poisoning considered

66. Master Mental Model#

Use this mental model:

                    REQUEST
                       |
                       v
                    CACHE
                  /       \
               HIT         MISS
                |            |
                v            v
             RESPONSE      DATABASE
                              |
                              v
                            CACHE
                              |
                              v
                           RESPONSE

Then ask:

1. What should we cache?
2. What is the cache key?
3. How long should it live?
4. What happens on a miss?
5. How do we invalidate it?
6. What happens if it becomes stale?
7. What happens if the cache fails?
8. What happens when many requests miss simultaneously?
9. How do we handle hot keys?
10. How much cache capacity do we need?

If you can answer these questions, you understand the fundamentals of production caching.


67. Chapter Summary#

In this chapter, we learned:

  • what caching is
  • why caching improves latency and scalability
  • cache hit
  • cache miss
  • cache hit ratio
  • cache latency
  • cache-aside
  • read-through caching
  • write-through caching
  • write-back caching
  • write-around caching
  • TTL
  • cache invalidation
  • cache eviction
  • LRU
  • LFU
  • cache key design
  • key namespaces
  • serialization
  • local caches
  • distributed caches
  • multi-level caching
  • cache stampede
  • cache avalanche
  • cache penetration
  • negative caching
  • cache warming
  • cache failure handling
  • cache consistency
  • event-driven invalidation
  • HTTP/CDN caching
  • cache fragmentation
  • hot keys
  • capacity planning
  • cache security
  • production caching architecture

The central idea is:

Caching moves frequently needed data closer to the consumer, reducing latency and protecting slower dependencies. The difficult part is not storing data—it is deciding what to cache, how long to keep it, how to invalidate it, and how to behave when the cache fails.


68. Final Interview Checklist#

Before moving forward, make sure you can explain:

  • Cache
  • Cache hit
  • Cache miss
  • Hit ratio
  • TTL
  • Cache-aside
  • Read-through
  • Write-through
  • Write-back
  • Write-around
  • Cache invalidation
  • LRU
  • LFU
  • Local cache
  • Distributed cache
  • Multi-level cache
  • Cache stampede
  • Cache avalanche
  • Cache penetration
  • Negative caching
  • Cache warming
  • Hot keys
  • Cache consistency
  • Cache failure handling
  • Cache capacity planning
  • Cache security
  • Database protection through caching
  • CDN vs application caching

69. What's Next?#

Chapter 16 — Advanced Caching#

Next we will go deeper into production-grade caching.

We will cover:

  • Redis architecture
  • Redis data structures
  • distributed cache clustering
  • cache partitioning
  • cache replication
  • cache eviction policies in practice
  • distributed locks
  • request coalescing
  • stale-while-revalidate
  • refresh-ahead
  • probabilistic caching techniques
  • cache consistency strategies
  • cache invalidation at scale
  • hot-key mitigation
  • cache warming at scale
  • multi-region caching
  • cache failover
  • cache observability
  • Redis-based rate limiting
  • Redis sessions
  • distributed counters
  • production caching architectures
  • advanced interview problems

Finished this chapter?

Mark it as complete to track your mastery.

Previous ChapterChapter 14 — Database Partitioning & ShardingNext ChapterChapter 16 — Advanced Caching
PrevNext