This page shows how to use ServiceStack's C# Redis Client to take advantage of the Publish/Subscribe messaging paradigm built into Redis to be able to develop high-performance network notification servers.
Publish/Subscribe messaging pattern in Redis
Redis is largely recognized as the most efficient NoSQL solution to manage rich data structures, providing the data source to power stateless back-end app servers. What a lot of people don't know is that it also has a number of characteristics that also make it a prime candidate to develop high-performance networking solutions with. Probably its most noteworthy feature in this area to date, is its built-in Publishing / Subscribe / Messaging support which enables a new range of elegant comet-based and high performance networking solutions to be developed.
Under the covers this is achieved with the PUBLISH/SUBSCRIBE redis server operations. Essentially these operations allows any number of clients to be able to Listen on any arbitrary named channel. As soon as an external client Publishes a message to that channel, each listening client is notified. The clients will continue to receive messages as long as they maintain at least one active subscription.
Service Stack's C# Client exposes this functionality in a similar way as the redis-rb Ruby client. Essentially you create a Subscription, Then Register your handlers on each of the events you're interested in, then it's just a matter of Subscribing to the channels you're interested in. When you want your consumers to stop receiving messages you need to unsubscribe from all channels.
Pub/Sub Examples
Below are some examples showing how to use the API to accomplish some basic tasks. At the end of each example is the Console output showing the sequence of events helping you visualize the order of each operation. The full runnable source code for these examples are available here.
No comments:
Post a Comment