Azure Service Fabric makes the deployment, management, and packaging of scalable and reliable microservice and cloud applications effortless. It aims to provide developers with a robust platform to tackle the common challenges of building distributed cloud-based applications.
NCache, a .NET native In-Memory Distributed Cache, is available to deploy within Azure Service Fabric clusters for enhanced performance and reliability. It improves your application performance and scalability by caching your data closer to the applications.
NCache Deployment Architecture in Azure Service Fabric
The Azure Service Fabric infrastructure includes the Service Fabric cluster resource along with the supporting Azure resources, such as load balancers and a virtual machine scale set. A Service Fabric cluster may be running multiple applications that consist of individual services written in different languages. Moreover, this cluster handles the scalability and availability of these applications and their individual services.
In such enterprise applications, a distributed caching solution such as NCache is integral to facilitate high performance. It can also be deployed within the Service Fabric application as a container service. In this scenario, the NCache servers run within the containers on multiple nodes of the virtual machine scale set and communicate with each other to form a clustered cache.
Thus, the cache discovery service gets real-time information about the NCache Server IP addresses of NCache servers and provides that information to the clients. Using this information, the clients can perform operations on the NCache cache cluster. This deployment architecture is demonstrated in the following diagram.
Step 1: Create Service Fabric Cluster
To deploy NCache, create a Service Fabric cluster along with a resource group that has a key vault resource. The key vault contains the certificate to be used with the Service Fabric cluster. To get started, you may start with the ARM template for a 5-node Service Fabric cluster and then make the following changes:
- In the .jsonfile, change the default value of the vmImageSku parameter in the parameters section from 2016-Datacenter to 2016-Datacenter-with-Containers. This will set up VMSS nodes with Docker capabilities pre-installed.
- Update the ARM template to allow for Open Networking Mode.
- Add port 8251, which is the port for the NCache Management Center, among the load balancer ports you want to expose. Change the distribution mode for this port to SourceIP. This would allow for the NCache Management Center to be accessible with a sticky session to maintain a consistent state between server and client when creating caches, adding nodes to them, etc.
Once this is done, you can deploy the ARM template, and the resulting resources and configurations will be provisioned by the Azure Resource Manager. Once the infrastructure is in place and all the configurations specified in the ARM template have been verified, go to the next step.
Step 2: Create NCache Cluster Service
The NCache Cluster Service is the primary service operating within the containers on the Service Fabric VMSS, representing the running NCache server. The NCache server windows container images are available on DockerHub. This service uses the Open Configuration Mode and its usage details are explained later in the blog. The purpose of this service is to expose the following ports required for the communication:
- The Management Port: Port Number 8250 on which the cache servers listen for the management operations.
- Client Port: Port number 9800 for performing all the CRUD operations.
In Service Fabric’s Open Networking Mode, containers share the same port by using separate IP addresses from the virtual machine’s secondary IP address pool. In case of the NCache Service, this would have the additional benefit of allowing the NCache containers to communicate across host boundaries when part of a clustered cache. This is not possible with the default NAT mode.
Given below is the sample NCache Cluster Service manifest file:
1 2 3 4 5 6 7 |
<ContainerHost> <ImageName>alachisoft/ncache</ImageName> </ContainerHost> <Endpoints> <Endpoint Name="cache-management" Protocol="tcp" UriScheme="tcp" Port="8250" CodePackageRef="Code" /> <Endpoint Name="cache-client" Protocol="tcp" UriScheme="tcp" Port="9800" CodePackageRef="Code" /> </Endpoints> |
Step 3: Create NCache Management Service
NCache Management Service is also a container service and uses the same Docker image as the NCache Cluster Service. The purpose of this service is to expose the port 8251 for management and monitoring purposes by accessing the NCache Management Center.
The reason to separate the NCache Management Service from the NCache Cluster Service is that in Open configuration mode, host-to-port mapping is not allowed. The NCache Management Center needs to be accessed by providing the host port and for this, another service known as NCache Management Service is created with NAT mode. Given below is the sample service manifest file for NCache Management Service.
1 2 3 4 5 6 |
<ContainerHost> <ImageName>alachisoft/ncache</ImageName> </ContainerHost> <Endpoints> <Endpoint Name="web-management" Protocol="http" UriScheme="http" Port="8251" Type="Input" CodePackageRef="Code" /> </Endpoints> |
Step 4: Create NCache Discovery Service
The NCache Discovery Service is a stateless, reliable ASP.NET Core Web API that interacts with the Service Fabric naming service to retrieve endpoints registered by the NCache Cluster Service. It then returns this endpoint information to clients in the form of an HTTP response.
The client applications can then perform operations on the NCache clustered cache by creating cache connections using the acquired IP address information. The service manifest for NCache Discovery Service is shown below:
1 2 3 4 5 6 7 |
<EnvironmentVariables> <EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="" /> </EnvironmentVariables> <Endpoints> <!-- This endpoint is used by the communication listener to obtain the port on which to listen. --> <Endpoint Protocol="http" Name="ServiceEndpoint" UriScheme="http" Port="55100" Type="Input" /> </Endpoints> |
Step 5: Create Cache Cluster
This is where the actual cache creation takes place. To start with, make sure that the NCache Discovery Service is up and running. From the discovery service, access the URI (made of clustered DNS and the port on which the cache discovery service is listening). On opening the URI followed by the DNS, the IP addresses for each endpoint are shown which are registered by the NCache Service.
Once you have ensured that the service is up and running, you can now access the NCache Management Center to create a clustered cache. To access the NCache Management Center, access it using the DNS followed by port number 8251. This brings you to the main window of NCache Management Center.
Create the cache cluster by following the steps in the create cache cluster section in the documentation.
Step 6: Create Client Application Service
The different NCache client services send HTTP requests to the NCache Discovery Service which provides the clients with the IP addresses of the containers on which NCache is running in response. The clients can then use the IP addresses to create cache connections with which they communicate with the NCache clustered cache. If you need to monitor client-side NCache performance counters or use out-proc client caches, deploy the client applications as container services. Ensure that the container images include NCache installed in cache-client mode.
Given below is a sample service manifest file for a client application service with NCache client installation:
1 2 3 4 5 6 |
<EntryPoint><ContainerHost><ImageName>ncache/apps:sfclient</ImageName></ContainerHost></EntryPoint> <EnvironmentVariables> <EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="Development"/> <EnvironmentVariable Name="Cache_Name" Value="democache"/> <EnvironmentVariable Name="Cache_DiscoveryService_URL" Value="<Cache Discovery Service URL>"/> <!-- Other environmental variables --> |
Step 7: Run Client Applications
Once the cache is up and running, you can start performing CRUD operations. The client applications will initially start with acquiring the IP addresses of the NCache server containers from the NCache Discovery Service and establish a connection with the cache. Once the connection is made, the cache management and CRUD operations can proceed.
You can validate the client connection using the NCache Management Center and NCache Monitor explained in the next step.
Step 8: Monitoring NCache Cluster
NCache Management Center has a NCache Monitor tool to help you track cluster performance. Monitoring your cache cluster gives you real-time information about the cluster health, cache activity, number of operations performed, and more. Monitoring your cache cluster helps you take suitable measures for issues such as memory overheads.
Follow the steps in the documentation to monitor your cache cluster. It shows you various dashboards such as Report Dashboard or Graphical Dashboard along with your client and server counters.
Step 9: Scaling NCache Cluster
NCache being scalable in nature, allows you to add servers to your cluster at runtime using the NCache Management Center. Simply scale the NCache Cluster Service to any number of instances you require. Once the new containers are running, you can add them to your cache cluster without stopping the cluster. You can then monitor your cache cluster to see activity on the added server node.
Conclusion
Deploying NCache in Azure Service Fabric enhances application performance and scalability with robust, distributed caching. This guide covers the essential steps for setup, configuration, and monitoring, ensuring optimal operation of your cache cluster. For additional details or support, consult the NCache documentation or contact our team at Alachisoft Support.