Try Playground
Show / Hide Table of Contents

Cache Connectivity

After successfully configuring NCache, you can integrate it into your existing applications by incorporating NCache API calls as mentioned on this page. We have discussed few of the sceanrios where these APIs have used below:

  • Default Connectivity (Local Network): If you are using NCache on-premises over your local network, you can use private Server IPs. However, this is not always possible, thus, NCache offers multiple APIs to overcome potential issues as discussed below. For instance, there can be situations where you need to initialize both Clustered and Client Caches within a single application. To do so, you can use the Get-Cache method to initialize both the caches. Likewise, if you want to prevent unauthorized access to your cache and has enabled security, you can ensure the smooth connectivity using Security Credentials.

  • Connectivity with LoadBalancer: However, if your application is deployed with a LoadBalancer that connects to all the servers to manage the transactional load you can use NCache IsLoadBalancer API to ensure cache conectivity.

  • Connectivity using Public IPs on Cloud: If you're using this option you can set your cache server IP through the client.ncconf file. This process is detailed below. And if you want to connect to NCache Cloud servers using their private IP addresses and need to communicate over a public network, you can assign a public IPv4 address to a cache server within the NCache cluster by executing the Set-CacheServerPublicIP cmdlet.

  • Connectivity using Port Forwarding: When deploying NCache in containerized environments, one effective approach is to assign static IP addresses to NCache containers within a custom network. This setup allows client applications to reliably connect to the cache servers using consistent IP addresses, facilitating stable communication across different network environments.

Note

This feature is also available in NCache Professional.

Prerequisites for Cache Connectivity

  • .NET
  • Java
  • Python
  • Node.js
  • To learn about the standard prerequisites required to work with all NCache client-side features including cache connectivity, please refer to the given page on Client-Side API Prerequisites.
  • For API details, refer to: ICache, CacheManager, GetCache, CacheConnectionOptions, RetryInterval, ConnectionRetries, EnableKeepAlive, KeepAliveInterval, ServerList, ServerInfo, LoadBalance, IsolationLevel, Mode, Credentials, UserCredentials.
  • To learn about the standard prerequisites required to work with all NCache client-side features cache connectivity, please refer to the given page on Client-Side API Prerequisites.
  • For API details, refer to: Cache, CacheManager, getCache, CacheConnectionOptions, TimeSpan, setRetryInterval, setEnableKeepAlive, setConnectionRetries, setEnableClientLogs, setKeepAliveInterval, setLoadBalance, setIsolationLevel, Credentials, setUserCredentials.
  • To learn about the standard prerequisites required to work with all NCache client-side features cache connectivity, please refer to the given page on Client-Side API Prerequisites.
  • For API details, refer to: Cache, CacheManager, get_cache, CacheConnectionOptions, set_retry_interval, set_connection_retries, set_keep_alive_interval, set_enable_client_logs, set_load_balance, set_isolation_level, Credentials, set_user_credentials.
  • To learn about the standard prerequisites required to work with all NCache client-side features cache connectivity, please refer to the given page on Client-Side API Prerequisites.
  • For API details, refer to: Cache, CacheManager, getCache, CacheConnectionOptions, setRetryInterval, TimeSpan, setEnableClientLogs, setKeepAliveInterval, setLoadBalance,setIsolationLevel, Credentials, setUserCredentials.

Connect to Single Cache

NCache provides ICache interface to get an instance of NCache’s cache. Moreover, the CacheManager class lets you connect to the cache instance via the GetCache method.

The following example connects to a cache named demoCache which is in running state.

  • .NET
  • Java
  • Python
  • Node.js
// Specify the cache name
string cacheName = "demoCache";

// Connect to cache
ICache cache = CacheManager.GetCache(cacheName);
// Specify the cache name
String cacheName = "demoCache";

// Connect to cache
cache = CacheManager.getCache(cacheName);
# Specify the cache name
cache_name = "demoCache"

# Connect to cache
cache = ncache.CacheManager.get_cache(cache_name)
// Specify the cache name
let cacheName = "demoCache";

// Connect to cache
let cache = await ncache.CacheManager.getCache(cacheName);
Note

To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.

Connect to Multiple Caches

Using the GetCache method, you can connect to multiple caches within a single application. This will help you manage the data of multiple caches using one application.

