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
maxmemoryandmaxmemory-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
KVstatements. - A
redis-cliservice on the same network for trying it out.
Prerequisites
- Docker with the Compose plugin.
What to edit
- Copy
.env.exampleto.envand setRASTERIZEDDB_PG_PASSWORD. Redis clients log in with it. - Set
KV_MAX_MEMORYandKV_EVICTIONin.env. Withnoeviction, writes fail once the store is full, as in Redis. - Optional: in
compose.yaml, remove--kv-persistand--kv-save-interval-secsfor a cache that does not survive a restart.
| Flag | Default | In this recipe |
|---|---|---|
--kv-port | closed | 6379 |
--kv-addr | 127.0.0.1 | 0.0.0.0, so other containers can reach it |
--kv-auth | password | password: AUTH is checked against the database's roles. trust is refused on anything but loopback. |
--kv-user | the --pg-user role, rasterized | the role a bare AUTH <password> is checked as |
--kv-max-memory | 0 (unbounded) | 256mb |
--kv-eviction | noeviction | allkeys-lru |
--kv-persist | off | on |
--kv-save-interval-secs | 300 | 300 |
--kv-tls | off | off. on makes every client use TLS (redis-cli --tls, rediss:// URLs). |
Run it
docker compose up -dCheck that it works
From the bundled redis-cli:
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:42PONG
OK
"{\"user\":7}"The same key from SQL:
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:
docker compose run --rm redis-cli CONFIG SET maxmemory 1gbCONFIG 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:
redis://:the-password@rasterizeddb:6379/0Databases 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
docker compose down # keeps the volume, and the saved keys with it
docker compose down -v # deletes the volumeLimits
- 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
ROLLBACKdoes not undo them. UseMULTI/EXECon the Redis port for atomic groups. - What else differs from Redis is in KV cache.
The files
# 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:# 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