Skip to content
keypal
Esc
↑↓navigate↵open⌘Jpreview
On this page

Redis

Store keys in Redis with ioredis

The Redis store keeps keys in Redis through an ioredis client, so every server running your app sees the same keys. Revoked keys expire on their own.

Install

npm install keypal ioredis
pnpm add keypal ioredis
yarn add keypal ioredis
bun add keypal ioredis
nub add keypal ioredis
aube add keypal ioredis

keypal works with ioredis 5 and 6.

Setup

Pass storage: 'redis' and your client:

import Redis from 'ioredis'
import { createKeys } from 'keypal'

const redis = new Redis() // or new Redis('redis://user:pass@host:6379')

const keys = createKeys({
  prefix: 'sk_',
  storage: 'redis',
  redis,
})

redis must be an ioredis client. keypal throws at startup if it is missing or if it is a cache-only client without pipelines.

Revoked keys expire

When you revoke or rotate a key, keypal sets an expiry on its record. After revokedKeyTtl seconds (default 604800, 7 days) Redis deletes it.

const keys = createKeys({
  prefix: 'sk_',
  storage: 'redis',
  redis,
  revokedKeyTtl: 60 * 60 * 24 * 30, // 30 days; 0 keeps revoked keys forever
})

Until it expires, verifying a revoked key returns REVOKED. After that the key is gone: verify returns INVALID_KEY and list no longer includes it. Set revokedKeyTtl: 0 if you need revoked keys to stay readable.

revokedKeyTtl applies only with storage: 'redis'. If you create a RedisStore yourself (below), keypal doesn’t set the expiry; call store.setTtl(id, seconds) after revoking if you want it.

Custom key prefix

To change the key prefix, create the store yourself:

import Redis from 'ioredis'
import { createKeys } from 'keypal'
import { RedisStore } from 'keypal/redis'

const store = new RedisStore({ client: new Redis(), prefix: 'myapp:apikey:' })

const keys = createKeys({ prefix: 'sk_', storage: store })

How keys are stored

With the default prefix apikey::

Redis key Type Holds
apikey:{id} string The key record as JSON
apikey:hash:{keyHash} string The key’s id, for verify
apikey:owner:{ownerId} set Ids of the owner’s keys
apikey:tag:{tag} set Ids of keys with the tag
apikey:log:{id} string One audit log entry
apikey:logs:all, apikey:logs:key:{keyId}, apikey:logs:owner:{ownerId}, apikey:logs:action:{action} sorted set Audit log ids by timestamp

Audit log entries don’t expire. Delete old ones with deleteLogs.

Behavior

  • Updates use a compare-and-set script: keypal writes the record only if it hasn’t changed since it was read, and retries up to five times. A background lastUsedAt write can’t undo a concurrent revoke.
  • Redis Cluster is not supported. The store reads and writes several keys per call (the record, its hash entry, owner and tag sets), and Cluster rejects that when the keys sit in different slots. Use a single node, or Sentinel for failover.
  • Caching: with Redis storage, the in-memory cache (cache: true) saves one round trip per verify. A Redis cache (cache: 'redis') costs the same round trip it saves. See Caching.

Was this page helpful?