NCache Pub/Sub is a high-performance, in-memory distributed messaging platform that enables decoupled communication for .NET applications with sub-millisecond latency. In high-transaction server environments, choosing the optimal communication channel for your applications is crucial. The Publisher/Subscriber (Pub/Sub) model is a popular choice for facilitating such communication. In this highly decoupled model, multiple clients publish messages as part of a message bus or intermediary communication channel, called a topic. Similarly, multiple clients can subscribe to these messages as part of this topic. This decoupling ensures that publishers do not send messages directly to subscribers but rather through the message bus, which handles all client communications.
Key Takeaways
Decoupled Architecture: NCache acts as a high-performance message bus, allowing publishers and subscribers to communicate independently without identity awareness.
Linear Scalability: The platform eliminates messaging bottlenecks by scaling horizontally across a distributed in-memory cluster at runtime.
Non-Durable Subscriptions: Optimized for real-time data where subscribers receive messages only while actively connected to a topic.
Durable Subscriptions: Provides both Exclusive and Shared (Round Robin) models to ensure message persistence and load balancing during subscriber disconnections.
Pattern-Based Messaging: Enhances flexibility by allowing applications to subscribe to multiple current and future topics simultaneously using wildcards.

Figure 1: Decoupled Publisher/Subscriber Messaging Architecture in NCache
In short, Pub/Sub messaging is a model where neither the publisher nor the subscriber knows each other’s identity. However, as the number of publishers and subscribers increases, the messaging load grows, potentially leading to scalability bottlenecks which defeat the primary purpose of a decoupled messaging interface. Let’s explore a distributed caching solution like NCache that retains the core benefits of Pub/Sub with the added advantage of improved performance and scalability.
Using NCache In-Memory PubSub
NCache provides fast, flexible, and highly scalable distributed caches for your .NET application. Using NCache as your event-driven Pub/Sub platform can be extremely beneficial for scaling your application seamlessly, preventing any system bottlenecks. Here’s how NCache acts as a messaging bus for your application:

Figure 2: Scaling Event-Driven Messaging with a Distributed NCache Cluster
NCache serves as an intermediary interface for your Pub/Sub setup, allowing multiple clients to subscribe to one or more topics simultaneously. Applications can connect to NCache servers and publish messages, which are then delivered to various clients based on their subscriptions. It lets you control whether messages are sent to all subscribers or just one, effectively acting as a messaging bus for your .NET server applications.
Quick PubSub Example with NCache
Imagine you have a global online gaming application where players interact through both audio and real-time text messages. As multiple players send messages to channels and teammates simultaneously, the volume of these messages can become enormous.
To handle this message load and ensure smooth communication, you need a robust messaging interface that enhances your game performance. For this purpose, NCache is the ideal choice for your game’s message bus. Here’s a code snippet demonstrating how server applications can publish and receive messages using NCache. This example shows how to obtain a topic and publish messages efficiently.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Precondition: Cache is already connected // Mention the name of the topic string topicName = "PLAYERMESSAGES"; // Create a Topic in NCache var topic = cache.MessagingService.CreateTopic(topicName); // Create and publish a message to the topic var message1 = new Message(new PlayerMessage { PlayerID = 1, MessageContent = "Ready for the next level!", Timestamp = DateTime.UtcNow }); PrintMessage(message1, "Publisher", null); // Publish the message with delivery option set to All subscribers topic.Publish(message1, DeliveryOption.All); |
Once a message is published against a topic, the subscribers create subscriptions against it to receive messages. The following code demonstrates how subscribing applications can register to a certain topic to receive desired messages.
|
1 2 |
// Register subscribers to this Topic var subscription1 = topic.CreateSubscription(MessageReceived_Subscription1); |
Types of Topic Subscriptions
NCache provides multiple types of topic subscriptions, i.e., Non-Durable, Durable (Shared and Exclusive), and Pattern based subscriptions as discussed below.
| Subscription Type | Message Persistence | Delivery Logic | Best For |
|---|---|---|---|
| Non-Durable | No (Messages lost if disconnected) | Broadcast to all active subscribers. | Real-time telemetry or notifications where “latest” is all that matters. |
| Exclusive Durable | Yes (Stored until processed) | One active subscriber at a time; others are queued. | Sequence-sensitive tasks or “Singleton” workers. |
| Shared Durable | Yes (Stored until processed) | Round Robin across all connected subscribers. | Scaling out heavy processing tasks (load balancing). |

