Step-by-Step Guide to Building and Benchmarking a Redis Master‑Slave Architecture
This article provides a comprehensive tutorial on setting up a one‑master‑one‑slave Redis replication environment, configuring read‑write separation, verifying data synchronization, testing read‑only behavior, and performing performance benchmarking with redis‑benchmark, complete with command examples and result analysis.
Hello, I am Wukong. After last week’s deep dive into Redis master‑slave principles, many readers appreciated the plain‑language explanation.
Prerequisite Articles
Previous cache‑practice series (parts 1‑7) are linked for reference.
Master‑Slave Architecture Setup
The setup consists of one master node handling writes and one slave node handling reads; successful data copy from master to slave confirms the architecture.
Redis Environment Installation
Install Redis on both machines following the official documentation.
Configure Master‑Slave and Read‑Write Separation
Required Settings
Slave read‑only flag (slave‑read‑only) – default, no change needed.
Authentication passwords: masterauth on the slave, requirepass on the master.
Slave‑of configuration to bind the slave to its master.
Bind Redis to the machine’s IP address.
Configuration Steps
(1) Shut down Redis on both servers: redis-cli shutdown (2) On the second machine (slave), edit /etc/redis/6379.conf:
vi /etc/redis/6379.conf
slave-read-only yes
masterauth abc123
slaveof eshop-cache01 6379
bind 192.168.10.115(3) On the first machine (master), edit /etc/redis/6379.conf:
vi /etc/redis/6379.conf
slave-read-only yes
requirepass abc123
bind 192.168.10.86(4) Restart Redis instances, starting the master first then the slave:
cd /etc/init.d
./redis_6379 start(5) Verify that Redis processes are running: ps -ef | grep redis (6) Check slave information:
# Connect to the slave
redis-cli -h 192.168.10.115 -p 6379 -a abc123
# Show replication info
info replication(7) Check master information:
# Connect to the master
redis-cli -h 192.168.10.86 -p 6379 -a abc123
# Show replication info
info replicationOpen port 6379 on both machines if needed:
iptables -A INPUT -p tcp --dport 6379 -j ACCEPTTesting Master‑Slave Synchronization
Verify Existing Data Sync
Check that keys are identical on both nodes: get keys Result shows matching key count and names.
Check a specific key: get key11 Values match, confirming synchronization.
Real‑Time Sync Test
On the master, set a new key: set key12 120 On the slave, retrieve it: get key12 The slave returns 120, proving real‑time replication.
Read‑Only Slave Test
Attempting to write on the slave should fail, confirming the read‑only setting.
Benchmarking the Master‑Slave Setup
Redis includes the redis-benchmark tool.
Typical benchmark command:
redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests>] [-k <boolean>]We use the default parameters: 50 concurrent clients, 100 000 total requests, 2‑byte payload.
Concurrency: 50
Total requests: 100 000
Data size per GET/SET: 2 bytes
Run the benchmark against the slave:
cd /usr/local/bin
./redis-benchmark -h 192.168.10.115 -p 6379 -c 50 -n 100000 -d 2Sample GET command result:
====== GET ======
100000 requests completed in 1.51 seconds
50 parallel clients
2 bytes payload
keep alive: 1
99.57% <= 1 milliseconds
99.77% <= 4 milliseconds
99.86% <= 5 milliseconds
99.92% <= 6 milliseconds
99.94% <= 7 milliseconds
99.99% <= 9 milliseconds
100.00% <= 9 milliseconds
66050.20 requests per secondInterpretation:
100 000 requests finished in 1.51 s.
99.57% completed within 1 ms.
All requests finished within 9 ms.
Throughput reached ~66 k requests/second.
Other command results are similar.
With the setup and benchmark complete, refer to the detailed article on master‑slave principles for deeper understanding.
Community Invitation
The author also promotes a knowledge‑sharing community (“星球”) with daily study challenges and interview questions, offering a low‑cost subscription.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
