Database Management
Redis
Subjective
Oct 05, 2025
How do you connect to Redis and perform basic operations?
Detailed Explanation
**Connection Methods:**
**Redis CLI:**
redis-cli -h localhost -p 6379 -a password
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> SELECT 1 # switch to database 1
**Python (redis-py):**
import redis
# Basic connection
r = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)
# Connection pool (recommended)
pool = redis.ConnectionPool(host='localhost', port=6379, max_connections=20)
r = redis.Redis(connection_pool=pool)
# Operations
r.set('key', 'value', ex=3600) # expires in 1 hour
value = r.get('key')
r.delete('key')
**Node.js (ioredis):**
const Redis = require('ioredis');
const redis = new Redis({
host: 'localhost',
port: 6379,
retryDelayOnFailover: 100
});
await redis.set('key', 'value');
const value = await redis.get('key');
**Best Practices:**
• Use connection pooling
• Handle connection errors
• Set appropriate timeouts
• Use pipelines for bulk operations
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts