Redis client for Kotlin: Lettuce and Kreds — comparison and examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Redis client for Kotlin: overview and comparison of popular libraries



Redis is a high-performance key-value data store widely used for caching, sessions, queues, and real-time applications. For the Kotlin language, there are several mature clients that allow efficient interaction with Redis. In this article, we will look at two main libraries: Lettuce (asynchronous, non-blocking) and Kreds (specialized for Kotlin with coroutine support).



1. Lettuce — powerful and flexible client for JVM



Lettuce is an asynchronous Redis client in Java that works great in Kotlin projects. It supports synchronous, asynchronous (based on CompletableFuture), and reactive (Project Reactor / RxJava) APIs. Lettuce is one of the most popular solutions for Spring Boot and microservices.



Adding dependency (Gradle)

dependencies {    implementation("io.lettuce:lettuce-core:6.3.2.RELEASE")}


Example of synchronous connection and operations

import io.lettuce.core.RedisClientimport io.lettuce.core.api.sync.RedisCommands

fun main() { val client = RedisClient.create("redis://localhost:6379") val connection = client.connect() val commands: RedisCommands<String, String> = connection.sync()

// Setting and getting a value commands.set("mykey", "Hello from Lettuce!") val value = commands.get("mykey") println("Value: $value") // Hello from Lettuce!

// Working with hashes commands.hset("user:1", "name", "Alice") commands.hset("user:1", "age", "30") val name = commands.hget("user:1", "name") println("User name: $name")

connection.close() client.shutdown()}


Asynchronous mode with coroutines

Lettuce supports an asynchronous API via CompletableFuture, which can be easily adapted for Kotlin coroutines using await():

import kotlinx.coroutines.runBlockingimport kotlinx.coroutines.future.await

fun main() = runBlocking { val client = RedisClient.create("redis://localhost:6379") val connection = client.connect() val asyncCommands = connection.async()

val result = asyncCommands.set("coroutine-key", "Kotlin + Lettuce").await() println("Set result: $result")

val value = asyncCommands.get("coroutine-key").await() println("Value: $value")

connection.close() client.shutdown()}


2. Kreds — Kotlin-first client with coroutines



Kreds is a modern Redis client written entirely in Kotlin. It natively supports coroutines, Flow, and serialization via kotlinx.serialization. Kreds uses Netty under the hood but provides a clean Kotlin API.



Adding dependency (Gradle)

dependencies {    implementation("io.ktor:kreds:0.9.0")}


Example of basic usage

import io.kreds.client.RedisClient

suspend fun main() { val client = RedisClient("localhost", 6379)

client.set("kreds-key", "Hello from Kreds!") val value = client.get("kreds-key") println("Value: $value")

// Working with lists client.lpush("mylist", "A", "B", "C") val item = client.rpop("mylist") println("Popped: $item")

client.close()}


Using Flow for subscribing to channels

Kreds supports Pub/Sub via Kotlin Flow, which is convenient for reactive applications:

import io.kreds.client.RedisClientimport kotlinx.coroutines.flow.collect

suspend fun main() { val client = RedisClient("localhost", 6379)

client.subscribe("channel-1").collect { message -> println("Received: ${message.payload}") }}


3. Comparison of Lettuce and Kreds



Recommendations

Characteristic