🟢 Node.js Intermediate

What is Redis and how is it used in Node.js applications?

Answer

Redis (Remote Dictionary Server) is an in-memory data structure store used as a database, cache, message broker, and session store. It is extremely fast (microsecond latency) because all data lives in RAM. Common Node.js use cases: (1) Caching: store expensive database query results with TTL — await client.setEx("users:list", 3600, JSON.stringify(users));; (2) Session storage: use connect-redis with express-session for distributed sessions; (3) Rate limiting: atomic increment operations make Redis ideal for counting requests per window; (4) Pub/Sub: real-time messaging between services; (5) Job queues: BullMQ uses Redis for reliable background job processing; (6) Leaderboards: sorted sets for real-time rankings. Node.js clients: ioredis (feature-rich, supports clusters and Sentinel) and redis (official client). Always set expiry (TTL) on cached keys to avoid stale data accumulation.