The following example connects to two caches, demoCache and demoClusteredCache from the same client.

  • .NET
  • Java
  • Python
  • Node.js
// Specify cache names
string cacheName1 = "demoCache";
string cacheName2 = "demoClusteredCache";

// Connect to the caches
ICache cache1 = CacheManager.GetCache(cacheName1);
ICache cache2 = CacheManager.GetCache(cacheName2);
// Specify cache names
String cacheName1 = "demoCache";
String cacheName2 = "demoClusteredCache";

// Connect to the caches
Cache cache1 = CacheManager.getCache(cacheName1);
Cache cache2 = CacheManager.getCache(cacheName2);
# Specify cache names
cache_name_1 = "demoCache"
cache_name_2 = "demoClusteredCache"

# Connect to the caches
cache_1 = ncache.CacheManager.get_cache(cache_name_1)
cache_2 = ncache.CacheManager.get_cache(cache_name_2)
// Specify cache names
let cacheName1 = "demoCache";
let cacheName2 = "demoClusteredCache";

// Connect to the caches
let cache1 = await ncache.CacheManager.getCache(cacheName1);
let cache2 = await ncache.CacheManager.getCache(cacheName2);

Connect to Cache Using CacheConnectionOptions

The CacheConnectionOptions allows specifying values of cache properties while establishing a connection to the cache. These values are the same which can be configured through client.ncconf file.

Note

Any configuration specified through the CacheConnectionOptions will override the value in client.ncconf for that particular client.

In this example, the values of the RetryInterval, ConnectionRetries, EnableKeepAlive, and KeepAliveInterval properties can be changed. For this application, these values will be used instead of the ones specified in the client configuration file.

  • .NET
  • Java
  • Python
  • Node.js
// Create new CacheConnectionOptions instance
var options = new CacheConnectionOptions();

// Specify the cache connection options to be set
options.RetryInterval = TimeSpan.FromSeconds(5);
options.ConnectionRetries = 2;
options.EnableKeepAlive = true;
options.KeepAliveInterval = TimeSpan.FromSeconds(30);

// Specify the cache name
string cacheName = "demoCache";

// Connect to cache with CacheConnectionOptions
ICache cache = CacheManager.GetCache(cacheName, options);
// Create new CacheConnectionOptions instance
CacheConnectionOptions options = new CacheConnectionOptions();

// Specify the cache connection options to be set
TimeSpan retryInterval = new TimeSpan(0, 0, 5);
options.setRetryInterval(retryInterval);
options.setConnectionRetries(2);
options.setEnableClientLogs(true);

TimeSpan keepAliveInterval = new TimeSpan(0, 0, 30);
options.setKeepAliveInterval(keepAliveInterval);

// Specify the cache name
String cacheName = "democache";

// Connect to cache with CacheConnectionOptions
Cache cache = CacheManager.getCache(cacheName, options);
# Create new CacheConnectionOptions instance
options = ncache.CacheConnectionOptions()

# Specify the cache connection options to be set
retry_interval = ncache.TimeSpan(0, 0, 5)
keep_alive_interval = ncache.TimeSpan(0, 0, 30)

options.set_retry_interval(retry_interval)
options.set_connection_retries(2)
options.set_enable_client_logs(True)
options.set_keep_alive_interval(keep_alive_interval)

# Specify the cache name
cache_name = "demoCache"

# Connect to cache with CacheConnectionOptions
cache = ncache.CacheManager.get_cache(cache_name, options)
// Create new CacheConnectionOptions instance
let options = new ncache.CacheConnectionOptions();

// Specify the cache connection options to be set
let retryInterval = new ncache.TimeSpan(0, 0, 5);

options.setRetryInterval(retryInterval);
options.setConnectionRetries(2);
options.setEnableClientLogs(true);

let keepAliveInterval = new ncache.TimeSpan(0, 0, 30);
options.setKeepAliveInterval(keepAliveInterval);

// Specify the cache name
let cacheName = "demoCache";

// Connect to cache with CacheConnectionOptions
let cache = await ncache.CacheManager.getCache(cacheName, options);

Connect to Cache Using CacheConnectionOptions for Load Balancer

