Skip to content

redis

Source: builtin/redis

Redis, Valkey and other wire-compatible key-value stores.

Provider configuration

NameTypeRequiredDefaultDescription
urlstringyes—Connection URL: redis://[user:password@]host:6379/<db>, or rediss:// for TLS. The path selects the logical database. Pass the password through a variable or env() rather than writing it into the scenario.

Actions

redis_command

Send one command and return its reply.

Push a job onto a queue.

action "redis_command" "enqueue" {
  args = ["LPUSH", "jobs:welcome_email", jsonencode({ user_id = action.postgres_insert.user.id })]
}

Attributes

NameTypeRequiredDefaultDescription
argsanyyes—The command and its arguments, as a list of strings, numbers and bools. jsonencode structured values first.

Outputs

Type: object({result=any}).

The reply as result:

ReplyType
simple, bulk or verbatim stringstring
integer, double, big numbernumber
booleanbool
nullnull
array, settuple of converted elements
mapobject, keys as strings

An error reply fails the action.

redis_hset

Set fields on a hash (HSET), then its expiry when ttl is set. With no ttl, an existing expiry is left alone, as HSET does.

Turn on feature flags for an organisation.

action "redis_hset" "flags" {
  key    = "flags:${action.postgres_insert.org.id}"
  fields = { new_checkout = true, beta_reports = false }
}

Attributes

NameTypeRequiredDefaultDescription
fieldsanyyes—Object of field = value.
keystringyes—The key of the hash.
on_conflictstringno"update"What to do when the key already exists: "update" sets the fields on it, "ignore" leaves it as it is.
ttlstringno—Expiry for the whole hash, as a duration like "1h".

Outputs

Type: object({created=bool,key=string,new_fields=list(string)}).

FieldTypeDescription
keystringThe key.
createdboolWhether the hash was new.
new_fieldslist(string)Fields the action added, sorted. Empty when nothing was written.

redis_set

Store a value under a key (SET). With no ttl, any existing expiry is cleared, as SET does.

Seed a session for a user, expiring in an hour.

action "redis_set" "session" {
  key   = "session:${var.token}"
  value = { user_id = action.postgres_insert.user.id, roles = ["admin"] }
  ttl   = "1h"
}

Attributes

NameTypeRequiredDefaultDescription
keystringyes—The key.
on_conflictstringno"update"What to do when the key already exists: "update" overwrites it, "ignore" leaves it as it is (SET NX).
ttlstringno—Expiry, as a duration like "1h".
valueanyyes—The value. Objects, maps, lists and tuples are stored as JSON.

Outputs

Type: object({created=bool,key=string}).

FieldTypeDescription
keystringThe key.
createdboolWhether the key was new.

Notes

  • redis_set and redis_hset are typed alternatives to redis_command for the two shapes most seed data takes. Values are encoded the same way for both: strings as-is, numbers as decimal strings (9.99, 42), bools as true / false, and objects, maps, lists and tuples as JSON. Null is an error.
  • ttl uses Go duration syntax ("30s", "1h"), like the action-level timeout.
  • Each typed action reads what exists and writes in one MULTI/EXEC, so created and new_fields are exact even with other clients writing.
  • orchard teardown undoes redis_set and redis_hset automatically, unless the action has its own lifecycle { teardown { } } block or sets lifecycle { managed_teardown = false }. A key the action created is deleted. An overwritten key is restored as it was, value and expiry, when an earlier action in the run created it, or with managed_teardown = true; otherwise an overwritten string is left as written, and an existing hash loses only the fields the action added. Keys larger than 1 MB aren’t restored. redis_command has no automatic teardown.
  • redis_command arguments are sent as text. Interpolated values aren’t escaped or quoted, but they can’t change which command runs: each list element is one argument.