Caching in TypeScript: Redis, Memcached, and Built-in Solutions

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Caching in TypeScript: Redis, Memcached, and Built-in Solutions



Modern web applications in TypeScript require high response speed and efficient resource management. One of the key tools for achieving this goal is caching. In this article, we will thoroughly examine three main approaches to caching in the TypeScript/Node.js ecosystem: using Redis, Memcached, and built-in solutions (in-memory cache). You will learn how to choose the right tool for your task and see working code examples.



Why is caching needed in TypeScript?



Caching allows you to temporarily store frequently requested data in fast storage to reduce response time and lower the load on databases or external APIs. In TypeScript running on Node.js, caching is especially useful for:

  • Speeding up repeated database queries;
  • Reducing latency when accessing external services;
  • Optimizing computationally intensive operations;
  • Managing user sessions.


Let's look at three main types of caching available to a TypeScript developer.



1. Built-in Caching (In-Memory Cache)



The simplest and fastest way is to store data in the memory of the Node.js process itself. You can use Map or more advanced libraries such as node-cache for this.



Advantages:

  • Maximum speed (no network calls);
  • Ease of implementation;
  • Does not require installing external services.


Disadvantages:

  • Data is lost when the application restarts;
  • Limited by the RAM of a single process;
  • Does not work in cluster mode (each process has its own cache).


Example using node-cache



Install the library:

npm install node-cache


Implementation of a simple caching service:

import NodeCache from 'node-cache';

class MemoryCacheService { private cache: NodeCache;

constructor(ttlSeconds: number = 600) { this.cache = new NodeCache({ stdTTL: ttlSeconds, checkperiod: 120 }); }

public get<T>(key: string): T | undefined { return this.cache.get<T>(key); }

public set<T>(key: string, value: T, ttl?: number): boolean { return this.cache.set(key, value, ttl); }

public has(key: string): boolean { return this.cache.has(key); }

public del(key: string): number { return this.cache.del(key); }

public flush(): void { this.cache.flushAll(); }}

// Usageconst cacheService = new MemoryCacheService(300); // TTL = 5 minutes

async function getData(id: string): Promise<any> { const cached = cacheService.get<any>(`data:${id}`); if (cached) { return cached; }

// Simulating an expensive operation const data = await fetchFromDatabase(id); cacheService.set(`data:${id}`, data); return data;}


2. Caching with Redis



Redis is a powerful in-memory data store that is often used as a cache. It supports complex data structures (strings, hashes, lists, sets) and can work in cluster mode.



Advantages:

  • Persistence (optional);
  • Support for complex data structures (e.g., sorted sets);
  • Built-in TTL (Time-To-Live) mechanisms;
  • Excellent performance (up to 100,000 operations/sec);
  • Pub/Sub support for message exchange.


Disadvantages:

  • Requires installing and configuring a separate server;
  • Uses more memory than built-in solutions;
  • Network latency (although minimal).


Example of working with Redis in Typ

Blogs

Book Recommendations