Skip to content

Stateful Connections

Mark Paluch edited this page Jun 13, 2020 · 2 revisions

Lettuce 4.0 introduced StatefulConnection interfaces to separate connection resources from the particular API. Every API operates on a stateful connection that maintains the state (authentication, transactional/multi-status, selected database, read-only/read-write state). This state is restored after the connection reconnects. All async, sync and reactive API’s operate on the underlying connection. If you close for example the connection using the sync API, the same connection is no longer usable over the async or reactive API. Multiple connections (multiple calls to connect() other connect…​() methods) create multiple connections that are isolated and behave independently from each other.

You can obtain a stateful connection from an existing connection by calling getStatefulConnection() on the async, sync or reactive API. Depending on the API (standalone, sentinel, pubsub or cluster) you will get different types (StatefulRedisClusterConnection, StatefulRedisConnection, …​).

A stateful connection provides different APIs: sync, async and reactive. The API objects should not be closed unless you plan to close the connection. Closing the API will close the underlying connection. Retrieving the API instance instead of reusing is the preferred style since the API instances are cached anyways.

Clone this wiki locally