Introduction
In modern distributed applications, in-memory distributed caching plays a vital role in ensuring high performance, scalability, and fault tolerance. One of the most widely used .NET caching solutions is NCache, a 100% native .NET distributed caching platform.
While caching improves application speed and reduces database load, managing the health and performance of the cache in real time is equally important. If your cache nodes go down, memory consumption spikes, or throughput drops, your application may face bottlenecks. That’s where real-time cache monitoring and alerting comes in.
In this article, we’ll explore:
Why real-time cache monitoring is critical.
How NCache provides monitoring and alerting features.
Practical examples of configuring alerts.
A C# example of subscribing to cache events for custom monitoring.
By the end, you will be able to integrate monitoring and alerting into your projects using NCache.
Why Real-Time Monitoring and Alerting?
Imagine you are running an e-commerce application. Your shopping cart and session data are stored in NCache. If a cache node suddenly crashes or memory utilization spikes:
Customers might lose their sessions.
Database traffic will suddenly increase.
End-user experience will be negatively impacted.
Proactive monitoring and alerting helps you:
Detect failures instantly.
Trigger automated recovery actions.
Optimize cache configurations based on load.
Ensure high availability and performance.
Monitoring in NCache
NCache provides several built-in tools and APIs for monitoring cache clusters:
NCache Manager (GUI): Visual dashboard to monitor cluster health, client connections, throughput, memory usage, etc.
PerfMon Counters: Integration with Windows Performance Monitor for detailed metrics.
Command-Line Tools:
NCache PowerShelland CLI utilities for scripting.NCache Alerts & Notifications: Subscribe to cache events (node join/leave, cache start/stop, item eviction, etc.).
Practical Scenario: Real-Time Alerts
Let’s say you want to be notified if:
A cache node leaves the cluster.
A cache goes down.
Cache size exceeds a threshold (memory pressure).
We’ll use NC ache's event notification system to implement this.
Step 1. Enable Notifications in NCache
The very first step in building a real-time monitoring and alerting system with NCache is to enable event notifications at the cache level. By default, NCache allows you to configure whether a cache should broadcast certain events (like when nodes join or leave, cache stops, or items are added/removed). This setting can be controlled through NCache Manager, which is the GUI tool that comes with NCache. To enable it, simply open NCache Manager, right-click on the cache you want to monitor, and select Properties. Within the properties window, navigate to the Advanced Options or Notifications section (depending on your version of NCache). There, you’ll find options to enable event notifications. Once enabled, any client connected to this cache will be able to subscribe to those events programmatically. This step is crucial because if notifications are not enabled at the cache configuration level, your application will never receive real-time alerts, regardless of how much code you write. Think of this as turning on the “push notifications” switch for your distributed cache cluster.
Right-click on the cache → Properties → Enable event notifications.
Step 2. Subscribe to Cache Events in C#
After enabling notifications, the next step is writing C# code that subscribes to those cache events. This is where the Alachisoft.NCache.Client library comes into play. Using the CacheManager.GetCache() method, you establish a connection between your application and the distributed cache. Once connected, you can hook into various event handlers like CacheCleared, CacheStopped, ItemAdded, ItemRemoved, MemberJoined, and MemberLeft. Each of these events corresponds to real-world scenarios: for example, CacheStopped might indicate that the cache cluster is unavailable (critical alert), while MemberLeft could mean a server node unexpectedly went down. Subscribing to these events is as simple as attaching a method to them, much like how you’d subscribe to button click events in a WinForms or WPF application. This step transforms your application into a real-time monitoring agent because it will immediately know when important changes occur in the cache cluster. Developers often log these events to the console while testing, but in production, you would redirect them to log files, dashboards, or alerting systems.
using System;
using Alachisoft.NCache.Client;
using Alachisoft.NCache.Runtime.Caching;
namespace NCacheMonitoringDemo
{
class Program
{
static void Main(string[] args)
{
// Connect to the cache
string cacheName = "demoClusteredCache";
var cache = CacheManager.GetCache(cacheName);
Console.WriteLine("Connected to cache: " + cacheName);
// Subscribe to cache-level events
cache.CacheCleared += OnCacheCleared;
cache.ItemAdded += OnItemAdded;
cache.ItemRemoved += OnItemRemoved;
// Subscribe to cluster-level events
cache.CacheStopped += OnCacheStopped;
cache.MemberJoined += OnMemberJoined;
cache.MemberLeft += OnMemberLeft;
Console.WriteLine("Listening for real-time cache alerts...");
Console.ReadLine(); // Keep running
}
private static void OnCacheCleared(object sender, EventArgs e)
{
Console.WriteLine("[ALERT] Cache cleared at: " + DateTime.Now);
}
private static void OnItemAdded(object sender, CacheItemEventArgs e)
{
Console.WriteLine($"[INFO] Item added: {e.Key}");
}
private static void OnItemRemoved(object sender, CacheItemRemovedEventArgs e)
{
Console.WriteLine($"[INFO] Item removed: {e.Key}, Reason: {e.Reason}");
}
private static void OnCacheStopped(object sender, EventArgs e)
{
Console.WriteLine("[CRITICAL ALERT] Cache has stopped!");
// Here you can send email/SMS/Teams alert
}
private static void OnMemberJoined(object sender, NodeJoinedEventArgs e)
{
Console.WriteLine($"[INFO] Node joined: {e.NodeName}");
}
private static void OnMemberLeft(object sender, NodeLeftEventArgs e)
{
Console.WriteLine($"[ALERT] Node left: {e.NodeName}");
}
}
}
Join the conversation! Your thoughts help the community grow.