Figure 3: Hierarchy and Classification of NCache Pub/Sub Subscription Models
Non-Durable Subscription
In a non-durable subscription, subscribers only receive messages while connected. If they disconnect, they miss any messages sent during this period. Such subscriptions are exclusive and automatically deleted upon disconnection. This means that if that subscriber rejoins or establishes the connection again, it will be considered a new subscription. For more information, refer to the NCache documentation.
Durable Subscriptions
In a durable subscription, the cache ensures that subscribers do not miss any messages even if they disconnect due to application or machine shutdown, restart, or network issues. Messages are stored on the server until the subscriber reconnects or they expire. Unlike non-durable subscriptions, durable ones are not automatically deleted on disconnection and remain active until unsubscribed.
- Exclusive Durable Subscriptions: An exclusive durable subscription allows only one active subscriber at a time. If the current subscriber unsubscribes gracefully, the exclusive subscription can be assigned to a new subscriber. If a subscriber leaves abruptly, the new subscription request is accepted after waiting for an idle time. The assigned messages always stay there even if there is no subscriber.
|
1 2 3 |
// Create and register subscribers for topic // Message received callback is specified IDurableTopicSubscription subscriber = topic.CreateDurableSubscription("RuntimePlayerComms", SubscriptionPolicy.Exclusive, MessageReceived, TimeSpan.FromMinutes(20)); |
- Shared Durable Subscriptions: Durability can also be achieved through shared subscriptions. In this type of subscription, more than one client can subscribe to a single subscription for load sharing. Messages are distributed using the Round Robin method to send messages to all connected clients. So, even if a client disconnects, the messages continue to be distributed among the remaining subscribers. Therefore, a subscription remains active as long as at least one client is connected.
|
1 2 3 |
// Create and register subscribers for the topic // Message received callback is specified IDurableTopicSubscription subscriber = topic.CreateDurableSubscription("RuntimePlayerComms", SubscriptionPolicy.Shared, MessageReceived, TimeSpan.FromMinutes(20)); |
Pattern Based Subscriptions
To streamline subscribing to multiple topics, NCache provides pattern-based subscriptions. This feature allows you to subscribe to all current and future topics that match a specified pattern. For more information on the supported wildcards in NCache’s pattern-based subscriptions, refer to the NCache documentation.
|
1 2 3 4 5 |
// Only ? * [] wildcards supported string topicName = "team*"; string subscriptionName = "TeamPlayersComms"; // Get the topic ITopic topic= cache.MessagingService.GetTopic(topicName, TopicSearchOptions.ByPattern); |
Conclusion
Why choose NCache? Here’s why:
- In-Memory Speed: As a distributed cache, NCache eliminates database latency by keeping Pub/Sub messages in RAM for .NET applications.
- Scalability: It scales linearly by adding servers at runtime without disruption.
- Flexibility: It dynamically rebalances data, requiring no client intervention.
In essence, NCache offers a powerful, scalable, and fast messaging solution for your application. Its in-memory architecture effectively handles high message loads, making it an excellent choice for optimizing communication in your .NET application. Sign up and explore NCache’s messaging service today!
Frequently Asked Questions (FAQ)
Q: How does NCache handle message delivery when a subscriber disconnects?
A: It depends on the subscription type. In a Non-Durable subscription, any messages published during the disconnection are lost. However, with Durable Subscriptions (both Shared and Exclusive), NCache stores the messages on the server and delivers them once the subscriber reconnects, provided the messages have not expired.
Q: Can NCache Pub/Sub distribute a single message to multiple subscribers?
A: Yes. By using the Non-Durable subscription model or setting the DeliveryOption to All, NCache acts as a broadcast bus where every connected subscriber receives a copy of the published message.
Q: What is the advantage of using Pattern-Based Subscriptions?
A: Pattern-based subscriptions allow a single client to subscribe to multiple topics (current and future) using wildcards like * or ?. This is particularly useful in dynamic environments where topics are created on the fly (e.g., individual player channels in a gaming app).
Q: How does NCache prevent messaging bottlenecks as traffic increases?
A: NCache uses a distributed in-memory architecture that scales linearly. Unlike disk-based brokers that are limited by I/O speed, NCache scales by adding more server nodes to the cluster, which increases both the storage capacity for messages and the processing power for delivery.
Q: What is the difference between Shared and Exclusive Durable subscriptions?
A: An Exclusive subscription ensures that only one active subscriber processes messages at a time, maintaining strict sequence. A Shared subscription allows multiple subscribers to connect to the same subscription, with NCache distributing messages among them using Round Robin for efficient load balancing.






