Pub/Sub Subscription Types and Overview
NCache provides multiple types of Pub/Sub subscriptions between publishers and subscribers.
In simple terms, the client receives an ITopic instance on get call by giving topic name. Using this instance, the client can make calls for publish and subscribe.
The returned instance allows the following calls using the instance:
Through the subscribe call, the client gets all the messages published on the topic it has subscribed for.
Note: This feature is only available for NCache Enterprise edition.
Through pattern-based subscription, clients can provide style patterns to subscribe to multiple topics matching the pattern. For this, it is important that the topics matching the pattern already exist on server Once the subscription is successfully created on pattern-based topic, the clients receive messages published on topics that match the pattern.
In case new topics are created matching the pattern of a subscriber, those topics are added to the existing subscription list. Also, if a topic is created after pattern-based-subscription, which matches the pattern provided, it should register that client subscription against it. As with other subscriptions, all messages published on the corresponding are received by the corresponding client. Similarly, in case of un-subscription, it unsubscribes against all the matching topics with provided pattern without affecting any other subscription by this call. Note that a pattern can be used only to receive failure notifications and not publish messages.
A client can only get pattern-based topic, however, a client is not allowed to create it.
Supported Wildcards for Pattern-Based Subscriptions
Pattern-based matching support the following three wildcards:
Examples of Wildcards Usage
* : zero or many characters
bl* subscribes to black, blue and blur etc
? : any one character
`h?t` subscribes to hit, hot and hat
 : specify a range
`b[ae]t` subscribes to bet and bat, but not bit
Delivery Option Behavior for Pattern-based Subscriptions
There are two delivery options for messages in pattern-based subscriptions:
- Any: Under this delivery option, the message is assigned to any one of the subscribers as per pattern matching or by name subscription.
- All: Under this delivery option, the message is assigned to all subscribers for each pattern-matching subscriptions as well as all subscriptions of the topic.
Durable and Non-Durable Subscriptions
NCache offers durable and non-durable subscriptions for clients. In non-durable subscriptions, the messages intended for the client are conveyed only until the client remains connected. Under any circumstances that the client leaves the network, it will not be able to receive the messages on rejoining as the messages are lost during the disconnection phase of the client. By default, all subscriptions created on a topic are non durable subscriptions.
On the other hand, durable subscriptions ensure that the client does not miss out on any messages and, upon rejoining in case of disconnection, the client will receive corresponding messages. The structure of durable and non-durable subscriptions is as follows:
Note: This feature is only available for NCache Enterprise edition.
Durable subscriptions have a unique subscription name at the topic level, and a policy name that determines whether subscription is shared or exclusive. Durable subscriptions are not impacted due to client disconnection and re-connection. If a client is disconnected, the messages intended for this client are stored on a server until the client rejoins or messages are expired. Durable subscriptions are not deleted automatically on client disconnection unless the client has properly unsubscribed to them.
On the client side, durable subscriptions are stored just like other regular subscriptions. Poll call gets messages of active durable subscriptions similar to normal subscriptions. Also, durable subscriptions are transferred to and replicated on all nodes as done so with regular subscriptions. In case a new node is added, it is transferred all the durable subscriptions along with the messages and cache data.
Durable subscription offers the following policies:
Exclusive Durable Subscription
Exclusive Durable Subscription means that one subscription has only one active subscriber at a time. Until the connection is active, no new subscriber request on the same subscription is accepted.
In Exclusive Durable Subscriptions, once the client is connected, it will receive messages until the connection remains established. If, for any reason, the connection breaks and after some time interval the connection is re-established, the client will receive all the assigned messages that it may have missed out on during its disconnection time period. This ensures that no assigned message is lost.
Shared Durable Subscription
Shared Durable Subscription means that the subscription can have more than one subscribers registered at a time. The basic purpose here is load sharing.
In case of Shared Durable Subscriptions, multiple subscribers can subscribe on one subscription. For sending messages to multiple subscribers messages, the Round Robin method is adopted. Even if any subscriber leaves the network, the messages will continue to be distributed between active subscribers.
The subscription will remain and cannot be unsubscribed until all subscribers are disconnected. This means as long as there is even a single active subscriber, this subscription will stay active. In case shared subscription disconnects and then reconnects, the messages will be assigned to the corresponding subscribers. To elaborate, the term 'disconnect' in this scenario means undergoing a state where there are no active subscribers.
Delivery Options for Durable Subscriptions
There are two delivery options for messages in durable subscriptions:
- Any: Under this delivery option, any single subscription can receive the message regardless of whether it is shared or exclusive. This is determined by a Round Robin fashion.
- All: Under this delivery option, an exclusive subscription receives the message, however, on shared subscription, all of the registered subscribers receive the message.
Expiration of Durable Subscriptions
You can give expiration as a parameter for durable subscriptions, however, it is optional. If any subscription is created with expiration time defined, as long as it has active clients, each time the client polls or performs any other activity, the expiration time is reset. The expiration time starts at the instance of client inactivity. If the client remains inactive for the entire expiration time period defined, the subscription will be expired. Once the subscription expires, it will be removed from all nodes.
Non-durable subscriptions only cater to the "Exclusive" policy. This means that at any instance, only one client will be available for non-durable subscription. The behavior and implementation for Exclusive Non-durable Subscription is the same as that for Exclusive Durable Subscription.
NCache provides sample application for Pub/Sub at:
- Shipped with NCache: %NCHOME%\samples\dotnet\PubSub