Redis Performance Testing

Performance testing is an important way to evaluate Redis performance. This lesson covers using redis-benchmark for testing.

Introduction to redis-benchmark

redis-benchmark is Redis's built-in performance testing tool that can test various Redis operations.

Basic Usage

BASH
# Basic test (default 100000 requests, 50 concurrent clients)
redis-benchmark

# Specify host and port
redis-benchmark -h 127.0.0.1 -p 6379

# Specify request count and concurrency
redis-benchmark -n 100000 -c 100

Common Parameters

Parameter Description Default
-h Host address 127.0.0.1
-p Port 6379
-n Number of requests 100000
-c Number of parallel clients 50
-d Data size (bytes) 3
-k Use keepalive 1
-q Quiet output false
-t Commands to test all
-P Pipeline size 1

Interpreting Test Results

Sample Output

TEXT
====== SET ======
  100000 requests completed in 1.23 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

99.00% <= 1 milliseconds
99.80% <= 2 milliseconds
100.00% <= 3 milliseconds
81300.81 requests per second

====== GET ======
  100000 requests completed in 1.15 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

99.00% <= 1 milliseconds
99.90% <= 2 milliseconds
100.00% <= 3 milliseconds
86956.52 requests per second

Key Metrics

Metric Description
requests per second QPS (Queries Per Second)
99.00% <= 1 milliseconds 99% of requests completed within 1 ms
parallel clients Number of concurrent clients
payload Data size

Common Test Scenarios

1. Testing SET Operations

BASH
# Test SET operation
redis-benchmark -t set -n 100000 -c 50

# Test SET with pipeline
redis-benchmark -t set -n 100000 -c 50 -P 10

2. Testing GET Operations

BASH
# Test GET operation
redis-benchmark -t get -n 100000 -c 50

3. Testing Specific Commands

BASH
# Test specific commands
redis-benchmark -t set,get,incr,lpush,rpush,lpop,rpop,sadd,hset -n 100000 -c 50

4. Testing Pipeline

BASH
# Use pipeline (batch send commands)
redis-benchmark -t set -n 100000 -c 50 -P 10

# Compare performance with and without pipeline
redis-benchmark -t set -n 100000 -c 50 -P 1
redis-benchmark -t set -n 100000 -c 50 -P 10
redis-benchmark -t set -n 100000 -c 50 -P 100

5. Testing Large Data

BASH
# Test with large data size
redis-benchmark -t set -n 100000 -c 50 -d 1024

# Test different data sizes
redis-benchmark -t set -n 100000 -c 50 -d 64
redis-benchmark -t set -n 100000 -c 50 -d 256
redis-benchmark -t set -n 100000 -c 50 -d 1024

Performance Optimization Tips

1. Use Pipeline

Pipelining can significantly improve performance by reducing network round trips:

BASH
# Without pipeline
redis-benchmark -t set -n 100000 -c 50 -P 1
# Result: ~80000 QPS

# With pipeline
redis-benchmark -t set -n 100000 -c 50 -P 10
# Result: ~200000 QPS

2. Use Batch Commands

Batch commands are more efficient than individual commands:

BASH
# Individual commands
redis-benchmark -t set -n 100000 -c 50
# Result: ~80000 QPS

# Batch commands (MSET)
redis-benchmark -t mset -n 100000 -c 50
# Result: ~150000 QPS

3. Tune Configuration

BASH
# Adjust max memory
redis-cli CONFIG SET maxmemory 256mb

# Adjust max client connections
redis-cli CONFIG SET maxclients 10000

# Disable persistence (for testing)
redis-cli CONFIG SET save ""
redis-cli CONFIG SET appendonly no

Performance Baseline Reference

Single Instance Performance

Operation QPS (no pipeline) QPS (pipeline P=10)
SET 80,000 200,000
GET 90,000 250,000
INCR 85,000 220,000
LPUSH 80,000 200,000
LRANGE (100) 30,000 80,000
💡 Note: Actual performance depends on hardware, network environment, data size, and other factors.

Custom Test Scripts

Using Lua Scripts

BASH
# Create a test script
cat > test.lua << 'EOF'
for i = 1, 100000 do
  redis.call('SET', 'key:' .. i, 'value:' .. i)
end
EOF

# Execute the test
redis-cli --eval test.lua

Using Python

PYTHON
import redis
import time

r = redis.Redis(host='localhost', port=6379)

# Test SET operation
start = time.time()
for i in range(100000):
    r.set(f'key:{i}', f'value:{i}')
end = time.time()

print(f'SET: {100000 / (end - start):.0f} QPS')

❓ FAQ

Q Do redis-benchmark results differ from production?
A Yes. redis-benchmark runs in an ideal environment. Actual production performance is affected by network, concurrency, data size, etc.
Q How do I test Redis cluster performance?
A Use redis-benchmark with specific node parameters, or use dedicated cluster testing tools.
Q What pipeline size is recommended?
A Typically 10-100, depending on network environment and data size.

📖 Summary

📝 Exercises

  1. Basic test: Use redis-benchmark to test SET and GET operations
  2. Pipeline test: Compare performance with and without pipelining
  3. Optimization test: Try different configuration parameters and observe performance changes

Next Lesson

In the next lesson, we will learn Redis Pipelining, understanding the principles and usage of pipelining.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