Azure Service Fabric makes the deployment, management, and packaging of scalable and reliable microservice applications very easy and efficient. It simplifies the development and management of cloud applications as well as reliably scale and orchestrate services. Azure Service Fabric’s purpose is to provide developers with a very rich platform that addresses many of the problems commonly associated with developing distributed cloud-based applications.
NCache provides its deployment within Azure Service Fabric cluster for enhanced performance and reliability. NCache is an in-memory distributed caching solution that improves your applications’ performance and scalability by caching your data and residing closer to the applications.
NCache Deployment Architecture in Azure Service Fabric
Let’s take an overview of the overall deployment architecture of NCache in Azure Service Fabric. The infrastructure consists of the Service Fabric cluster resource together with all its supporting Azure resources including an Azure load balancer and a virtual machine scale set. A Service Fabric cluster may have multiple running applications that could be made of individual services as it is common in microservices-based architecture and these individual services can be written in a different language. Service Fabric handles the scalability and high availability of the applications and their individual services.
In such enterprise applications, a distributed caching solution such as NCache is an integral part to facilitate high performance. NCache can also be deployed within the Service Fabric application as a container service. In this scenario, NCache servers run within the containers on multiple nodes of the virtual machine skill set and communicate with each other to form a clustered cache.
The cache discovery service gets real-time information about the 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. Putting this all together, the following diagram shows the deployment of NCache in Service Fabric cluster:
Let us go through the stepwise details of the deployment of NCache to understand it better.
Step 1: Create Service Fabric Cluster
The first step is to create a service fabric cluster in order to deploy NCache. Create a resource group with key vault resource. The key vault contains the certificate that will be used with the Service Fabric cluster. To get started, you may start with the ARM template for a 5-node Service Fabric cluster given here and then make the following changes:
- In the json file, 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.
- Among the load balancer ports that you would like to expose, add one with a value of 8251 which is the port value on which the NCache Web Manager will be running. Change the distribution mode for this port to SourceIP. This would allow for the NCache Web Manager to be accessed with a sticky session to provide a consistent state between server and client when in the process of creating caches and adding nodes to them.
Once this is done, you can deploy the ARM template and the resources and configurations thereof 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
NCache cluster service is the main service running that represents the NCache server running within the containers on the service Fabric VMSS. The NCache server windows container images are available on DockerHub. The configuration mode this service uses is 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 such as cache creation etc.
- Client Port: Port number 9800 for performing all the CRUD operations.
In Service Fabric, the Open networking mode allows multiple containers to share the same port by allocating individual IP addresses to them from the secondary IP address pool of the virtual machine they are running on. 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:
<Endpoint Name="cache-management" Protocol="tcp" UriScheme="tcp" Port="8250" CodePackageRef="Code" />
<Endpoint Name="cache-client" Protocol="tcp" UriScheme="tcp" Port="9800" CodePackageRef="Code" />
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 Web Manager. 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. NCache Web Manager 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.
<Endpoint Name="web-management" Protocol="http" UriScheme="http" Port="8251" Type="Input" CodePackageRef="Code" />
Step 4: Create NCache Discovery Service
The NCache discovery service is a stateless reliable ASP.NET Core Web API that consults with the Service Fabric naming service and acquires the endpoints that were registered by the NCache Cluster Service. The information retrieved by accessing the endpoints is then provided to the clients as an HTTP response.
The client applications can then perform operations on the NCache clustered cache by creating cache connection using the acquired IP address information. The service manifest for NCache Discovery Service is shown below:
<EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="" />
<!-- 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" />
Step 5: Create Cache Cluster
This part 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 which is 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 made sure that the service is up and running, you can now access the NCache Web Manager to create NCache clustered cache. To access the NCache Web Manager, access it using the DNS followed by port number 8251. This brings you to the main window of NCache Web Manager. 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 require monitoring for the client-side NCache performance counters and/or require out-proc client caches with your client services, the client applications need to be deployed as container services whose source images include the NCache installation in cache-client mode. In this case, the client service also uses the Open networking mode.
Given below is the sample service manifest file for a client application service with NCache client installation:
<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 from the client-side. 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 Web Manager and NCache Web Monitor explained in the next step.
Step 8: Monitoring NCache Cluster
NCache Web Manager has an embedded NCache Web Monitor tool that helps you monitor your cache cluster. Monitoring your cache cluster gives you real-time information about the cache cluster health, cache activity, number of operations performed, and much more. Monitoring your cache cluster helps you take suitable measures for issues such as memory overheads etc.
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 very scalable in nature, allows you to add servers to your cache cluster at runtime. NCache Web Manager lets you add servers in the cache cluster. Simply scale the NCache cluster service to however many instances you require. Once the new NCache server containers are running, you can add them to your cache cluster without having to stop the cache cluster.
You can then monitor your cache cluster to see activity on the added server node.
To put the whole article together, we have covered the deployment of Azure Service Fabric in NCache. NCache is a very scalable caching solution and ensures high performance and extremely fast transactions. These benefits can be achieved by following a few easy steps and deploying NCache in your containerized environment with Azure Service Fabric.