Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effect of duplicate Redis subscription to same channel name

Tags:

redis

To subscribe to an instance of StackExchange.Redis.ISubscriber one needs to call the following API:

void Subscribe(RedisChannel channel, Action<RedisChannel, RedisValue> handler, CommandFlags flags = CommandFlags.None);

Question is, what happens if one calls this same line of code with the same channel name as a simple string, say "TestChannel"?

Does ISubscriber check for string equality or it just does not care and therefore we will have two subscriptions?

like image 683
Klaus Nji Avatar asked Dec 16 '16 21:12

Klaus Nji


People also ask

What does Redis Subscribe do?

Redis Pub/Sub implements the messaging system where the senders (in redis terminology called publishers) sends the messages while the receivers (subscribers) receive them. The link by which the messages are transferred is called channel. In Redis, a client can subscribe any number of channels.

Is Redis pub/sub blocking?

Redis' pub/sub sends messages to clients subscribed (listening) on a channel. If you are not listening, you will miss the message (hence the blocking call).

Is Redis pub/sub reliable?

The Redis pub/sub is not a reliable messaging system. Messages that are not retrieved will be discarded when your client is disconnected or a master/standby switchover occurs.

Does Redis pub/sub persist data?

More precisely, Redis Pub/Sub is designed for real-time communication between instances where low latency is of the utmost importance, and as such doesn't feature any form of persistence or acknowledgment.


1 Answers

I am making an assumption that your question is targeted at the Redis API itself. Please let me know if it isn't.

The answer is also based on the assumption that you are using a single redis client connection.

The pubsub map is a hashtable.

To answer your question: If you subscribe multiple times with the same string, you will continue to have only one subscription(you can see that the subscribe happens based on the hashtable here: https://github.com/antirez/redis/blob/3.2.6/src/pubsub.c#L64.

Conversely, calling a single unsubscribe will unsubscribe your other subscriptions for that channel/pattern as well.

If it helps, here is a simple example in Go (I have used the go-redis library) that illustrates the unsubscribe and hashtable storage parts of the answer.

package main

import (
    "fmt"
    "log"
    "time"

    "github.com/go-redis/redis"
)

func main() {
    cl := redis.NewClient((&redis.Options{
        Addr:     "127.0.0.1:6379",
        PoolSize: 1,
    }))

    ps := cl.Subscribe()

    err := ps.Subscribe("testchannel")
    if err != nil {
        log.Fatal(err)
    }

    err = ps.Subscribe("testchannel")
    if err != nil {
        log.Fatal(err)
    }

    err = ps.Unsubscribe("testchannel")
    if err != nil {
        log.Fatal(err)
    }

    go func() {
        msg, err := ps.ReceiveMessage()
        if err != nil {
            log.Fatal(err)
        }
        fmt.Println(msg.Payload)
    }()

    err = cl.Publish("testchannel", "some value").Err()
    if err != nil {
        log.Fatal(err)
    }

    time.Sleep(10 * time.Second)
}
like image 73
Sudarshan Reddy Avatar answered Oct 29 '22 09:10

Sudarshan Reddy