DEV Community

Cover image for Caching: The Performance Trick That Can Also Break Your Application
Soumyajit Mukherjee
Soumyajit Mukherjee

Posted on

Caching: The Performance Trick That Can Also Break Your Application

Caching is one of the most effective ways to improve application performance.

It can also produce some of the strangest bugs developers encounter.

Why Cache?

Suppose a page repeatedly requests the same expensive data.

Without caching:

Request
  ↓
Application
  ↓
Database
  ↓
Response
Enter fullscreen mode Exit fullscreen mode

With caching:

Request
  ↓
Cache
  ↓
Response
Enter fullscreen mode Exit fullscreen mode

The expensive operation can potentially be avoided.

Cache-Aside Pattern

One common strategy is:

let value = await cache.get(key);

if (!value) {
  value = await database.getData();
  await cache.set(key, value);
}

return value;
Enter fullscreen mode Exit fullscreen mode

The application checks the cache first.

If the data isn't there, it loads the data from the database and stores it in the cache.

The Hard Part: Invalidation

Imagine you cache:

user:123
Enter fullscreen mode Exit fullscreen mode

Then the user changes their name.

The database now says:

Alex Smith
Enter fullscreen mode Exit fullscreen mode

but the cache still says:

Alex Johnson
Enter fullscreen mode Exit fullscreen mode

Your application now returns stale information.

This is why developers often repeat:

There are only a few hard things in computer science, and cache invalidation is one of them.

TTL

A time-to-live can automatically expire cached values.

product:123
TTL: 300 seconds
Enter fullscreen mode Exit fullscreen mode

This doesn't eliminate stale data completely, but it limits how long it can remain.

Cache Everything?

No.

Caching data that changes frequently can create complexity without providing much benefit.

Final Thoughts

Caching should be introduced around measurable performance problems.

First understand:

What is expensive?
How frequently is it requested?
How often does it change?
How stale can it safely be?
Enter fullscreen mode Exit fullscreen mode

Then design the cache.

Top comments (0)