Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure eviction (time to live) on Amazon AWS Elasticache Redis + Spring Data

I'm working on a project where we use Spring Data Cache abstraction along with AWS Elasticache Redis and I would like to know how to configure the eviction time of the objects on the cache.

There's not too much official documentation on how to configure Spring Data Cache Abstraction with Elasticache Redis. We found some good info here: http://blog.joshuawhite.com/java/caching-with-spring-data-redis/

But there's nothing about configuring eviction time or time to live of the objects that are cached. Any help?

like image 701
rugobal Avatar asked Feb 11 '14 16:02

rugobal


People also ask

How do I evict a cache in Redis?

Eviction policy The default policy for Azure Cache for Redis is volatile-lru , which means that only keys that have a TTL value set with a command like EXPIRE are eligible for eviction. If no keys have a TTL value, then the system won't evict any keys.

What is eviction in ElastiCache?

Evictions occur when cache memory is overfilled or is greater than the maxmemory setting for the cache, causing the engine to select keys to evict to manage its memory.

Does AWS Redis persist data?

By default, the data in a Redis node on ElastiCache resides only in memory and isn't persistent. If a node is rebooted, or if the underlying physical server experiences a hardware failure, the data in the cache is lost. If you require data durability, you can enable the Redis append-only file feature (AOF).

What happens when ElastiCache Redis runs out of memory?

ElastiCache for Redis implements the maxmemory-policy that's set for the cache node's parameter group when out of memory. The default value (volatile-lru) frees up memory by evicting keys with a set expiration time (TTL value). When a cache node doesn't have any keys with a TTL value, it returns an error instead.


1 Answers

You can configure eviction time by providing expires map in RedisCacheManager. For example you have cacheable method specified like that:

@Cacheable(value = "customerCache", key = "#id")
public Customer findOne(Integer id) {
    return customerRepository.findOne(id);
}

in your applicationContext.xml it will look like this:

<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" c:template-ref="redisTemplate" p:usePrefix="true">
    <property name="expires">
        <map>
            <entry key="customerCache" value="350"/>                    
        </map>
    </property>
</bean>

This will configure "customerCache" values to be evicted 350 seconds after these were first added to the cache.

like image 98
Roman Seleznov Avatar answered Nov 04 '22 16:11

Roman Seleznov