Caching, Queues, and Real-Time Systems
Learn Redis caching, background jobs, message queues, WebSockets, Socket.IO concepts, and event-driven architectures.
Caching with Redis
Caching reduces latency and database load by storing frequently accessed data in a fast key-value store. Redis is commonly used for application caching, distributed locks, counters, sessions, rate limiting, and ephemeral data.
A cache strategy must define expiration, invalidation, consistency expectations, and behavior when the cache is unavailable.
const cached = await redis.get(`user:${id}`);
if (cached) {
return JSON.parse(cached);
}
const user = await User.findById(id).lean();
await redis.set(`user:${id}`, JSON.stringify(user), { EX: 300 });
return user;- Caching is a trade-off between performance and consistency.
- Every cache should have an invalidation or expiration strategy.
- The application should define what happens when the cache is unavailable.
What is the cache-aside pattern?
Message Queues and Background Jobs
Queues move slow or retryable work out of synchronous HTTP requests. Typical background jobs include sending emails, processing images, generating reports, consuming events, and integrating with third-party APIs.
A queue-based architecture improves responsiveness and provides controlled retries, concurrency, and failure handling.
- Queues decouple request handling from asynchronous background work.
- Workers should be idempotent when retries are possible.
- Dead-letter or failure handling is important for permanently failing jobs.
Why use a background queue for email delivery?
WebSockets and Real-Time Communication
WebSockets provide a persistent, bidirectional connection between a client and server. They are useful for chat, live notifications, dashboards, multiplayer applications, and collaborative experiences.
Socket.IO builds higher-level features around real-time communication, including rooms, events, reconnection behavior, and adapters for distributed deployments.
import { Server } from 'socket.io';
const io = new Server(httpServer, {
cors: { origin: '[https://example.com](https://example.com)' }
});
io.on('connection', (socket) => {
socket.on('message', (message) => {
io.emit('message', message);
});
});- WebSockets enable bidirectional real-time communication.
- Distributed Socket.IO deployments need an appropriate adapter or shared infrastructure.
- Authentication and authorization also apply to socket connections.
What is a key characteristic of WebSockets?