Skip to main content
Version: 2.x

AsyncRedis

AsyncRedis exposes the same commands as Redis, but each command returns as soon as it has been queued. This lets a program send many commands before it waits for any reply.

Return Typesโ€‹

A command on Redis has type IO[RedisError, A]. The same command on AsyncRedis has type UIO[IO[RedisError, A]]. The outer effect queues the command and never fails. The inner effect waits for the reply:

type Redis = GenRedis[GenRedis.Sync]
type AsyncRedis = GenRedis[GenRedis.Async]

Redis.local, Redis.singleNode produce both services in one layer. Redis.cluster produces only Redis.

Pipelining Commandsโ€‹

To send several commands, then wait for all the replies, queue each one and await the inner effects together:

import zio._
import zio.redis._

def writeAll(redis: AsyncRedis): IO[RedisError, Unit] =
for {
first <- redis.set("a", 1L)
second <- redis.set("b", 2L)
third <- redis.set("c", 3L)
_ <- first.zip(second).zip(third)
} yield ()

All three commands are on the wire before the first reply is awaited, which reduces round trips. Use AsyncRedis for bulk writes and Redis everywhere else.

ZIO Chat

๐Ÿ”ฅ Ignite your ZIO journey with ZIO Chat! ๐Ÿ”ฅ Ready to unravel the intricacies of ZIO? As your trusty sidekick, I'm here to light the way. No matter if you're wandering through functional forests or traversing effectful landscapes, I've got answers to keep you on track. Your ZIO adventure starts here โ€“ ask me anything and let's explore together!