Often, application deployments employ load balancers to prevent requests from overwhelming servers, i.e., prevent clients from accessing the servers directly. In such circumstances, to ensure that your cluster connects to all the servers, NCache offers the IsLoadBalancer API, as demonstrated below:

  • .NET
CacheConnectionOptions cacheConnectionOptions = new CacheConnectionOptions();
cacheConnectionOptions.ServerList = new List<ServerInfo>()
{
    new ServerInfo(true, "20.200.20.40", 9800)
};
ICache cache = CacheManager.GetCache("demoCache", cacheConnectionOptions);
Note

Learn more about how NCache deals with these load balancers here.

Connect to Clustered and Client Cache

Note

Using the GetCache method to connect to Clustered and Client Cache in a single call is not a recommended approach.

One can connect to Clustered and Client Cache in a single call using the GetCache method. This will initialize both the caches within a single application, hence helping you manage the data of multiple caches through one application.

Note

If the local cache is connected to the Client Cache, serialization must be the same as that of the Clustered Cache. Both caches must be configured previously and must be in a running state.

The following example connects to two caches using the GetCache method.

  • .NET
  • Java
  • Python
  • Node.js
// Specify the cache names
string clusteredCache = "demoCache";
string clientCache = "myClientCache";

CacheConnectionOptions cacheConnectionOptions = new CacheConnectionOptions();
cacheConnectionOptions.LoadBalance = true;
cacheConnectionOptions.ConnectionRetries = 5;

CacheConnectionOptions clientCacheConnectionOptions = new CacheConnectionOptions();
clientCacheConnectionOptions.LoadBalance = true;
clientCacheConnectionOptions.ConnectionRetries = 5;
clientCacheConnectionOptions.Mode = IsolationLevel.OutProc;

// Connect to the caches in a single call
// CacheConnectionOptions which can be null if not required
ICache cache = CacheManager.GetCache(clusteredCache, cacheConnectionOptions, clientCache, clientCacheConnectionOptions);
// Specify cache names
String clusteredCache = "demoCache";
String clientCache = "myClientCache";

// Create and set CacheConnectionOptions for clustered cache
CacheConnectionOptions cacheConnectionOptions = new CacheConnectionOptions();
cacheConnectionOptions.setLoadBalance(true);
cacheConnectionOptions.setConnectionRetries(5);

// Create and set CacheConnectionOptions for Client Cache
CacheConnectionOptions clientCacheConnectionOptions = new CacheConnectionOptions();
clientCacheConnectionOptions.setLoadBalance(true);
clientCacheConnectionOptions.setConnectionRetries(5);
clientCacheConnectionOptions.setIsolationLevel(IsolationLevel.OutProc);

// Connect to caches in single call
// CacheConnectionOptions which can be null if not required
Cache cache = CacheManager.getCache(clusteredCache, cacheConnectionOptions, clientCache, clientCacheConnectionOptions);
# Specify cache names
clustered_cache = "demoCache"
client_cache = "myClientCache"

# Create and set CacheConnectionOptions for clustered cache
cache_connection_options = ncache.CacheConnectionOptions()
cache_connection_options.set_load_balance(True)
cache_connection_options.set_connection_retries(5)

# Create and set CacheConnectionOptions for Client Cache
client_cache_connection_options = ncache.CacheConnectionOptions()
client_cache_connection_options.set_load_balance(True)
client_cache_connection_options.set_connection_retries(5)
client_cache_connection_options.set_isolation_level(ncache.IsolationLevel.OUT_PROC)

# Connect to both of the caches in a single call
# CacheConnectionOptions can be null if not required
cache = ncache.CacheManager.get_cache(clustered_cache, cache_connection_options, client_cache, client_cache_connection_options)
// Specify cache names
let clusteredCache = "demoCache";
let clientCache = "myClientCache";

// Create and set CacheConnectionOptions for clustered cache
let cacheConnectionOptions = new ncache.CacheConnectionOptions();
cacheConnectionOptions.setLoadBalance(true);
cacheConnectionOptions.setConnectionRetries(5);

// Create and set CacheConnectionOptions for Client Cache
let clientCacheConnectionOptions = new ncache.CacheConnectionOptions();
clientCacheConnectionOptions.setLoadBalance(true);
clientCacheConnectionOptions.setConnectionRetries(5);
clientCacheConnectionOptions.setIsolationLevel(ncache.IsolationLevel.OutProc);

