---
title: "Cache Invalidation in Express Doesn’t Have to Be Painful"
url: https://daily.dev/posts/cache-invalidation-in-express-doesn-t-have-to-be-painful-kdc0fraar
source_url: https://daily.dev/posts/cache-invalidation-in-express-doesn-t-have-to-be-painful-kdc0fraar
type: freeform
source: "Daniel Shan Balico"
author: "Daniel Shan Balico"
published: 2026-05-19T08:03:34.923Z
updated: 2026-05-19T08:03:55.302Z
tags: ["typescript", "redis", "express"]
reading_time: 2
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Cache Invalidation in Express Doesn’t Have to Be Painful

**[Daniel Shan Balico](https://daily.dev/sources/p2yytl0zi4lqo6trfpgku)** · [@dsbalico](https://daily.dev/dsbalico) · 2 min read · 0 upvotes · 0 comments

## Summary

express-tag-cache is a new npm package providing tag-based Redis caching middleware for Express. Instead of manually tracking and deleting individual cache keys, developers can assign shared tags to routes and invalidate all related caches at once. It supports dynamic tags per request parameter, programmatic usage outside middleware (for background jobs, queue workers, cron tasks), multi-tenant context, SHA-256 deterministic keys, size guards, and soft invalidation. Designed for microservices and high-traffic Express APIs where cache invalidation complexity grows quickly.

## Content

I got tired of manually clearing Redis keys every time data changed in my Express apps 😅

So I built [express-tag-cache](https://www.npmjs.com/package/express-tag-cache)

A high-performance tag-based Redis caching middleware for Express that makes cache invalidation actually manageable.

Instead of doing this:

❌ Delete individual cache keys manually

❌ Track dozens of related keys yourself

❌ Risk stale API responses everywhere

You can now do this:

```
// Cache related routes with shared tags
app.get('/api/products',
  cacheMiddleware.cache(['products']),
  handler
);

// Automatically invalidate all related caches
app.post('/api/products',
  cacheMiddleware.invalidate(['products']),
  handler
);
```

Every cached route sharing the `products` tag gets invalidated instantly.

Some things I focused on while building this:

⚡ Fast Redis-backed caching

🧠 Smart tag-based invalidation

🔒 SHA-256 deterministic cache keys

🏢 Multi-tenant app context support

📦 TypeScript-first developer experience

🚫 Built-in cache bypass handling

🛡️ Size guards to prevent Redis abuse

🧹 Soft invalidation support for better performance

One feature I personally love is dynamic tags:

```
app.get('/api/products/:id',
  cacheMiddleware.cache([
    'products',
    (req) => `products:${req.params.id}`
  ]),
  handler
);
```

Then invalidate only what changed:

```
app.put('/api/products/:id',
  cacheMiddleware.invalidate([
    'products',
    (req) => `products:${req.params.id}`
  ]),
  handler
);
```

It also supports direct programmatic usage if you want full control outside middleware flows:

```
// Store cache manually
await tagcache.set({
  key: 'user:123',
  value: JSON.stringify(user),
  tags: ['users', 'user:123']
});

// Retrieve cache
const cachedUser = await tagcache.get({
  key: 'user:123',
  tags: ['users']
});

// Invalidate by tags
await tagcache.invalidate({
  tags: ['user:123'],
  deleteCacheKeys: true
});
```

This makes it useful not just for Express routes, but also for:

• Background jobs

• Queue workers

• Cron tasks

• Service-to-service caching

• Complex business logic layers

This package was designed for real-world Express APIs where cache invalidation becomes painful fast — especially in microservices, multi-tenant systems, or high-traffic apps.

GitHub: [https://github.com/dsbalico/express-tag-cache](https://github.com/dsbalico/express-tag-cache)
NPM: [https://www.npmjs.com/package/express-tag-cache](https://www.npmjs.com/package/express-tag-cache)

## Similar posts on daily.dev

- [Tag-based cache invalidation now available for all responses](https://daily.dev/posts/tag-based-cache-invalidation-now-available-for-all-responses-zkapj5u4f) · Vercel · 0 upvotes · 0 comments

---

Tags: [#typescript](https://daily.dev/tags/typescript), [#redis](https://daily.dev/tags/redis), [#express](https://daily.dev/tags/express)

[View this post on daily.dev](https://daily.dev/posts/cache-invalidation-in-express-doesn-t-have-to-be-painful-kdc0fraar)
