Publish-Subscribe for Local Cache Invalidation (StackOverflow)

329 views
Skip to first unread message

Carlos Mendes

unread,
Apr 12, 2011, 7:49:06 PM4/12/11
to servic...@googlegroups.com
I've read that SO is using Redis for caching, particularly the pub-sub mechanism to handle local cache removals on different web servers (http://meta.stackoverflow.com/questions/69164/does-stackoverflow-use-caching-and-if-so-how)

The RedisPubSubTests provided with ServiceStack are really simple to understand but I'm not figuring out how to handle subscriptions on a web application.

Any ideas?

Thks in advance 

Demis Bellot

unread,
Apr 12, 2011, 8:00:26 PM4/12/11
to servic...@googlegroups.com, Carlos Mendes
Hi Carlos,

You should probably do a bit of background about this feature. This is the underlying primitive mechanism that the C# client uses:

Then you want to find out what to do. Redis PubSub is a non-durable real-time PubSub mechanism (i.e. if you're not listening you're going to lose messages).
When 1 Redis Client subscribes to a message it blocks the Redis Client at this point.
  Console.WriteLine("Started Listening On '{0}'", ChannelName);
    subscription.SubscribeToChannels(ChannelName); //blocking

So somewhere in another web server you are going to have another RedisClient that publishes a message to 'ChannelName':
 using (var redisPublisher = new RedisClient(TestConfig.SingleHost))
 {
    redisPublisher.PublishMessage(ChannelName, message);
 }

When that happens, any client 'subscribed or listening to that channel' will receive the message and fire off any callbacks:
 subscription.OnMessage = (channel, msg) =>
  {
        Console.WriteLine("Received '{0}' from channel '{1}'", msg, channel);

        //As soon as we've received all 5 messages, disconnect by unsubscribing to all channels
        if (++messagesReceived == PublishMessageCount)
        {
            subscription.UnSubscribeFromAllChannels();
        }
    };

The 'subscribed' client will only stop blocking, i.e. allow the program flow to continue if they unsubscribe from all channels, e.g. subscription.UnSubscribeFromAllChannels();

Anyway I hope this explains it better.

Cheers,
D

Carlos Mendes

unread,
Apr 12, 2011, 8:13:40 PM4/12/11
to servic...@googlegroups.com, Carlos Mendes
Thanks again Demis!

I think I can live without delivery confirmation since I want to keep this simple.

I'll give it a try.
Reply all
Reply to author
Forward
0 new messages