Skip to content

Set Redis max memory policy#400

Open
annehaley wants to merge 1 commit into
masterfrom
redis-max-mem
Open

Set Redis max memory policy#400
annehaley wants to merge 1 commit into
masterfrom
redis-max-mem

Conversation

@annehaley
Copy link
Copy Markdown
Collaborator

Resolves #398

This PR changes the command for the Redis container in development to the following:

["redis-server", "--maxmemory", "4096MB", "--maxmemory-policy", "allkeys-lru"]

For production, we can't set the max memory size of the Heroku Redis add-on without upgrading the plan (we currently use the mini plan), but we can set the max memory policy to allkeys-lru.

Comment thread docker-compose.yml

redis:
image: redis:alpine
command: ["redis-server", "--maxmemory", "4096MB", "--maxmemory-policy", "allkeys-lru"]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we'd set this in CI too, but GH Actions doesn't let you pass a custom command to a service.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's fine. It wouldn't make a difference for our tests anyway.

Copy link
Copy Markdown
Collaborator

@brianhelba brianhelba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have concerns about how this will affect the use of Django Channels, which uses Redis to mediate websocket connections.

I think a better policy may be volatile-lru, but that might require cooperation from large_image. I'm happy to help investigate more.

@manthey
Copy link
Copy Markdown

manthey commented Jun 5, 2026

I have concerns about how this will affect the use of Django Channels, which uses Redis to mediate websocket connections.

I think a better policy may be volatile-lru, but that might require cooperation from large_image. I'm happy to help investigate more.

volatile-lru requires that entries have a TTL. large_image uses redis (or other tile caches) strictly as LRU -- there is no reason to evict tiles except for the memory area being full and the then LRU us a sensible policy. If websocket connections are more recently used than the speed at which tiles are cached, this won't functionally be a problem, but fundamentally, these two sets of data have very different concerns. The correct architecture is really have two separate caches, one that is LRU for tiles and one that is ALWAYS as large as total possible websocket connections .

@brianhelba
Copy link
Copy Markdown
Collaborator

#363 set our cache implementation to use large_image...RedisCache, which doesn't support setting TTL on keys.

However, we could:

  • Switch back to using django_large_image...DjangoCache
    • Just set LARGE_IMAGE_CACHE_BACKEND = "django" or unset it entirely as it's the django_large_image default
  • DjangoCache will rely on Django's general cache framework
  • Configure Django's cache framework to use Redis

@brianhelba
Copy link
Copy Markdown
Collaborator

I think volatile-lru gives us the balance we want. We can set the TTL value on the tile cache to be arbitrarily long, so tiles will practically only get evicted by memory pressure, not time. In this case, the TTL is still essential as a marker that the tile data doesn't need to be preserved for the application to function correctly.

@manthey
Copy link
Copy Markdown

manthey commented Jun 5, 2026

I think volatile-lru gives us the balance we want. We can set the TTL value on the tile cache to be arbitrarily long, so tiles will practically only get evicted by memory pressure, not time. In this case, the TTL is still essential as a marker that the tile data doesn't need to be preserved for the application to function correctly.

I can't tell from how the redis docs are written if the eviction is anything that is past its TTL or anything that has a TTL at all. If the former, then the shortest possible TTL would permit tile to be evicted (but redis would normally keep them since it takes effort to evict things). If the latter, the a long TTL would prevent needless eviction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set Redis Max Memory Policy

4 participants