Redis cache

Keep firewall verdicts across restarts and share them between instances. Recommended before metadata filtering.

The firewall caches the verdicts it gets from Socket. By default that cache lives in the container's memory, so it is lost on every restart and each instance has its own. Redis lets the cache survive restarts and allows every firewall instance to share one cache. Redis is optional.

Set up Redis before Metadata filtering. Filtering works without Redis, but with Redis its per-version verdicts survive restarts and are shared between instances. Redis never holds package files.

Before you start: a running firewall (Stand up Socket Firewall for the first time).

1. Add Redis to the compose file

Add a redis service next to the firewall in docker-compose.yml. Redis is only reachable on the compose network, not from the host.

services:
  socket-firewall:
    # ...existing settings...
    depends_on:
      - redis

  redis:
    image: redis:7-alpine
    container_name: socket-firewall-redis
    command: ["redis-server", "--requirepass", "${REDIS_PASSWORD}", "--appendonly", "yes", "--maxmemory", "2000mb", "--maxmemory-policy", "volatile-lru"]
    volumes:
      - redis-data:/data
    restart: unless-stopped

volumes:
  redis-data:

--appendonly yes and the volume keep the cache on disk, so restarting Redis doesn't empty it.

2. Point the firewall at Redis

Append to .env. Compose also reads REDIS_PASSWORD from here for the redis service.

REDIS_ENABLED=true
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=change_me_to_a_long_random_string

Generate the password with openssl rand -hex 32.

📘

Use environment variables, not socket.yml, for the password

A redis: block in socket.yml works too, but values in that file are not shell-expanded, so password: ${REDIS_PASSWORD} is stored as that literal string.

3. Recreate the stack

docker compose up -d --force-recreate
docker compose ps        # both containers Up; firewall (healthy)

Verify

Generate a verdict, then confirm it landed in Redis and survives a firewall restart.

# Helper: redis-cli with the password from .env
rc() { docker compose exec -T redis redis-cli --no-auth-warning -a "$(grep '^REDIS_PASSWORD=' .env | cut -d= -f2-)" "$@"; }

rc PING          # PONG
rc DBSIZE        # note the number

curl -sk -o /dev/null https://sfw.example.com/npm/lodahs/-/lodahs-0.0.1-security.tgz
rc DBSIZE        # higher than before

docker compose restart socket-firewall
sleep 30
rc DBSIZE        # unchanged: the cache survived the restart
curl -sk -o /dev/null https://sfw.example.com/npm/lodahs/-/lodahs-0.0.1-security.tgz
docker compose logs socket-firewall | grep SOCKET_DECISION | tail -1
  • PING returns PONG
  • DBSIZE goes up after a package request
  • DBSIZE is unchanged after docker compose restart socket-firewall
  • The decision logged after the restart shows "cached":true

Running more than one instance

Point every firewall instance at the same Redis (REDIS_HOST = a shared Redis or a managed service such as ElastiCache or Azure Cache for Redis). Instances then share verdicts, so a package checked by one is served from cache by all.

For a managed Redis with TLS, add to .env:

REDIS_SSL=true
REDIS_SSL_VERIFY=true
# Only if needed:
REDIS_SSL_CA_CERT=/etc/redis-tls/ca.pem          # private CA
REDIS_SSL_CLIENT_CERT=/etc/redis-tls/client.pem  # mTLS
REDIS_SSL_CLIENT_KEY=/etc/redis-tls/client-key.pem
REDIS_SSL_SERVER_NAME=redis.example.com          # SNI, if it differs from REDIS_HOST

Mount the certificate files into the container (for example ./redis-tls:/etc/redis-tls:ro under volumes:). The same settings exist under redis: in socket.yml (ssl_ca_cert, ssl_client_cert, ssl_client_key, ssl_server_name).

Redis memory and settings

Limit Redis memory to 2 GB with --maxmemory 2000mb.

Set the eviction policy to --maxmemory-policy volatile-lru. Every cached entry has an expiry and can be fetched from Socket again.

Persistence is optional. Set --appendonly yes to keep the cache across a Redis restart. Missing keys cause the firewall to ask Socket again.

The firewall connects to one primary Redis endpoint. Use REDIS_READ_HOST to add a read replica. The firewall does not support endpoints that speak the Redis Cluster protocol, such as ElastiCache with cluster mode enabled, or MemoryDB. Use REDIS_PASSWORD to set a password. Redis ACL usernames are not supported.

When Redis is unreachable the firewall uses its in-memory cache, /health stays OK, and the log shows Failed to connect to Redis.

Clearing the cache

🚧

Restarting the firewall no longer clears verdicts

Other pages suggest restarting the firewall to pick up a policy change or waiver immediately. With Redis, verdicts survive the restart. To force fresh verdicts, flush Redis instead.

rc FLUSHDB      # rc helper from Verify above

FLUSHDB empties the whole Redis database the firewall uses (REDIS_DB). This includes keys stored by other applications in the same database. Give the firewall its own Redis database. After a flush, the firewall asks Socket again the next time each package is requested.

Troubleshooting

SymptomFix
DBSIZE stays at 0REDIS_ENABLED isn't true, the firewall wasn't recreated after editing .env, or the firewall can't reach Redis (next row)
Firewall is healthy but Redis is unusedThe firewall falls back to its in-memory cache and stays (healthy). Check docker compose logs socket-firewall | grep -i "redis" for Failed to connect to Redis
NOAUTH Authentication required from redis-cliPass the password with -a
Failed to connect to Redis: connection refused [role=primary localhost:6379]REDIS_HOST must be the compose service name (redis), not localhost
Cache empty after a Redis restartPersistence is off. Check the --appendonly yes flag and the redis-data volume

Next

Metadata filtering.

Reference


Did this page help you?