id: redis-persistence-guard
namespace: company.team
description: |
Check that a Redis instance used as a data store really persists what it holds: read INFO
persistence and the save / appendonly configuration, flag an instance with no persistence at
all, a failed BGSAVE or AOF write, or a snapshot older than allowed while keys keep changing.
Alert Slack, and optionally turn on the required persistence with CONFIG SET, trigger a
BGSAVE, wait for it to finish, and read INFO again to verify.
inputs:
- id: host
type: STRING
displayName: Redis host
defaults: "localhost"
- id: port
type: INT
displayName: Redis port
defaults: 6379
- id: required
type: SELECT
displayName: Required persistence
values:
- RDB
- AOF
- RDB_AND_AOF
defaults: RDB_AND_AOF
- id: rdb_save_policy
type: STRING
displayName: RDB save policy to apply
description: Value set with CONFIG SET save when RDB is required but disabled.
Redis' own default.
defaults: "3600 1 300 100 60 10000"
- id: max_snapshot_age_minutes
type: INT
displayName: Maximum snapshot age (minutes)
description: With RDB enabled, a last successful save older than this while keys
have changed is flagged.
defaults: 120
- id: auto_remediate
type: BOOL
displayName: Auto remediate
defaults: false
- id: dry_run
type: BOOL
displayName: Dry run
description: Must be false, together with auto_remediate, for CONFIG SET and
BGSAVE to run.
defaults: true
- id: network
type: STRING
displayName: Docker network for redis-cli
description: Network of the redis-cli container. Leave empty to use Docker's
default bridge.
defaults: ""
tasks:
- id: info
type: io.kestra.plugin.redis.cli.RedisCLI
description: Read-only.
host: "{{ inputs.host }}"
port: "{{ inputs.port }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: "{{ inputs.network }}"
commands:
- INFO persistence
- id: save_config
type: io.kestra.plugin.redis.cli.RedisCLI
description: Read-only.
host: "{{ inputs.host }}"
port: "{{ inputs.port }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: "{{ inputs.network }}"
commands:
- CONFIG GET save
- id: state
type: io.kestra.plugin.core.output.OutputValues
description: INFO persistence parsed into a map, plus the save policy.
values:
info: "{{ outputs.info.vars.command_1 | replace({'\\\\n': '
', '\\n': '
'}) | split('
') | jq('map(select(contains(\":\")) | split(\":\") | {(.[0]): (.[1:] |
join(\":\") | gsub(\"\\r\"; \"\"))}) | add') | first | toJson }}"
save: "{{ outputs.save_config.vars.command_1 | replace({'\\\\n': '
', '\\n': '
'}) | split('
') | jq('.[1] // \"\"') | first }}"
- id: findings
type: io.kestra.plugin.core.output.OutputValues
values:
items: >-
{{ {'i': fromJson(outputs.state.values.info), 'save':
outputs.state.values.save, 'req': inputs.required, 'now': (now() |
timestamp), 'maxAge': inputs.max_snapshot_age_minutes * 60} | jq('
(.i.aof_enabled == "1") as $aof | (.save | length > 0) as $rdb
| [ (if ($aof | not) and ($rdb | not) then {code: "NO_PERSISTENCE", detail: "save is empty and appendonly is off: every key is lost on restart"} else empty end),
(if (.req | test("AOF")) and ($aof | not) then {code: "AOF_REQUIRED_OFF", detail: "appendonly is off"} else empty end),
(if (.req | test("RDB")) and ($rdb | not) then {code: "RDB_REQUIRED_OFF", detail: "save is empty"} else empty end),
(if .i.rdb_last_bgsave_status != "ok" then {code: "BGSAVE_FAILED", detail: "rdb_last_bgsave_status=\(.i.rdb_last_bgsave_status)"} else empty end),
(if $aof and .i.aof_last_write_status != "ok" then {code: "AOF_WRITE_FAILED", detail: "aof_last_write_status=\(.i.aof_last_write_status)"} else empty end),
(if $rdb and (.i.rdb_changes_since_last_save | tonumber) > 0 and (.now - (.i.rdb_last_save_time | tonumber)) > .maxAge
then {code: "STALE_SNAPSHOT", detail: "last save \((.now - (.i.rdb_last_save_time | tonumber)) / 60 | floor | tostring | sub("\\.0$"; "")) min ago, \(.i.rdb_changes_since_last_save) changes since"} else empty end) ]') | first | toJson }}
- id: summary
type: io.kestra.plugin.core.log.Log
message: >-
Redis {{ inputs.host }}:{{ inputs.port }} persistence: appendonly {{
fromJson(outputs.state.values.info).aof_enabled == '1' ? 'on' : 'off' }},
save "{{ outputs.state.values.save }}", {{
fromJson(outputs.state.values.info).rdb_changes_since_last_save }} changes
since the last save. Findings: {{ fromJson(outputs.findings.values.items)
| jq('if length == 0 then "none" else map("\(.code) (\(.detail))") |
join(", ") end') | 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 ' ~ inputs.host ~ ':' ~ inputs.port ~ ' is not persisting as required (' ~ inputs.required ~ '): ' ~ (fromJson(outputs.findings.values.items) | jq('map("\(.code): \(.detail)") | join("; ")') | first) ~ '.' ~ (inputs.auto_remediate and not inputs.dry_run ? ' Enabling persistence now.' : ' 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 }}"
then:
- id: enable_aof
type: io.kestra.plugin.redis.cli.RedisCLI
runIf: "{{ inputs.required contains 'AOF' and
fromJson(outputs.state.values.info).aof_enabled != '1' }}"
host: "{{ inputs.host }}"
port: "{{ inputs.port }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: "{{ inputs.network }}"
commands:
- CONFIG SET appendonly yes
- id: enable_rdb
type: io.kestra.plugin.redis.cli.RedisCLI
runIf: "{{ inputs.required contains 'RDB' and outputs.state.values.save | length
== 0 }}"
host: "{{ inputs.host }}"
port: "{{ inputs.port }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: "{{ inputs.network }}"
commands:
- CONFIG SET save "{{ inputs.rdb_save_policy }}"
- id: bgsave
type: io.kestra.plugin.redis.cli.RedisCLI
description: Take a snapshot now, so the data already in memory is on disk
without waiting for the save policy.
host: "{{ inputs.host }}"
port: "{{ inputs.port }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: "{{ inputs.network }}"
commands:
- BGSAVE
- id: wait_for_save
type: io.kestra.plugin.core.flow.LoopUntil
description: Poll INFO until the background save has finished and nothing is
left unsaved.
condition: >-
{{ outputs.poll.vars.command_1 is defined and
outputs.poll.vars.command_1 contains 'rdb_bgsave_in_progress:0'
and outputs.poll.vars.command_1 contains 'rdb_changes_since_last_save:0' and outputs.poll.vars.command_1 contains 'rdb_last_bgsave_status:ok' }}
checkFrequency:
interval: PT2S
maxIterations: 30
tasks:
- id: poll
type: io.kestra.plugin.redis.cli.RedisCLI
host: "{{ inputs.host }}"
port: "{{ inputs.port }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: "{{ inputs.network }}"
commands:
- INFO persistence
- id: reinfo
type: io.kestra.plugin.redis.cli.RedisCLI
host: "{{ inputs.host }}"
port: "{{ inputs.port }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: "{{ inputs.network }}"
commands:
- INFO persistence
- id: resave_config
type: io.kestra.plugin.redis.cli.RedisCLI
host: "{{ inputs.host }}"
port: "{{ inputs.port }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: "{{ inputs.network }}"
commands:
- CONFIG GET save
- id: after
type: io.kestra.plugin.core.output.OutputValues
values:
info: "{{ outputs.reinfo.vars.command_1 | replace({'\\\\n': '
', '\\n': '
'}) | split('
') | jq('map(select(contains(\":\")) | split(\":\") | {(.[0]):
(.[1:] | join(\":\") | gsub(\"\\r\"; \"\"))}) | add') | first |
toJson }}"
save: "{{ outputs.resave_config.vars.command_1 | replace({'\\\\n': '
', '\\n': '
'}) | split('
') | jq('.[1] // \"\"') | first }}"
- id: verify_result
type: io.kestra.plugin.core.flow.If
condition: >-
{{ (inputs.required contains 'AOF' and
fromJson(outputs.after.values.info).aof_enabled != '1')
or (inputs.required contains 'RDB' and outputs.after.values.save | length == 0)
or fromJson(outputs.after.values.info).rdb_last_bgsave_status != 'ok'
or fromJson(outputs.after.values.info).rdb_changes_since_last_save != '0' }}
then:
- id: remediation_failed
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Persistence still not as required after remediation: {{
outputs.after.values.info }}"
else:
- id: remediated
type: io.kestra.plugin.core.log.Log
message: >-
Persistence verified: appendonly {{
fromJson(outputs.after.values.info).aof_enabled == '1' ? 'on'
: 'off' }}, save "{{ outputs.after.values.save }}", last
BGSAVE ok with 0 changes since. CONFIG SET is lost on restart
unless redis.conf has the same settings: run CONFIG REWRITE or
update your config.
else:
- id: persisting
type: io.kestra.plugin.core.log.Log
message: "Redis {{ inputs.host }}:{{ inputs.port }} persists as required ({{
inputs.required }})."
errors:
- id: alert_flow_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{{ {'text': 'Redis persistence guard failed on ' ~ inputs.host ~ ':' ~ inputs.port ~ ' (execution ' ~ execution.id ~ '). Persistence state unknown, check the logs.'} | toJson }}
triggers:
- id: every_15_minutes
type: io.kestra.plugin.core.trigger.Schedule
description: Shipped disabled, and always in safe mode.
cron: "*/15 * * * *"
disabled: true
inputs:
auto_remediate: false
dry_run: true