// Connect to caches in single call
// CacheConnectionOptions which can be null if not required
let cache = await ncache.CacheManager.getCache(clusteredCache, cacheConnectionOptions, clientCache, clientCacheConnectionOptions);

Connect to Cache with Security Credentials

If security has been enabled, you need to provide the security credentials while connecting to the caches so that only the authorized user can perform operations. For more details on using security in NCache, see NCache Security.

  • .NET
  • Java
  • Python
  • Node.js
// Specify cache name and user credentials
string cacheName =  "demoCache";
string userId = "userid";
string password = "mypassword";

// Initialize the CacheConnectionOptions
var options = new CacheConnectionOptions();

// Enter the credentials
options.UserCredentials = new Credentials(userId, password);

// Connect to the cache using the security credentials
ICache cache = CacheManager.GetCache(cacheName, options);
// Initialize credentials
String userId = "UserId";
String password = "UserPassword";
String cacheName = "demoCache";

// Use CacheConnectionOptions to provide credentials
CacheConnectionOptions options = new CacheConnectionOptions();

// Use UserCredentials property to assign credentials
Credentials credentials = new Credentials(userId, password);
options.setUserCredentials(credentials);

// Connecting to cache with security credentials
Cache cache = CacheManager.getCache(cacheName, options);
# Initialize credentials
user_id = "UserId"
password = "UserPassword"
cache_name = "demoCache"

# Use CacheConnectionOptions to provide credentials
options = ncache.CacheConnectionOptions()

# Use UserCredentials property to assign credentials
credentials = ncache.Credentials(user_id, password)
options.set_user_credentials(credentials)

# Connecting to cache with security credentials
cache = ncache.CacheManager.get_cache(cache_name, options)
// Initialize credentials
let userId = "UserId";
let password = "UserPassword";
let cacheName = "demoCache";

// Use CacheConnectionOptions to provide credentials
let options = new ncache.CacheConnectionOptions();

// Use UserCredentials property to assign credentials
let credentials = new ncache.Credentials(userId, password);
options.setUserCredentials(credentials);

// Connecting to cache with security credentials
let cache = await ncache.CacheManager.getCache(cacheName, options);
Note

If you’re unable to connect to cache servers when using NCache Cloud using their private IP addresses and need to use a public network instead, you’ll need to configure a public IP. To do so, run the Set-CacheServerPublicIP cmdlet to the cache server in question to assign a public IPv4 address to the cache server in the NCache cluster.

Connect to Cache Using Public IP's

If you have subscribed to NCache Cloud and chosen the Public IP connectivity option. You will need to modify the client.ncconf file differently and make sure your cache server's public ips are mentioned, regardless of the fact that the cache servers also have private ips that they use within the cache cluster.

Please refer to client.ncconf to learn more.

Troubleshooting

Port is Not Accessible

Sometimes clients may be unable to connect to a cache due to your firewall configurations.

Workaround

Use the details in our getting started guide to change your firewall settings.

Unable to Find client.ncconf

Sometimes a client may be unable to find the client.ncconf file while connecting to the cache. This happens when NCache directory (%NCHOME%) is not set.

Workaround

If %NCHOME% is not set, follow the steps below to set the environment variable for it:

  • Right-click on the Start button and select System.
  • In the right pane, click on Advanced System Settings.
  • In the System Properties window, go to the Advanced tab and click on the Environment Variables button.
  • In the Environment Variables window, under System Variables, click on New.
  • For Variable Name enter %NCHOME%.
  • For Variable Value enter C:\Program Files\NCache.
  • Click OK.
  • Now to update the system path in the same Environment Variables window, find the Path variable and click Edit.
  • In the Edit Environment Variable window, click New and add %NCHOME%\bin to the list. This makes sure that the tools are available globally.
  • Click OK to close each of the windows.

No server available to process request

Sometimes a client may be unable to connect to the cache because the Client/Server Port is blocked by a firewall.

Workaround

Make sure that the Client/Server port (at which NCache Service starts and accepts different client connections) is not blocked by a firewall. The default value of this port is '9800'. You can change the default value from Alachisoft.NCache.Service.exe.config or Alachisoft.NCache.Service.dll.config file.

