- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
? What is Redis?Redis is not just a cache—it's a blazing-fast, in-memory data structure store that can do so much more. Let’s dive deep into what Redis is, why you should use it, and how to integrate it effectively.
Redis (REmote DIctionary Server) is an open-source, in-memory data store that can be used as:
- A database
- A cache
- A message broker
- A queue system
It stores everything in memory (RAM), making it ultra-fast
? Redis Architecture Overview
Here’s a simplified visual of how Redis fits into your application stack:
Core concepts:
- Key-Value Store – Basic data model.
- Data Structures – Strings, Lists, Sets, Sorted Sets, Hashes, Streams.
- Pub/Sub – Real-time communication between services.
- Persistence – Save data with RDB snapshots or AOF.
- High Availability – Use Sentinel or Cluster Mode.
| Use Case | Redis Feature |
|---|---|
| Caching | TTL, Memory LRU |
| Real-Time Chat | Pub/Sub |
| Task Queues | Lists, Streams |
| Leaderboards | Sorted Sets |
| Session Storage | Key-Value Expiry |
| Feature | Redis | SQL/NoSQL DBs |
|---|---|---|
| Speed | ? Super fast (RAM-based) | Slower (Disk-based) |
| Persistence | Optional (AOF/RDB) | Always |
| Data Types | Rich (List, Set, etc.) | Limited or JSON-only |
| Use Case | Real-time, caching | Complex querying |
? Local Installation
# Ubuntu
sudo apt update
sudo apt install redis-server
# Start Redis
redis-server
? Docker
docker run --name redis-server -p 6379:6379 redis
? Connect Redis with Node.js
Install ioredis or redis npm package:
npm install ioredis
Sample usage:
import Redis from 'ioredis';
const redis = new Redis();
// Set a key
await redis.set("user:123", "John");
// Get a key
const name = await redis.get("user:123");
console.log(name); // John
import express from 'express';
import Redis from 'ioredis';
const app = express();
const redis = new Redis();
app.get('/data', async (req, res) => {
const cached = await redis.get("data");
if (cached) {
return res.send(JSON.parse(cached));
}
const data = await fetchFromDatabase();
await redis.set("data", JSON.stringify(data), 'EX', 60); // Expires in 60 sec
res.send(data);
});
? Pub/Sub Example
const pub = new Redis();
const sub = new Redis();
sub.subscribe("chat");
sub.on("message", (channel, message) => {
console.log(`New message on ${channel}: ${message}`);
});
pub.publish("chat", "Hello Redis!");
? Other Helpful Diagrams
Redis Use Case Map
? When NOT to Use Redis
- When you need complex queries (joins, filters, aggregations).
- When data loss is unacceptable (unless persistence is configured well).
- When you're working with large datasets that don’t fit into memory.
const key = `rate:${userId}`;
const count = await redis.incr(key);
if (count === 1) await redis.expire(key, 60); // 60 sec window
if (count > 10) {
return res.status(429).send("Too many requests");
}
? Conclusion
Redis is lightning-fast, versatile, and easy to integrate into modern applications. Whether you use it for caching, queues, or pub/sub, it can significantly improve performance and scalability.
Start small—add it as a cache layer—and you’ll be hooked in no time!