id: redis-replica-health-guard
namespace: company.team
description: |
Check a Redis primary and its replicas: every expected replica connected and online on the
primary, each replica's link to the primary up, its offset close to the primary's, and
replica-read-only on, so no client writes data on a replica that the next resync silently
deletes. Alert Slack, and optionally turn replica-read-only back on with CONFIG SET, then
read INFO again to verify. Lagging or disconnected replicas are reported, never changed.
inputs:
- id: primary_host
type: STRING
displayName: Primary host
defaults: "redis-primary"
- id: replica_hosts
type: ARRAY
itemType: STRING
displayName: Replica hosts
defaults:
- redis-replica-1
- redis-replica-2
- id: port
type: INT
displayName: Port
defaults: 6379
- id: max_lag_bytes
type: INT
displayName: Maximum replication lag (bytes)
defaults: 1048576
- id: use_password
type: BOOL
displayName: Authenticate with REDIS_PASSWORD
description: Send the REDIS_PASSWORD secret to the primary. Replicas are read
without a password; add the same property to their tasks if they require
one.
defaults: false
- id: network
type: STRING
displayName: Docker network for redis-cli
description: Network of the redis-cli containers. Leave empty for Docker's
default bridge.
defaults: ""
- id: auto_remediate
type: BOOL
displayName: Auto remediate
description: Only replica-read-only is changed. Lag and links are reported.
defaults: false
- id: dry_run
type: BOOL
displayName: Dry run
description: Must be false, together with auto_remediate, for CONFIG SET to run.
defaults: true
tasks:
- id: primary_info
type: io.kestra.plugin.redis.cli.RedisCLI
description: Read-only.
host: "{{ inputs.primary_host }}"
port: "{{ inputs.port }}"
password: "{{ inputs.use_password ? secret('REDIS_PASSWORD') : null }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: "{{ inputs.network }}"
commands:
- INFO replication
- id: replicas_info
type: io.kestra.plugin.core.flow.Loop
description: Read-only. INFO replication and CONFIG GET replica-read-only on
every replica.
values: "{{ inputs.replica_hosts | toJson }}"
outputs:
- id: state
type: STRING
value: >-
{{ {'host': item.value, 'info': (outputs.rinfo.vars.command_1 ?? ''),
'ro': (outputs.rro.vars.command_1 ?? '')} | toJson }}
tasks:
- id: one
type: io.kestra.plugin.core.flow.Sequential
allowFailure: true
tasks:
- id: rinfo
type: io.kestra.plugin.redis.cli.RedisCLI
host: "{{ item.value }}"
port: "{{ inputs.port }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: "{{ inputs.network }}"
commands:
- INFO replication
- id: rro
type: io.kestra.plugin.redis.cli.RedisCLI
host: "{{ item.value }}"
port: "{{ inputs.port }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: "{{ inputs.network }}"
commands:
- CONFIG GET replica-read-only
- id: findings
type: io.kestra.plugin.core.output.OutputValues
values:
items: >-
{{ {'p': outputs.primary_info.vars.command_1, 'r':
outputs.replicas_info.outputs | jq('map(.outputs.state | fromjson)') |
first, 'max': inputs.max_lag_bytes, 'n': inputs.replica_hosts | length}
| jq('
(.p | split("\\n") | map(select(contains(":")) | gsub("\\r"; "") | split(":")) | map({(.[0]): (.[1:] | join(":"))}) | add // {}) as $p
| .max as $max | .n as $n
| ($p.master_repl_offset // "0" | tonumber) as $po
| [ (if $p.role != "master" then {code: "PRIMARY_NOT_MASTER", severity: "CRITICAL", host: "primary", detail: "role is \($p.role // "unknown")"} else empty end),
(if (($p.connected_slaves // "0") | tonumber) < $n then {code: "MISSING_REPLICAS", severity: "HIGH", host: "primary", detail: "\($p.connected_slaves // 0) replicas connected, \($n) expected"} else empty end),
(.r[] | . as $r | ($r.info | split("\\n") | map(select(contains(":")) | gsub("\\r"; "") | split(":")) | map({(.[0]): (.[1:] | join(":"))}) | add // {}) as $i
| ($r.ro | split("\\n") | .[1] // "") as $ro
| if ($i.role // "") == "" then {code: "REPLICA_UNREACHABLE", severity: "HIGH", host: $r.host, detail: "no INFO reply"}
elif $i.role != "slave" then {code: "NOT_A_REPLICA", severity: "HIGH", host: $r.host, detail: "role is \($i.role)"}
else
( (if $i.master_link_status != "up" then {code: "LINK_DOWN", severity: "HIGH", host: $r.host, detail: "master_link_status \($i.master_link_status)"} else empty end),
(if ($po - ($i.slave_repl_offset // "0" | tonumber)) > $max then {code: "REPLICA_LAG", severity: "MEDIUM", host: $r.host, detail: "\($po - ($i.slave_repl_offset | tonumber)) bytes behind"} else empty end),
(if $ro == "no" then {code: "REPLICA_WRITABLE", severity: "HIGH", host: $r.host, detail: "replica-read-only is no: writes on this replica are lost at the next resync", fix: "CONFIG SET replica-read-only yes"} else empty end) )
end) ]') | first | toJson }}
- id: summary
type: io.kestra.plugin.core.log.Log
message: >-
Redis replication {{ inputs.primary_host }} with {{ inputs.replica_hosts |
length }} replicas: {{ fromJson(outputs.findings.values.items) | length }}
findings. {{ fromJson(outputs.findings.values.items) |
jq('map("\(.severity) \(.code) \(.host): \(.detail)") | join("; ")') |
first }}
- id: route
type: io.kestra.plugin.core.flow.If
condition: "{{ fromJson(outputs.findings.values.items) | length > 0 }}"
then:
- id: alert
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{{ {'text': 'Redis replication ' ~ inputs.primary_host ~ ': ' ~ (fromJson(outputs.findings.values.items) | jq('map("\(.severity) \(.code) \(.host) (\(.detail))") | join("; ")') | first) ~ '.' ~ (inputs.auto_remediate and not inputs.dry_run ? ' Setting replica-read-only back on.' : ' No change applied (auto_remediate=' ~ inputs.auto_remediate ~ ', dry_run=' ~ inputs.dry_run ~ ').')} | toJson }}
- id: remediate_gate
type: io.kestra.plugin.core.flow.If
condition: "{{ inputs.auto_remediate and not inputs.dry_run and
fromJson(outputs.findings.values.items) | jq('map(select(.fix !=
null)) | length') | first > 0 }}"
then:
- id: remediate
type: io.kestra.plugin.core.flow.Loop
concurrencyLimit: 1
values: "{{ fromJson(outputs.findings.values.items) | jq('map(select(.fix !=
null) | .host)') | first | toJson }}"
outputs:
- id: after
type: STRING
value: "{{ {'host': item.value, 'ro': outputs.recheck.vars.command_1 ?? ''} |
toJson }}"
tasks:
- id: set_ro
type: io.kestra.plugin.redis.cli.RedisCLI
host: "{{ item.value }}"
port: "{{ inputs.port }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: "{{ inputs.network }}"
commands:
- CONFIG SET replica-read-only yes
- id: recheck
type: io.kestra.plugin.redis.cli.RedisCLI
host: "{{ item.value }}"
port: "{{ inputs.port }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: "{{ inputs.network }}"
commands:
- CONFIG GET replica-read-only
- id: verify_result
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.remediate.outputs | jq('map(.outputs.after | fromjson |
select(.ro | split(\"\\\\n\") | .[1] != \"yes\")) | length') |
first > 0 }}"
then:
- id: remediation_failed
type: io.kestra.plugin.core.execution.Fail
errorMessage: "replica-read-only is still not yes on every replica after CONFIG
SET."
else:
- id: remediated
type: io.kestra.plugin.core.log.Log
message: >-
replica-read-only set back to yes and verified on {{
fromJson(outputs.findings.values.items) | jq('map(select(.fix
!= null) | .host) | join(", ")') | first }}. Lag and link
findings are left for review. Persist the setting in
redis.conf or with CONFIG REWRITE.
else:
- id: healthy
type: io.kestra.plugin.core.log.Log
message: "Primary and all {{ inputs.replica_hosts | length }} replicas are
connected, in sync and read-only."
errors:
- id: alert_flow_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{{ {'text': 'Redis replica health guard failed for ' ~ inputs.primary_host ~ ' (execution ' ~ execution.id ~ '). Replication state unknown, check the logs.'} | toJson }}
triggers:
- id: every_5_minutes
type: io.kestra.plugin.core.trigger.Schedule
description: Shipped disabled, and always in safe mode.
cron: "*/5 * * * *"
disabled: true
inputs:
auto_remediate: false
dry_run: true