Configuring Redis as an LRU cache

I’m trying to configure Redis to be used as an LRU cache. According to this article we’ll need to set the following configuration options:

# Where 400mb is (just below?) the service's available memory
maxmemory 400mb

# Exact policy might be different per use case
maxmemory-policy allkeys-lfu

I could fork http://github.com/render-examples/redis and change the configuration for my own needs. But I don’t want to hardcode the max memory variable as I want it to scale with whatever plan it’s on.

I believe Redis allows configuration to be set on runtime like this:

# Where 400 is dynamically generated based on available memory
echo "CONFIG SET maxmemory 400" | redis-cli

I guess this could be part of the Dockerfile. But I’m not sure how.

Any advice on how to set this all up?

Right now I came up with this:

# render.yaml
- type: pserv
  name: startupjobs-redis-cache
  env: docker
  branch: master
  dockerfilePath: ./.render/redis-cache/Dockerfile
  dockerContext: ./.render/redis-cache
  plan: starter plus
  envVars:
    - key: REDIS_MAXMEMORY
      value: 768MB
  disk:
    name: data
    mountPath: /var/lib/redis
    sizeGB: 1
// ./.render/redis-cache/Dockerfile
FROM redis:6-alpine

COPY redis.conf .

ENTRYPOINT echo "maxmemory $REDIS_MAXMEMORY" >> ./redis.conf && redis-server ./redis.conf

This isn’t too bad, because I get to set the maxmemory value in the render.yaml file alongside the plan I use for the redis service. Which should be changed together anyway.

But I’m still curious to hear if there’s a way to remove some of the duplication. Perhaps Render could set some ENV vars based on the plan. RENDER_MEMORY, RENDER_CPUS, etc.

I agree with you. Render can provide environment variables with plan detail: CPU and memory limitation. I create this feature request. Please upvote it. We will notify you when this feature is available. Thank you.

1 Like