Guides/Server Setup
Server Setup7 min read

How to Install and Use Redis on Ubuntu

Redis is an in-memory data store used for caching, sessions, rate limiting, and queues. This guide covers installation, basic data structures, connecting from Node.js, and production configuration.

Install Redis

Install from the official Redis repository for the latest stable version: sudo apt update — curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg — add the repository — sudo apt install redis. Start and enable: sudo systemctl enable --now redis-server. Test: redis-cli ping (should return PONG). Check version: redis-server --version.

Basic Redis Data Structures

String (most common): SET key value EX 3600 (expires in 1 hour) — GET key. Hash (object): HSET user:1 name "Alice" email "a@b.com" — HGET user:1 name. List (queue/stack): LPUSH queue job1 — RPOP queue. Set (unique values): SADD tags nodejs python — SMEMBERS tags. Sorted Set (leaderboard): ZADD scores 100 "alice" — ZRANGE scores 0 -1 WITHSCORES. Each structure has O(1) lookups — that is why Redis is so fast.

Connect from Node.js with ioredis

Install: npm install ioredis. Connect: import Redis from "ioredis"; const redis = new Redis(process.env.REDIS_URL). Cache example: async function getCachedUser(id) { const cached = await redis.get(`user:${id}`); if (cached) return JSON.parse(cached); const user = await db.findUser(id); await redis.set(`user:${id}`, JSON.stringify(user), "EX", 3600); return user; }. This reduces database queries by serving cached data for 1 hour.

Configure Redis for Production

Edit /etc/redis/redis.conf. Set a password: requirepass yourStrongPassword. Bind to localhost only: bind 127.0.0.1. Disable debug commands: rename-command DEBUG "" — rename-command FLUSHALL "" — rename-command CONFIG "". Set max memory and eviction policy: maxmemory 256mb — maxmemory-policy allkeys-lru (evicts least-recently-used keys when full). Restart: sudo systemctl restart redis.

Use Redis for Rate Limiting

Rate limiting with Redis is fast and accurate across multiple server instances: async function isRateLimited(ip, limit=10, window=60) { const key = `ratelimit:${ip}`; const count = await redis.incr(key); if (count === 1) await redis.expire(key, window); return count > limit; }. This allows up to 10 requests per 60-second window per IP. Unlike in-memory Maps, this works correctly with multiple Node.js processes or servers.

Need Help?

Want this done for you?

Our engineering team handles implementations like this every week. Get a free scoping call — we will tell you exactly what it takes and what it costs.

Book a free call

© 2026 NexWorldTech — Built for Global Dominance.