Redis client for TypeScript: ioredis and redis

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

Redis client for TypeScript: ioredis and redis



Redis is a high-performance in-memory data store, often used for caching, session management, message queues, and real-time applications. When developing in TypeScript (especially in a Node.js environment), you will need a reliable client to interact with Redis. Today, the two main candidates are the redis library (official from Redis) and ioredis (a popular third-party alternative). Both support TypeScript out of the box, but have differences in API, performance, and additional features.



1. The ioredis library



ioredis is a full-featured Redis client for Node.js, written in TypeScript. It is considered the de facto standard in the community due to its reliability, support for clusters, Sentinel, and built-in capabilities for working with data streams.



Installation

npm install ioredis# oryarn add ioredis


Connection and basic example

import Redis from 'ioredis';

const redis = new Redis({ host: 'localhost', port: 6379, // password: 'your-password', // if required retryStrategy: (times) => { // reconnection strategy return Math.min(times * 50, 2000); }});

redis.on('connect', () => { console.log('Connected to Redis');});

redis.on('error', (err) => { console.error('Redis error:', err);});

// Using async/awaitasync function demo() { await redis.set('user:1', JSON.stringify({ name: 'Ivan', age: 30 })); const user = await redis.get('user:1'); console.log('Retrieved data:', JSON.parse(user));}

demo();


Features of ioredis

  • Full TypeScript support — all methods have strict typing, allowing the IDE to suggest argument and result types.
  • Clusters and Sentinel — built-in support for Redis cluster mode and automatic failover.
  • Stream processing — a convenient interface for Redis Streams (XADD, XREAD, XGROUP).
  • Channels (Pub/Sub) — simple subscription to channels and receiving messages in real time.
  • Pipeline and transactions — grouping commands to improve performance.
  • Automatic reconnection — built-in retry strategy.


Pipeline example

import Redis from 'ioredis';

const redis = new Redis();

async function pipelineDemo() { const pipeline = redis.pipeline(); pipeline.set('key1', 'value1'); pipeline.set('key2', 'value2'); pipeline.get('key1'); pipeline.get('key2');

const results = await pipeline.exec(); console.log('Pipeline results:', results); // [ [ null, 'OK' ], [ null, 'OK' ], [ null, 'value1' ], [ null, 'value2' ] ]}

pipelineDemo();


2. The official redis library



Starting from version 4.x, the official redis client was completely rewritten in TypeScript. It offers a modern Promise-based API, a modular architecture, and deep integration with TypeScript.



Installation

npm install redis# oryarn add redis


Connection and example

import { createClient } from 'redis';

const client = createClient({ url: 'redis://localhost:6379' // or with password: 'redis://user:password@localhost:6379'});

client.on('error', (err) => console.error('Redis Client Error', err));

async function connectAndUse() { await client.connect(); console.log('Connected!');

await client.set('session:123', 'active'); const status = await client.get('session:123'); console.log('Session status:', status); // 'active'

await client.disconnect();}

connectAndUse();

Recommendations