Pub/Sub Messaging, Continuous Query and Events

Many server applications today need to coordinate work with other applications and share messages and data in an asynchronous manner. And, often this is done in a high transaction environment. For high transaction environments, NCache is an ideal candidate for these asynchronous event driven communications.

NCache provides a rich set of features to cater to the needs of these applications including Pub/Sub Messaging, Continuous Queries, Item Level Events, and Cache Level Events. Each is described below.

Pub/Sub Messaging and Continuous Query for .NET

Figure 1: NCache as a Highly Scalable Messaging Platform

Why NCache?

For .NET / .NET Core applications, NCache is the ideal choice for doing high transaction Pub/Sub Messaging, Continuous Query, and Events. Here is why.

  • Extremely Fast & Scalable: NCache is an extremely fast due to being totally in-memory and gives you sub-millisecond response times. And, it is a distributed cache that lets you add servers to the cache cluster to achieve linear scalability and handle extreme transaction loads.
  • High Availability thru Data Replication: NCache intelligently replicates data in the cache without compromising performance. So, you won't lose any data (events and messages) even if a cache server goes down.
  • 100% Native .NET / .NET Core: For .NET application, NCache offers a completely native stack to integrate with. Unlike other solutions that may have a .NET API but often require Linux deployments or unsupported Windows “ports”, NCache is 100% committed to .NET / .NET Core and fits into your application stack seamlessly.

Pub/Sub Messaging

NCache provides a powerful Pub/Sub Messaging platform for .NET applications. In this Pub/Sub paradigm, the Publisher applications can send messages without knowing who the Subscribers are. The messages can be categorized into Topics and then subscribers can subscribe to specific topics and receive these messages asynchronously.

  • Publisher: an application that publishes the message to be consumed by multiple subscribers. The publisher does not know who the subscribers are and how many.
  • Subscriber: an application that registers itself to receive messages against a specific Topic. The subscriber does not know who the publisher is and how many other subscribers are there.
  • Topic: this is a category of messages and also called a Channel. Publishers publish messages against it and subscribers receive messages from it. You can have multiple Topics in NCache
  • Message: this is the actual message sent by Publishers and consumed by Subscribers.

Pub/Sub Messaging and Continuous Query for .NET

Figure 1: Pub/Sub Messaging with NCache


Below is a source code example on how a Publisher publishes a message against at Topic.

ITopic topic = _cache.MessagingService.CreateTopic(topicName);

// Creating new message containing a “payload” with expiration TimeSpan.
Message message = new Message(payload, timeSpan);

// Publishing the above created message.
topic.Publish(message, DeliveryOption.All, true);
   

Continuous Query (CQ)

Continuous Query is a powerful feature of NCache that allows you to monitor data changes in the distributed cache based on SQL-like query criteria. You create a Continuous Query (CQ) with SQL-like criteria against the cache. And, NCache then monitors this data-set in the cache for any additions, updates, or removals. And, when any of this happens, NCache notifies your application. Below is an example:

string sql = "SELECT Alachisoft.NCache.Sample.Data.Customer 
   WHERE this.Country = ?";

values["Country"] = "USA";
ContinuousQuery cq = new ContinuousQuery(sql, values);

// Register to be notified when a qualified item is added to the cache
cq.RegisterNotification(new QueryDataNotificationCallback(QueryDataModified),
			EventType.ItemAdded, EventDataFilter.None);

// Register to be notified when a qualified item is updated in the cache
cq.RegisterNotification(new QueryDataNotificationCallback(QueryDataModified), 
			EventType.ItemUpdated, EventDataFilter.DataWithMetadata);

// Register to be notified when a qualified item is deleted from the cache
cq.RegisterNotification(new QueryDataNotificationCallback(QueryDataModified), 
			EventType.ItemRemoved, EventDataFilter.None);

// Register continuous query with the cache 
_cache.RegisterCQ(cq);

Once a Continuous Query (CQ) is registered with the cache, NCache monitors all cache updates (add, update, delete) against this CQ criteria. And, if any updates match this CQ criteria, NCache notifies the applications that have registered against this CQ. More than one application or application instances can register against the same CQ.

Item Level Events

Applications can register to be notified when a specific cache-item (identified by a key) is either updated or removed. The applications can be anywhere. As long as they’re connected to the cache, they receive the event notifications. Below are the events and callbacks you can register.

// Register a callback for key based notifications
CacheDataNotificationCallback myMethod = 
new CacheDataNotificationCallback(KeyNotificationMethod);

string key = "Products:10045";
_cache.RegisterCacheNotification(key, myMethod, 
EventType.ItemAdded | EventType.ItemRemoved | EventType.ItemUpdated);

	...

// Sample callback function to receive events
static void KeyNotificationMethod(string key, CacheEventArg cacheEventArgs)
{
		switch (cacheEventArgs.EventType)
		{
				case EventType.ItemAdded:
						break;
				case EventType.ItemRemoved:
						break;
				case EventType.ItemUpdated:
						break;
		}
}

Once a callback function is registered with NCache for a given cached-item, NCache now checks for it whenever any item is added, updated, or removed. And, it then notifies the application for it.

Cache Level Events

Applications can register to be notified whenever any item is added, updated, or removed from the entire cache. Although, this could get quite chatty in a high transaction cache situation, but it is very useful in many other cases.

The applications can be anywhere. As long as they’re connected to the cache, they receive the event notifications. Below are the events and callbacks you can register.

// Register a callback for cache level notifications
CacheDataNotificationCallback myMethod = 
			new CacheDataNotificationCallback(CacheNotificationMethod);

_cache.RegisterCacheNotification(myMethod, 
		EventType.ItemAdded | EventType.ItemRemoved | EventType.ItemUpdated,
		EventDataFilter.None);
	...

With this, you can use NCache as a powerful yet simple messaging platform. Read more about NCache from the links below and download a fully working 60-day trial.