Unable to communicate with server node

Cluster nodes can also be unable to communicate with each other because of Cluster Ports being blocked by a firewall.

Error: "Unable to communicate with server node"

Workaround

See if your firewall is allowing the Client/Server and Cluster port. A cluster port is a port at which nodes in a cluster communicate. You can change the default value of these ports from NCache Management Center. If you have specified a port range in the NCache Management Center, unblock all ports in the range.

Client Socket Deadlock

You may experience a deadlock situation on your client socket because of a long wait for a response from the cache server. This may also result in a lot of waiting threads that may cause performance issues.

Workaround

Step 1: Changes on Server-side

Go to the service configuration file:

  • Windows .NET 4.8: Alachisoft.NCache.Service.exe.config located in %NCHOME%\bin\service
  • Windows .NET: Alachisoft.NCache.Service.dll.config located in %NCHOME%\bin\service
  • Linux .NET: Alachisoft.NCache.Daemon.dll.config located in /opt/ncache/bin/service
  • Windows Java: Alachisoft.NCache.Service.dll.config located in %NCHOME%\bin\service
  • Linux Java: Alachisoft.NCache.Daemon.dll.config located in /opt/ncache/bin/service

Add the following lines in the configuration file:

<add key ="NCacheServer.EnableBadClientDetection" value = "true" />

The value "true" indicates that BadClientDetection is enabled on your client socket. This means that now if a deadlock situation arises on your client socket, the socket will be reset. The default value of this property is "false".

<add key ="NCacheServer.ClientSocketSendTimeout" value = "10" />

When you have enabled BadClientDetection, you can specify the time interval after which the socket is reset in case of a deadlock. The default value of this property is "10" and this value can not be less than 1.

A few other properties have also been introduced in Alachisoft.NCache.Service.exe.config to help with this issue. Please configure these properties as follows:

<add key ="NCacheServer.EventPriorityRatio" value="30"/>

On the server-side, a priority queue is used for events and cache operations. You can configure a ratio of events and cache operations for this queue through the property called EventPriorityRatio. The default value of this property is 30 which indicates a ratio of 30:70 for events and cache operations. The value of this property can not be less than 1.

<add key ="NCacheServer.EventBulkCount" value="50"/>

The cache server now sends events to clients in bulk. Through this property, you can set the number of items to be sent in bulk. By default, this value is 50 and the value can not be less than 1. This is available in client version 4124 and above.

<add key ="NCacheServer.EventBulkCollectionInterval" value="2"/>

This property is used to set the time interval, in seconds, after which the bulk of events is fired even in case the bulk count does not reach the EventBulkCount that the user has specified. The default value for this property is 2 seconds and the value can not be less than 1.

Restart the NCache Service for these changes to take effect.

Step 2: Changes on Client Side

Please make the following changes in your App.config/Web.config file:

<configuration>
    <appSettings>
          <add key ="NCacheClient.AsynchronousEventNotification" value="true"/>
          <add key ="NCacheClient.NumberofEventProccesingThreads" value="2"/>
     </appSettings>
</configuration>
  • AsynchronousEventNotification: Specifies whether events will be fired asynchronously or synchronously on the client-side. The default value is "true" which indicates that events will be fired asynchronously. The value "false" means that events will be fired synchronously on the client-side.

  • NumberofEventProcessingThreads: When the user has configured events to be fired synchronously, this flag is used to specify the number of threads that will process these events on the client-side. A very large value can cause performance issues.

See Also

.NET: Alachisoft.NCache.Client namespace.
Java: com.alachisoft.ncache.client namespace.
Python: ncache.client class.
Node.js: Cache class.

In This Article
  • Prerequisites for Cache Connectivity
  • Connect to Single Cache
  • Connect to Multiple Caches
  • Connect to Cache Using CacheConnectionOptions
    • Connect to Cache Using CacheConnectionOptions for Load Balancer
  • Connect to Clustered and Client Cache
  • Connect to Cache with Security Credentials
  • Connect to Cache Using Public IP's
  • Troubleshooting
    • Port is Not Accessible
    • Unable to Find client.ncconf
    • No server available to process request
    • Unable to communicate with server node
    • Client Socket Deadlock
  • See Also

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top