NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›
DOCS / DOCKER RECIPES / KV CACHE (REDIS REPLACEMENT)

KV cache (Redis replacement)

Run RasterizedDB as your application's Redis: any Redis client connects to port 6379 unchanged, and the same keys are reachable from SQL as KV … statements. Use it to drop a separate Redis container from a stack that already uses RasterizedDB, or to give an application a cache with the database's own logins.

What you get

  • A Redis-protocol listener on 6379 (strings, counters, hashes, lists, sets, sorted sets, lifetimes, pub/sub).
  • A memory limit and an eviction policy, as Redis's maxmemory and maxmemory-policy.
  • Keys saved to the data volume every 300 seconds when anything changed, and on a clean shutdown (--kv-persist). Remove that flag for a pure cache that starts empty.
  • The PostgreSQL port on 5432 for SQL and KV statements.
  • A redis-cli service on the same network for trying it out.

Prerequisites

  • Docker with the Compose plugin.

What to edit

  1. Copy .env.example to .env and set RASTERIZEDDB_PG_PASSWORD. Redis clients log in with it.
  2. Set KV_MAX_MEMORY and KV_EVICTION in .env. With noeviction, writes fail once the store is full, as in Redis.
  3. Optional: in compose.yaml, remove --kv-persist and --kv-save-interval-secs for a cache that does not survive a restart.
FlagDefaultIn this recipe
--kv-portclosed6379
--kv-addr127.0.0.10.0.0.0, so other containers can reach it
--kv-authpasswordpassword: AUTH is checked against the database's roles. trust is refused on anything but loopback.
--kv-userthe --pg-user role, rasterizedthe role a bare AUTH <password> is checked as
--kv-max-memory0 (unbounded)256mb
--kv-evictionnoevictionallkeys-lru
--kv-persistoffon
--kv-save-interval-secs300300
--kv-tlsoffoff. on makes every client use TLS (redis-cli --tls, rediss:// URLs).

Run it

sh
docker compose up -d

Check that it works

From the bundled redis-cli:

sh
docker compose run --rm redis-cli PING
docker compose run --rm redis-cli SET session:42 '{"user":7}' EX 3600
docker compose run --rm redis-cli GET session:42
text
PONG
OK
"{\"user\":7}"

The same key from SQL:

sh
psql "host=127.0.0.1 port=5432 user=rasterized dbname=postgres sslmode=require" -c "KV GET session:42"

The memory limit and policy can also be changed on a running server, as in Redis:

sh
docker compose run --rm redis-cli CONFIG SET maxmemory 1gb

CONFIG SET needs a superuser role. The flags in compose.yaml set both values again at every start, so put a lasting change in .env.

Point an application at it

Replace the Redis host with the rasterizeddb service and give the role's password. A typical Redis URL, from another service in the same Compose project:

text
redis://:the-password@rasterizeddb:6379/0

Databases 0 to 15 (SELECT n) work as in Redis. To log in as another role, send AUTH <role> <password> (or put the role before the colon in the URL). Create roles with CREATE ROLE over the PostgreSQL port; see Security.

Stop and clean up

sh
docker compose down        # keeps the volume, and the saved keys with it
docker compose down -v     # deletes the volume

Limits

  • Persistence is snapshots only, like Redis's RDB without the AOF: a crash loses what changed since the last save. A clean stop saves first.
  • The Redis port serves the store of the default database (postgres). Every other database has its own store, reachable over SQL only by connecting to that database.
  • Writes to the store are not part of SQL transactions: a database ROLLBACK does not undo them. Use MULTI/EXEC on the Redis port for atomic groups.
  • What else differs from Redis is in KV cache.

The files

compose.yaml · Docker ComposeDOWNLOAD ↓
# RasterizedDB as a Redis-compatible cache, beside its SQL ports. Any Redis
# client connects to port 6379 unchanged. Copy .env.example to .env first.
name: rasterizeddb-kv

services:
  rasterizeddb:
    image: ${RASTERIZEDDB_IMAGE:-milendenev/rasterizeddb:1.1.0}
    environment:
      RASTERIZEDDB_PG_PASSWORD: ${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD in .env}
    command:
      - --kv-port
      - "6379"
      - --kv-addr
      - 0.0.0.0
      # Redis's maxmemory and maxmemory-policy.
      - --kv-max-memory
      - ${KV_MAX_MEMORY:-256mb}
      - --kv-eviction
      - ${KV_EVICTION:-allkeys-lru}
      # Keep the keys across restarts: a snapshot every 300 s when anything
      # changed, and one on a clean shutdown. Remove for a pure cache.
      - --kv-persist
      - --kv-save-interval-secs
      - "300"
      - --log-level
      - warn
    ports:
      - "127.0.0.1:6379:6379"   # Redis
      - "127.0.0.1:5432:5432"   # PostgreSQL (SQL, and KV statements)
    volumes:
      - data:/var/lib/rasterizeddb
    restart: unless-stopped
    stop_grace_period: 60s
    healthcheck:
      test: ["CMD", "bash", "-c", "exec 3<>/dev/tcp/127.0.0.1/6379"]
      interval: 10s
      timeout: 5s
      retries: 6
      start_period: 30s

  # A redis-cli on the same network, for trying it out:
  #   docker compose run --rm redis-cli PING
  redis-cli:
    image: redis:7.4-alpine
    profiles: ["tools"]
    entrypoint: ["redis-cli", "-h", "rasterizeddb", "-p", "6379"]
    environment:
      REDISCLI_AUTH: ${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD in .env}
    depends_on:
      rasterizeddb:
        condition: service_healthy

volumes:
  data:
.env.example · Environment fileDOWNLOAD ↓
# Copy to .env next to compose.yaml and change the password.

# The password of the role `rasterized`, used on the first start only.
# Redis clients send it with AUTH (no user name needed).
RASTERIZEDDB_PG_PASSWORD=change-me-to-a-long-random-password

# The most the store may hold (0 = unbounded), and what goes when it is full:
# noeviction, allkeys-lru, volatile-lru, allkeys-random, volatile-random, volatile-ttl
KV_MAX_MEMORY=256mb
KV_EVICTION=allkeys-lru

# The image and tag. Pin a full version.
RASTERIZEDDB_IMAGE=milendenev/rasterizeddb:1.1.0
✓ page rendered in 296 µs axum + tera + htmx · no other javascript