# Memory Limits & Eviction Policies — Redis Fundamentals: Caching, Data Structures and Persistence

Source: https://www.geekswithgeeks.com/en/redis/redis-eviction

> Cap Redis memory with maxmemory and choose an eviction policy such as allkeys-lru so a full cache degrades gracefully.

## maxmemory and eviction

Redis lives in RAM, so set a limit with `maxmemory`. When the limit is reached, the `maxmemory-policy` decides what happens: `noeviction` rejects writes, `allkeys-lru` removes the least recently used keys, and `volatile-ttl` removes keys with the soonest expiry.

Set the limit and policy in `redis.conf` or at runtime.

```bash
redis-cli CONFIG SET maxmemory 256mb
redis-cli CONFIG SET maxmemory-policy allkeys-lru
redis-cli INFO memory
```

For a pure cache use `allkeys-lru`. If Redis also holds data you cannot rebuild, such as queues or sessions, avoid evicting it and size memory with headroom.

Quick check

**Quiz:** Which policy evicts the least recently used keys across the whole keyspace?

- [ ] noeviction
- [x] allkeys-lru
- [ ] volatile-ttl
- [ ] allkeys-random

*Answer:* allkeys-lru. allkeys-lru removes the least recently used keys when maxmemory is reached.
