Skip to content

Getting Started

What is Kreds?

Kreds is an asynchronous, non-blocking redis client library written in Kotlin using co-routines.

It makes use if Java NIO, Netty, Kotlin Coroutines. It provides an idiomatic approach to use redis in your server applications.

The following features of redis are implemented in Kreds.

  • Commands operating on Strings, Hash, Lists, Keys, Sets, Sorted Sets. ✔️
  • Blocking commands. ✔️
  • Pipelining. ✔️
  • Publish/Subscribe. ✔️
  • Connection handling commands. ✔️

Basic usage

Here's a basic usage of the library.

Basic Usage
fun main(){
    runBlocking {
        val job  = launch(Dispatchers.Default) {
            newClient(Endpoint.from("")).use { client ->
                client.set("foo", "100")
                // prints 101
                println("incremented value of foo ${client.incr("foo")}") 
                client.expire("foo", 3u) // set expiration to 3 seconds
                assert(client.get("foo") == null)
            } // <--- the client/connection to redis is closed.
        job.join() // wait for the co-routine to complete
        shutdown() // shutdown the Kreds Event loop.


Kreds supports pipelining of Redis commands

Pipelining enables you to execute redis commands in a batch, instead of making a network call for each command.

Pipelining usage

Pipeline Execution
fun main(){
    runBlocking {
        newClient(Endpoint.from("")).use { client  ->
            val pipe = client.pipelined() // Create a pipeline object on this client connection.

            // incrementedValueFn can be invoked post pipeline execution to get command result.
            val incrementedValueFn = pipe.incr("bar")  
            val setCountFn = pipe.scard("myset")

            pipe.execute() // execute the pipeline of commands.

            assert(incrementedValueFn() == 501L)
            assert(setCountFn() == 9L)
        shutdown() // shutdown the Kreds event loop on application shutdown.


Use Kreds to publish message and subscribe to channels. Kreds does not use single thread per subscription, so you are not limited by the number of threads you have.

This enabled Kreds applications to subscribe to many channels and scale up easily.

Subscriber Usage

Redis Subscriber
fun main(){
    runBlocking {
        // This call returns after all the coroutines finish.
        coroutineScope {
            val kredSubscriptionHandler = object : AbstractKredsSubscriber() {

                override fun onMessage(channel: String, message: String) {
                    println("Received message: $message from channel $channel")

                override fun onSubscribe(channel: String, subscribedChannels: Long) {
                    println("Subscribed to channel: $channel")

                override fun onUnsubscribe(channel: String, subscribedChannels: Long) {
                    println("Unsubscribed from channel: $channel")

                override fun onException(ex: Throwable) {
                    println("Exception while handling subscription to redis: ${ex.stackTrace}")


            newSubscriberClient(Endpoint.from(""),kredSubscriptionHandler).use { client ->
        shutdown() // Shutdown the Kreds event loop on application shutdown.

Publisher Usage

Redis Publisher
suspend fun publish(){
    newClient(Endpoint.from("")).use { publisher ->
        for(i in 1 until 10){