For Impetus’ White Papers archive, visit- http://www.impetus.com/whitepaper
Most applications face challenges related to robustness, speed, and scalability. This paper focuses on Windows Server AppFabric, which provides a distributed in‐memory cache for applications data.
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Building High Performance and Scalable Applications Using AppFabric Cache- Impetus White Paper
1. Building High Performance and
Scalable Applications using
AppFabric Cache
W H I T E P A P E R
Abstract
Most applications face challenges related to robustness, speed,
and scalability. This white paper focuses on Windows Server
AppFabric, which provides a distributed in-memory cache for
applications data.
The paper talks about AppFabric cache’s ability to helps
organization create scalable, available, high-performance
applications by exposing a unified view of distributed memory
for client application consumption. It focuses on how AppFabric
allows companies to significantly improve application
performance by avoiding repetitive calls to the data source and
how it enables applications to scale with increasing demand, by
using a cache cluster that automatically handles the complexities
of load balancing.
Impetus Technologies, Inc.
www.impetus.com
2. Building High Performance and Scalable Applications using AppFabric Cache
2
Table of Contents
Introduction...........................................................................................................3
Key challenges........................................................................................................3
The solution ...........................................................................................................3
AppFabric Caching .................................................................................................4
Windows Server AppFabric Architecture...............................................................4
Key features of AppFabric Caching........................................................................6
How AppFabric Caching works ..............................................................................7
AppFabric Cache API............................................................................................11
Best Practices.......................................................................................................12
Constraints...........................................................................................................12
Summary..............................................................................................................13
References ...........................................................................................................13
3. Building High Performance and Scalable Applications using AppFabric Cache
3
Introduction
In the programming world, data is captured and stored into the file system,
database and other sources. Sometimes, the cost of storing and retrieving the
data is expensive. While advanced technologies allow the capturing and storing
of data in a relatively inexpensive way, however organizing and accessing data is
a big challenge. Technologies such as AppFabric cache can help organizations
develop next-generation applications that are data-driven, where the data can
reside in the memory.
Key Challenges
Normally an in-memory cache is used to store frequently used application data
that helps improve the performance of the application. Reading and writing
data into memory is faster than reading from and writing to a database or disk.
However, some challenges exist in the traditional in-memory cache.
• Due to limited memory available for an Application, the in-memory cache
size is limited. Only a limited size of data can be stored into the in-memory
cache.
• Data stored in an in-memory can be lost if the Application goes down due to
any failure.
• There is no simple technique that can reduce pressure from the data-tier
without complex data partitioning techniques.
• Storing big data mash-ups from multiple data repositories is also a
challenge.
The Solution
To overcome the above challenges, the Impetus engineering team has
undertaken a research on Microsoft’s AppFabric cache. AppFabric caching has
the capability to combine multiple computers into a single unified cache cluster.
By using AppFabric caching, users can scale out their .Net applications easily and
inexpensively. Here’s how:
• AppFabric Caching Services use a cache cluster that automatically manages
the complexities of load balancing. It also enables organizations to achieve
better scalability by adding more computers on demand.
4. Building High Performance and Scalable Applications using AppFabric Cache
4
• Users can achieve even higher availability by allowing Caching Services to
store copies of the data across the cluster. They can run AppFabric Caching
Services with any distributed application on the same server, or access the
services over a network on a remote server.
• AppFabric Caching Services enables users to improve application
performance significantly by avoiding unnecessary calls to the data source.
In addition, there is seamless integration with ASP.Net for storing session state
in the cache. This can provide ASP.Net sites with a cost-effective path for quickly
improving site performance and scale. The performance, scalability, and
availability of AppFabric caching services, in conjunction with AppFabric rich
data services, can facilitate rich Web and enterprise applications development
and deployment.
AppFabric Caching
Windows AppFabric Caching (previously called Velocity), is a distributed in
memory caching software. ‘Velocity’ fuses memory across multiple computers
to provide a single unified cache view to applications. It allows high-
performance in memory access to cached data across multiple servers that use
caching access. Thus, it is well suited for applications that run on Web farms.
Furthermore, it allows for clustering, so that companies can scale up their
caching solution as their needs grow. Windows AppFabric Caching can be
configured to run as a service accessed over the network or can be run
embedded with the distributed application.
Windows Server AppFabric Architecture
AppFabric Caching Services are not tightly coupled to ASP.Net, and it is possible to use
them at any layer of an application architecture, including clients, workflows, and
services. However, in the context of Web applications, AppFabric caching provides
ASP.Net developers with a seamless upgrade path to a distributed in-memory caching
option, when their application requirements exceed the capabilities of the default
ASP.Net caching system. Specifically, AppFabric provides an in-memory caching
solution that accomplishes the following:
• Supports greater scale-out by offloading caching to a dedicated cache tier.
• Offers high availability of cached data.
• Avoids sticky routing.
5. Building High Performance and Scalable Applications using AppFabric Cache
5
Most developers are familiar with n-tier application architectures in which the
application is broken out into tiers. These typically include three classic layers: the
user interface, the business logic layer, and the data layer. Of course, users can break
these layers down further to provide for additional scalability and flexibility. The figure
below shows the presence of a fourth layer, the Caching tier. In this case, when users
first acquire some item of data, such as information from the ASP.Net application or
values read from a database, they can use an AppFabric Caching Services tier to
explicitly store this information in the cache cluster under a unique name. Later, the
application can directly read data from the cache cluster, without needing to go again
to the database.
Figure-1: AppFabric Cache Architecture
6. Building High Performance and Scalable Applications using AppFabric Cache
6
Key Features of AppFabric Caching
Figure-2: AppFabric Capabilities
Caching features of Windows Server AppFabric
• Data is stored on multiple nodes, improving availability.
• There is caching of any serializable CLR object.
• There is integration with ASP.Net, making it possible to cache ASP.Net
session data in the cache via the custom session state provider. This
increases the performance and scalability of ASP.Net applications.
• Automatic load balancing across cache cluster, thereby improving
scalability.
• There are regions, where data is segregated into logical segments.
• There is an Expiration feature, which allows time span based expiration of
cached data.
• The Evictions feature allows memory conservation.
• AppFabric cache supports enterprise scale: that is, tens, to hundreds of
computers.
7. Building High Performance and Scalable Applications using AppFabric Cache
7
How AppFabric Caching Works
This section describes the caching concepts that are useful in programming with
AppFabric.
Logical Hierarchy
Logical Hierarchy of AppFabric caching consists of Machine -> Cache Host ->
Named Caches -> Regions -> Cache Items -> Objects
• The machines are servers which run Cache Hosts.
• Cache Hosts are the physical processes that host the AppFabric Caching
instance. A user can run multiple processes that host a cache instance,
called Cache Hosts.
• Named Caches are the logical entities which can span across machines. The
named cache consists of regions, which store cache items.
• Regions are physically co-located Containers of Cache Items and may be
implicitly or explicitly created.
• The Cache Item is the key, Payload (Object) and has associated Tags,
Timestamps, and Versions.
8. Building High Performance and Scalable Applications using AppFabric Cache
8
Figure-3: Hierarchy of AppFabric Cache
Named Cache
A named cache is a configurable unit of in-memory storage that all applications use to
store data in the distributed cache. Users can configure one or more named caches for
each of their applications. Each cache can be configured independent of the others,
which lets users optimize the policies of each cache for their applications. All physical
configurations and cache policies such as failover, expiration, eviction, etc., are
specified at this level. All the applications that need to communicate to the same
named cache must instantiate the same named cache using the Data Cache Factory.
All caches are defined in the cluster configuration. The Windows PowerShell
administration tool can be used to create or reconfigure caches. Some settings can
only be configured when the cache is first created. Other settings can be changed
later, but may require the entire cache cluster to be restarted. There is a limit of 128
named caches.
Region
Regions are an additional data container that can be placed in the cache. Regions are a
cache construct: they are not defined in the cluster configuration settings. Regions are
9. Building High Performance and Scalable Applications using AppFabric Cache
9
optional; if required, they must be explicitly created at run time with the application
code by using the Create Region method.
An application is not required to use Regions and can use the put/get/remove APIs
with just the key to the object. In fact, the application will scale better when not using
Regions because the keys can be distributed across the named cache. If no Region is
specified, the system automatically partitions the keys into multiple implicitly created
Regions.
Cache Item
The Cache Item actually contains the object. The Cache Item stores the object with the
key, the object payload, tags, and the time to live (TTL), the created timestamp, the
version, and other internal book keeping information. A Region contains one or more
of these Cache Items.
Availability
This is one of the important features of the Windows Server AppFabric Cache. If one
node (machine) of the distributed cache gets down, users can get data from the
secondary node. When using a partitioned cache, it is possible to specify a number of
nodes as secondary cache. By doing this, users can store the same data on multiple
computers. Therefore, if one node fails then one of the secondary nodes becomes a
primary node and enables applications to continue accessing the data that was stored
on the computer.
10. Building High Performance and Scalable Applications using AppFabric Cache
10
Figure-4: Primary Cache and Secondary Cache routing
Local Cache
The Local Cache in AppFabric is similar to in-memory cache. Local Cache objects are
stored within the same process space in which the application is running. The server
stores objects in a serialized manner in the cache. When a cache client requests an
object from the cache, the server sends it to the client over the network. The cache
client then desterilizes the object for use by the client application. To speed up the
process of retrieving an object, users can enable the Local Cache.
11. Building High Performance and Scalable Applications using AppFabric Cache
11
AppFabric Cache API
The AppFabric Caching Services API is fairly simple. Users must first create an
instance of the Cache Factory object, which under the hood reads the
configuration settings from Web.config/app.config of their ASP.Net or
WF or WCF service. The need this Cache fFactory Object to retrieve a
named data cache object. These will be the least required steps for users
wanting to access the cached objects (data) from the AppFabric Caching
Services. The following code excerpt showcases this technique:
//Create instance of CacheFactory
DataCacheFactory_factory= new DataCacheFactory();
//Get a named cache from the factory
DataCache_cache= _factory.GetCache("default");
From here, users can deploy simple Get and Put commands to read or write
data from, or to the AppFabric caching storage. This snippet uses these two
commands to write and then read custom data from the AppFabric Cache:
//Put data into a cache
_cache.Put("id01", new Item("Test", "25.00", "..."));
...
//Get the data from the same or a different client
Item MyItem = (Item) _cache.Get("id01");
// If not present in the cache
if (MyItem== null)
{
//Read from the backend layer
MyItem= ReadFromDatabase();
//Populate the Cache
_cache.Put("id01", new Item("Test", "25.00", "..."));
returnMyItem;
}
12. Building High Performance and Scalable Applications using AppFabric Cache
12
Best Practices
• Expiration time on Velocity should not be set to very small values, which can
lead to increasing memory consumption. This occurs because of delays
related to garbage collection calls―old data expires, but remains in
memory, and a new copy of the data is added to memory.
• Local Cache: For best performance, users are advised to enable only the
LocalCaches for objects that change infrequently. Using the Local Cache for
frequently changing data may increase the chance of a client working with
stale objects. In instances of frequently changing data, it is best to disable
the Local Cache and pull data directly from the cluster.
• The default setting for RequestTimeout is 10 seconds, and therefore users
are advised not to set it at 0. If they do, the application will see a timeout on
every cache call.
• Even though having just one Cache Server in the client configuration can
suffice, it is recommended to maintain as many cache server host names in
the config file.
Constraints
There is no direct support of cache dependency in Window Server AppFabric
caching. If data gets changed in the database, the same does not change in the
Cache Cluster.