Testing Redis Operations
1. Using Redis CLI for Testing
| Flag | Description |
-x | Read last argument from stdin |
-r N -i seconds | Repeat command N times |
--no-raw / --raw | Control output formatting |
--csv | CSV output |
2. Implementing Unit Tests
@Test
void incrementsCounter() {
try (Jedis j = pool.getResource()) {
j.del("counter");
assertEquals(1L, j.incr("counter"));
assertEquals(5L, j.incrBy("counter", 4));
}
}
| Pattern | Detail |
| Isolation | Use a dedicated DB index or unique key prefix per test |
| Cleanup | FLUSHDB in @AfterEach on dedicated DB |
3. Using Redis Mock Libraries
| Library | Language |
| embedded-redis | Java — spawns real redis-server |
| Testcontainers Redis | JVM, .NET, Go, Python |
| fakeredis | Python — in-process simulation |
| ioredis-mock | Node.js |
4. Testing Transactions
Transaction tx = jedis.multi();
tx.set("a", "1");
tx.incr("a");
List<Object> results = tx.exec();
assertEquals(2L, results.get(1));
5. Testing Lua Scripts
| Tool | Use |
EVAL | Run inline script in tests |
SCRIPT LOAD + EVALSHA | Verify caching behavior |
redis-cli --eval file.lua | Run from file |
redis-benchmark -t set,get,lpush -n 500000 -P 32 --csv
7. Using --intrinsic-latency for Baseline Testing
| Command | Description |
redis-cli --intrinsic-latency 60 | Measure OS scheduling jitter for 60s |
8. Load Testing with Multiple Clients
| Flag | Description |
-c 200 | 200 concurrent clients |
-r 1000000 | 1M unique keys (avoid hot key cache) |
-t script load,evalsha | Script benchmark |
9. Testing Failover Scenarios
| Action | Test |
DEBUG SLEEP 60 on master | Trigger Sentinel detection |
SENTINEL FAILOVER <name> | Manual failover |
| kill -9 PID | Hard crash simulation |
10. Validating Data Integrity
| Tool | Use |
DEBUG DIGEST | Hash of entire dataset |
DEBUG DIGEST-VALUE key [key ...] | Per-key hash for diffs |
redis-check-rdb | Validate RDB file |
redis-check-aof | Validate AOF |
11. Testing Cluster Resharding
redis-cli --cluster reshard host:port \
--cluster-from <src-id> --cluster-to <dst-id> \
--cluster-slots 1000 --cluster-yes
| Verification | Method |
| Slot counts | CLUSTER COUNTKEYSINSLOT |
| Coverage | redis-cli --cluster check |
12. Simulating Network Partitions
| Tool | Description |
tc qdisc add ... netem loss 50% | Packet loss |
iptables -A INPUT -s peer -j DROP | Block peer traffic |
| Toxiproxy | Programmable network faults |
| CLIENT PAUSE | Application-level pause |