Database Management
Redis
Subjective
Oct 05, 2025
How do you tune Redis performance for high-throughput applications?
Detailed Explanation
Redis performance tuning strategies:
• Configuration Optimization:**
# redis.conf optimizations
# Memory
maxmemory 2gb
maxmemory-policy allkeys-lru
# Networking
tcp-keepalive 300
timeout 0
tcp-backlog 511
# Persistence
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
# Slow log
slowlog-log-slower-than 10000
slowlog-max-len 128
• Client-Side Optimizations:**
# Connection pooling
pool = redis.ConnectionPool(
host='localhost',
port=6379,
max_connections=50,
retry_on_timeout=True
)
r = redis.Redis(connection_pool=pool)
# Pipelining for bulk operations
pipe = r.pipeline(transaction=False)
for i in range(1000):
pipe.set(f'key:{i}', f'value:{i}')
results = pipe.execute()
# Use appropriate data structures
# Hash for objects
r.hmset('user:1000', {'name': 'John', 'email': 'john@example.com'})
# Sorted sets for rankings
r.zadd('leaderboard', {'player1': 1500, 'player2': 1200})
• Memory Optimization:**
# Compression settings
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
set-max-intset-entries 512
zset-max-ziplist-entries 128
# Monitor memory usage
INFO memory
MEMORY USAGE key
redis-cli --bigkeys
• Monitoring and Alerting:**
def monitor_performance():
info = r.info()
# Key metrics to track
ops_per_sec = info['instantaneous_ops_per_sec']
memory_usage = info['used_memory']
hit_ratio = info['keyspace_hits'] / (info['keyspace_hits'] + info['keyspace_misses'])
connected_clients = info['connected_clients']
# Alert thresholds
if ops_per_sec > 100000:
print('High load detected')
if hit_ratio < 0.9:
print('Low cache hit ratio')
if connected_clients > 1000:
print('High client connections')
• Scaling Strategies:**
• Use Redis Cluster for horizontal scaling
• Implement read replicas for read-heavy workloads
• Use Redis Sentinel for high availability
• Consider Redis modules for specialized use cases
**Performance Testing:**
# Benchmark Redis performance
redis-benchmark -h localhost -p 6379 -n 100000 -c 50
redis-benchmark -t set,get -n 100000 -q
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts