Micronaut Redis

Integration between Micronaut and Redis

Version: 1.1.1

1 Introduction

Micronaut features automatic configuration of the Lettuce driver for Redis via the redis-lettuce module.

Release History


  • Initial Release


  • Upgrade Lettuce 5.0.4.RELEASE5.1.3.RELEASE

2 Setting up the Redis Lettuce Driver

Using the CLI

If you are creating your project using the Micronaut CLI, supply the redis-lettuce feature to configure the Lettuce driver in your project:

$ mn create-app my-app --features redis-lettuce

To configure the Lettuce driver you should first add the redis-lettuce module to your classpath:

compile 'io.micronaut.configuration:micronaut-redis-lettuce'

You should then configure the URI of the Redis server you wish to communicate with in application.yml:

Configuring redis.uri
    uri: redis://localhost
The redis.uri setting should be in the format as described in the Connection URIs section of the Lettuce wiki

You can also specify multiple Redis URIs using redis.uris in which case a RedisClusterClient is created instead.

Available Lettuce Beans

Once you have the above configuration in place you can inject one of the following beans:

  • io.lettuce.core.RedisClient - The main client interface

  • io.lettuce.core.api.StatefulRedisConnection - A connection interface that features synchronous, reactive (based on Reactor) and async APIs that operate on String values

  • io.lettuce.core.pubsub.StatefulRedisPubSubConnection - A connection interface for dealing with Redis Pub/Sub

The following example demonstrates the use of the StatefulRedisConnection interface’s synchronous API:

Using StatefulRedisConnection
@Inject StatefulRedisConnection<String, String> connection
RedisCommands<String, String> commands = connection.sync()
commands.set("foo", "bar")
commands.get("foo") == "bar"
The Lettuce driver’s StatefulRedisConnection interface is designed to be long-lived and there is no need to close the connection. It will be closed automatically when the application shuts down.

3 Configuring the Redis Lettuce Driver

Customizing The Redis Configuration

You can customize the Redis configuration using any properties exposed by the DefaultRedisConfiguration class. For example, in application.yml:

Customizing Redis Configuration
    uri: redis://localhost
    ssl: true
    timeout: 30s

Multiple Redis Connections

You can configure multiple Redis connections using the redis.servers setting. For example:

Customizing Redis Configuration
            uri: redis://foo
            uri: redis://bar

In which case the same beans will be created for each entry under redis.servers but exposed as @Named beans.

Using StatefulRedisConnection
@Inject @Named("foo") StatefulRedisConnection<String, String> connection;

The above example will inject the connection named foo.

Redis Health Checks

When the redis-lettuce module is activated a RedisHealthIndicator is activated resulting in the /health endpoint and CurrentHealthStatus interface resolving the health of the Redis connection or connections.

See the section on the Health Endpoint for more information.

4 Redis and Testing

You can run an embedded version of Redis for testing and CI scenarios by adding a dependency on the Embedded Redis project:

testCompile "com.github.kstyrc:embedded-redis:0.6"

If Redis is unavailable for the configured Redis URI an embedded instance of Redis will be automatically be started and then shutdown at the end of the test (when stop is called on the ApplicationContext).

5 Redis for Caching

If you wish to use Redis to cache results then you need to have the Lettuce configuration dependency on your classpath. Lettuce is a non-blocking, reactive Redis client implementation and Micronaut provides an implementation that allows cached results to be read reactively.

Within your application configuration configure the Redis URL and Redis caches:

Cache Configuration Example
    uri: redis://localhost
            # expire one hour after write
            expire-after-write: 1h
Table 1. Configuration Properties for RedisCacheConfiguration
Property Type Description









The charset used to serialize and deserialize values



The cache expiration duration after writing into it.



The cache expiration duration after accessing it

6 Session State with Redis

Storing Session instances is Redis requires special considerations.

You can configure how sessions are stored in Redis using RedisHttpSessionConfiguration.

The following represents an example configuration in application.yml.

Configuring Redis Sessions
                enabled: true
                # The Redis namespace to write sessions to
                namespace: 'myapp:sessions'
                # Write session changes in the background
                write-mode: BACKGROUND
                # Disable programatic activation of keyspace events
                enable-keyspace-events: false
The RedisSessionStore implementation uses keyspace events to cleanup active sessions and fire SessionExpiredEvent and requires they are active.

By default sessions values are serialized using Java serialization and stored in Redis hashes. You can configure serialization to instead use Jackson to serialize to JSON if desired:

Using Jackson Serialization
                enabled: true
                valueSerializer: io.micronaut.jackson.serialize.JacksonObjectSerializer