# Redis

Source : https://webgune.cloud/en-us/docs/db-redis · Mis à jour le 2026-10-27

# Redis

An in-memory store for anything that needs to be fast and transient: cache, sessions, queues, pub/sub.

## Connection string

```bash
REDIS_URL=redis://host:6379
# with password
REDIS_URL=redis://:password@host:6379
```

## ioredis (Node.js)

```typescript
import Redis from "ioredis";

const redis = new Redis(process.env.REDIS_URL!);

await redis.set("session:abc", JSON.stringify({ userId: 42 }), "EX", 3600);
const session = await redis.get("session:abc");
```

## Application-level cache

```typescript
async function getUser(id: string) {
  const cached = await redis.get(`user:${id}`);
  if (cached) return JSON.parse(cached);

  const user = await db.user.findUnique({ where: { id } });
  await redis.set(`user:${id}`, JSON.stringify(user), "EX", 300);
  return user;
}
```

## Lightweight queue (pub/sub)

```typescript
// producer
await redis.publish("jobs", JSON.stringify({ type: "send-email", to }));

// consumer
const sub = new Redis(process.env.REDIS_URL!);
sub.subscribe("jobs");
sub.on("message", (_channel, payload) => handleJob(JSON.parse(payload)));
```

> Redis is in-memory. Without specific persistence, data that cannot be recreated elsewhere should never live there alone: a user session can be regenerated, an account balance cannot.
