This is my fourth post about events, you can read the the first one here.
Redis is quickly becoming my favorite technology to use for persistence. It can be used for short-term storage such as caching, or longer-term storage - replacing SQL.
One great feature of Redis is that it has a Publish-Subscribe (pubsub) system built-in. An application can subscribe to events, and publish events. Redis handles the distribution of the event to the subscribers.
Integrating this system into the Domain Events system pushes them out of the realm of the Domain.
In the examples below, I use the ServiceStack.Redis client. I will continue to use the Newtonsoft json serializer out of consistency, but I would recommend using the faster ServiceStack.Text one.
Below is the subscriber- this class will subscribe to the specified channels, listen for events from Redis, and forward that event to the IEventer implementation.
When your application starts, execute the Subscribe method passing in the RedisClientManager, and a list of channels to subscribe to. I am using my applications name, and Environment.MachineName. So I can now target specific machines out of a pool, along with all machines that are running my application.
Disclaimers - Yes, the handling of the Task in the Listen method and Dispose handling is bad, but it communicates general idea. Another thing I need to point out is the code duplication between the reflection code in this and the Raise Event Controller. I am always in conflict over making few posts with perfect examples, or many posts with close-enough examples.
In my next few posts, I'll talk about raising Redis events from our IEventer, and how we can record a history of events in Redis.
No comments:
Post a Comment