How to Add Redis Cache Expire and Memory‑Limit Experiments to ChaosBlade
This guide walks through the background, implementation, usage examples, and step‑by‑step open‑source contribution process for adding Redis cache expiration and memory‑limit chaos experiments to the ChaosBlade platform, including code snippets, Git workflow, and testing procedures.
Background
Operators need to simulate Redis failure scenarios such as forcing all keys to expire or triggering memory eviction. ChaosBlade adds two experiments: Redis cache expiration and Redis cache memory‑limit.
Redis Cache Expiration Experiment
Purpose
Simulate expiration of a single key or all keys to test application behavior on cache loss.
Implementation
Uses the go-redis client to call the EXPIRE command and modify the TTL of a specified key.
Usage
# Expire a key after 1 minute
blade create redis cache-expire --addr 192.168.56.101:6379 --password 123456 --key test1 --expiry 1m
# Set new expiry only when it is greater than the current TTL (option GT)
blade create redis cache-expire --addr 192.168.56.101:6379 --password 123456 --key test1 --option GT --expiry 1mDocumentation
https://chaosblade.io/docs/experiment-types/host/redis/blade_create_redis_cache_expire
Redis Cache Memory‑Limit Experiment
Purpose
Modify Redis maxmemory configuration to exceed the limit and trigger the eviction policy.
Implementation
Uses go-redis to change the maxmemory setting.
Usage
# Set maxmemory to 256M
blade create redis cache-limit --addr 192.168.56.101:6379 --password 123456 --size 256M
# Reduce maxmemory to 50% of the original value
blade create redis cache-limit --addr 192.168.56.101:6379 --password 123456 --percent 50Documentation
https://chaosblade.io/docs/experiment-types/host/redis/blade_create_redis_cache_limit
Contribution Guide for Adding the Experiments
Repository
Fork and clone the middleware repository, then create a development branch:
git clone https://github.com/chaosblade-io/chaosblade-exec-middleware
cd chaosblade-exec-middleware
git checkout -b devDirectory Structure
Create the Redis experiment package: mkdir -p exec/redis File redis.go defines the command specification:
package redis
import (
"github.com/chaosblade-io/chaosblade-spec-go/spec"
)
type RedisCommandSpec struct{ spec.BaseExpModelCommandSpec }
func (*RedisCommandSpec) Name() string { return "redis" }
func (*RedisCommandSpec) ShortDesc() string { return "Redis experiment" }
func (*RedisCommandSpec) LongDesc() string { return "Redis experiment" }
func NewRedisCommandSpec() spec.ExpModelCommandSpec {
return &RedisCommandSpec{
spec.BaseExpModelCommandSpec{
ExpActions: []spec.ExpActionCommandSpec{
NewCacheExpireActionSpec(),
NewCacheLimitActionSpec(),
},
ExpFlags: []spec.ExpFlagSpec{},
},
}
}Register the spec in model_darwin.go, model_linux.go, model_windows.go and in build/spec.go (see https://github.com/chaosblade-io/chaosblade-exec-middleware/blob/main/build/spec.go).
Implementation files for the two experiments:
https://github.com/chaosblade-io/chaosblade-exec-middleware/blob/main/exec/redis/redis_cache_expire.go
https://github.com/chaosblade-io/chaosblade-exec-middleware/blob/main/exec/redis/redis_cache_limit.go
Build
Compile the middleware for the target platform:
# macOS
make build_darwin
# Linux x86
make build_linux
# Linux ARM
make build_linux_armThe resulting binaries are placed in the target directory. Replace the existing chaos_middleware binary and chaosblade-middleware-spec-1.7.2.yaml on the test server with the new versions.
Testing
Run the experiments and verify Redis state before and after:
# Expire a key
blade create redis cache-expire --addr 192.168.56.101:6379 --password 123456 --key test1 --expiry 1m
# Limit memory
blade create redis cache-limit --addr 192.168.56.101:6379 --password 123456 --size 256MUse the Redis CLI to check TTL or maxmemory values to confirm the changes.
Pull Request Workflow
Commit changes with a signed commit message.
Push the dev branch to GitHub.
Create a Pull Request targeting main.
Address review comments and merge after approval.
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.
Alibaba Cloud Native
We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.
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.
