DEV Community

Cover image for Code Smell 49 - Caches

Code Smell 49 - Caches

Maxi Contieri
Learn something new every day. - I am a senior software engineer working in industry, teaching and writing on software design, SOLID principles, DDD and TDD.
・1 min read

Caches are sexy. They are a one-night stand. We need to avoid them in a long-term relationship.


  • Coupling

  • Testability

  • Cache invalidation.

  • Maintainability

  • Premature Optimization

  • Erratic Behavior

  • Lack of transparency

  • Non-Deterministic behavior


  1. If you have a conclusive benchmark and are willing to pay for some coupling: Put an object in the middle.

  2. Unit test all your invalidation scenarios. Experience shows we face them in an incremental way.

  3. Look for a real world cache metaphor and model it.

Sample Code




This is a design smell.

It will be difficult to enforce by policy.


  • Premature Optimization


Caches should be functional and intelligent

In this way we can manage invalidation.

General purpose caches are suitable only for low level objects like operating systems, files and streams.

We shouldn't cache domain objects.

This page is hosted on a cached website.


More Info

Common Cache Usages

Caching Patterns


Photo by Aimee Vogelsang on Unsplash

There are only two hard things in Computer Science: cache invalidation and naming things.

Phil Karlton

Discussion (0)