Skip to main content

Redis Data Source

Redis data sources provide key-value store operations accessible via the core.store runtime module. Use cases include caching, counters, rate limiting, session storage, and shared state across cluster nodes.

Registration

mutation {
core {
insert_data_sources(data: {
name: "redis"
type: "redis"
prefix: "redis"
path: "redis://:${secret:REDIS_PASSWORD}@redis-host:6379/0"
}) { name }
}
}

Path Format

Standard Redis URL: redis://[user:password@]host:port/db

Supports ${secret:ENV_VAR} syntax for credentials via ApplyEnvVars.

Examples

# Local Redis, no auth
redis://localhost:6379/0

# With password
redis://:mypassword@redis-host:6379/0

# With env var
redis://:${secret:REDIS_PASSWORD}@redis-host:6379/0

# Non-default database
redis://redis-host:6379/3

Usage

Redis sources are accessed through the core.store module:

Read Operations

# Get a value (returns null if key doesn't exist)
{ function { core { store {
get(store: "redis", key: "session:abc123")
} } } }

# List keys matching a pattern
{ function { core { store {
keys(store: "redis", pattern: "user:*")
} } } }

Write Operations

# Set a value with optional TTL (seconds)
mutation { function { core { store {
set(store: "redis", key: "session:abc", value: "{\"user\":\"alice\"}", ttl: 3600) {
success message
}
} } } }

# Delete a key
mutation { function { core { store {
del(store: "redis", key: "session:abc") { success }
} } } }

# Atomic increment (creates key with value 1 if not exists)
mutation { function { core { store {
incr(store: "redis", key: "page:views")
} } } }

# Set TTL on an existing key
mutation { function { core { store {
expire(store: "redis", key: "session:abc", ttl: 300) { success }
} } } }

Pub/Sub Operations

# Publish a message to a channel
mutation { function { core { store {
publish(store: "redis", channel: "notifications", message: "hello world") {
success message
}
} } } }

# Configure keyspace notification events (default: KEA = all events)
mutation { function { core { store {
configure_keyspace_events(store: "redis", events: "KEA") {
success message
}
} } } }

Subscriptions

# Subscribe to messages on a Pub/Sub channel
subscription { core { store {
subscribe(store: "redis", channel: "notifications") {
channel
message
}
} } }

# Watch for keyspace events matching a pattern
subscription { core { store {
watch(store: "redis", pattern: "__keyevent@0__:*") {
key
event
}
} } }

Operations Reference

OperationTypeReturnsDescription
getqueryString (nullable)Get value by key
keysquery[String]List keys matching glob pattern
setmutationOperationResultSet key-value with optional TTL
delmutationOperationResultDelete a key
incrmutationBigIntAtomic increment, returns new value
expiremutationOperationResultSet TTL on existing key
publishmutationOperationResultPublish message to a Pub/Sub channel
configure_keyspace_eventsmutationOperationResultConfigure Redis keyspace notifications
subscribesubscriptionstore_messageSubscribe to a Pub/Sub channel
watchsubscriptionstore_key_eventWatch keyspace events by pattern

Use with Rate Limiting

Redis is used as a shared counter backend for LLM rate limiting:

# LLM source with Redis-backed rate limits
https://api.openai.com/v1/chat/completions?model=gpt-4o&api_key=...&rpm=100&tpm=100000&rate_store=redis

Pub/Sub

Redis sources support Pub/Sub messaging and keyspace event notifications via GraphQL subscriptions and mutations.

Operations

OperationTypeDescription
subscribesubscriptionSubscribe to messages on a channel
watchsubscriptionWatch keyspace events matching a pattern
publishmutationPublish a message to a channel
configure_keyspace_eventsmutationEnable Redis keyspace notifications (sets notify-keyspace-events config)

Keyspace Notifications

To use watch, Redis keyspace notifications must be enabled. Use the configure_keyspace_events mutation:

mutation { function { core { store {
configure_keyspace_events(store: "redis", events: "KEA") {
success message
}
} } } }

This sets the Redis notify-keyspace-events configuration. Common values: KEA (all events), Kg (generic key commands), Ks (string commands).

See Pub/Sub Subscriptions in the Key-Value Store documentation for subscription query examples.

See Also