Caching in the Distributed Environment

Click here to load reader

  • date post

  • Category


  • view

  • download


Embed Size (px)


This presentation is based on the article published in the Microsoft Architecture Journal Issue 17, focused on Distributed computing

Transcript of Caching in the Distributed Environment

Caching in the Distributed Environment Abhijit Gadkari

Based on the article published in the Microsoft Architecture Journal : Issue 17 Available on-line at

AgendaBackground info and basics Different types of cache like temporal , spatial , primed and demand cache Some Examples Caching in the ORM world! Transactional cache and Shared cache Managing the interaction Size of a cache and its impact on application performance Five minute introduction of Velocity Microsoft s Distributed Caching platform Open Forum !3

BasicsStorage Size Latency On Board RAM Hard Disk Cloud Cost per byte Persistence

Data is stored in memory i.e. L1, L2, L3 etc. known as cache. This concept is extensively used in the von Neumann Architecture.Memory Access time is measured in access time. Given an address , the memory presents the data at some other time Memory Access Time = Latency + Transfer Size / Transfer Rate [2]4

Types of Data


Reference Data

Activity Data

Resource Data

Understanding the different types of data and their semantics helps to understand the different caching needs that comes with usage of that data type. [1]5

Why ? For Performance and AvailabilityData Type [1] Reference Data Caching Strategy [1] Practically immutable, non-volatile and long lasting in nature ideal candidate for caching. Can be shared across processes / application. For example, zip code, state list, department list, etc. Activity data is generated by the currently executing activity as part of a business transaction. Only good for the life on the transaction. Short lived in nature. For example, shopping cart on e-commerce web site. Highly dependent on domain logic and volatile in nature. Cache only when required. [a.k.a. dont cache unless and until absolutely required]. Commonly associated keywords concurrency , locking, ACID, dirty read, corrupt cache, business logic, etc. For example, quantity information in an inventory application. DO NOT CACHE [ME]

Activity Data

Resource Data


Keep a data item in electronic memory if its access frequency is five minutes or higher, otherwise keep it in magnetic memory[2] Wikipedia defines cache as a temporary area where frequently accessed data can be stored for rapid access[3]6

Principle of Locality Based on work done in 1959 on Atlas Systems Virtual Memory [4]

Temporal Cache Good for frequently accessed , relatively nonvolatile data. For example, drop-down list on a web pageSpatial Cache Data adjacent to recently referenced data will be requested in near future. For example, GridView paging7

Temporal Cache

public sealed class Cache : IEnumerable

using System.Web.Caching


Spatial Cache

In .NET, cache can be synchronized using SqlCacheDependency9

Primed and Demand Cache [5,6] Primed and Demand cache is based on the future use of the data. Predating future is not easy and should be based on sound engineering principals The primed cache pattern is applicable when the cache or the part of the cache can be predicted in advance. For example, a web browser cache

The demand cache pattern is useful when cache can not be predicted in advance. For example, a cached copy of user credentials The primed cache is populated at the beginning of the application, whereas the demand cache is populated during the execution of the application10

Primed Cache

In .NET ICachedReport interface can be used to store the pre-populated reports. The primed cache results in an almost constant size cache structure11

Demand Cache

1 user can have many roles 1 role can have many permissions Managing demand cache Minimize memory leak Maximize hit-ratio Effective eviction policy

In dynamic environment Adaptive Caching Strategies can be very effective12

Caching in the ORM World!cust_id 3456 7890 type gold bronze credit_allowed 1 0I M P E D A N C E M I S M A T C H






RDBMS persistent storage

In memory object graph

Ms Entity Framework /LINQ JDO, TopLink, Hibernate, NHibernate

The ORM manager populates the data stored in persistent storage like database in the form of an object graph. An object graph is a good caching candidate13


Layered Cache ArchitectureThe layering principle is based on the explicit Separation of responsibilities

Cache layering is prevalent in many ORM solutions. For Example, Velocity, HibernateThe first layer represents the transactional cache and the Second layer is the shared cache designed as a process or clustered cache15

Transactional Cache Objects formed in a valid state and participating in a transaction can be stored in the transactional cache Strictly bounded by the ACID rules Transactional cache size is small size and short lived Thrashing , cache corruption and caching conflicts should be strictly avoided Many caching frameworks offer out of the box prepackaged transactional cache solution16

Shared Cache Can be implemented as a process cache or clustered cache. The clustered cache introduces resource replication overhead Shared cache is a read-only cache Distributed caching solutions typically implements a shared cache solution Can be implemented as an identity map. For example, caching read-only, static reports using ICachedReport17


Chasing the Right Size Cache Remember the 80-20 rule a.k.a. Pareto principle and the bell shaped graph


Microsoft project code named Velocity [1]

Distributed in-memory application cache platform Can store any serializable CLR object Allows clustering and provides ASP.NET session provider object so that ASP.NET session objects can be stored in the distributed cache without having to write to database20

Conventional Stack Application Application

Stack with Distributed Cache Application Application

Web Server[s] / App Server[s] Database

Web Server[s] / App Server[s] Distributed Cache Application Application Database

One Logical View Velocity Named Cache Physical implementation

RegionsRegions Named Cache Regions21

Features [1]Machine -> Cache Host -> Named Cache -> Regions -> Cache Items -> objects Cache Operations Get [select] Returns object or entire Cache item Add [insert]- Creates new entry else exception if entry exists Put[update] - Replaces existing entry or creates a new one Remove [delete]- Removes existing entry

Expiration and Eviction Policy is based on time-to-live [TTL] logicConcurrency model supports optimistic version based updates and pessimistic locking

Velocity can be deployed as a service or embedded within the application. For example, host application can be ASP.NET / .NET application22

Example [1]// Create instance of cachefactory (reads appconfig) CacheFactory fac = new CacheFactory(); // Get a named cache from the factory Cache catalog = fac.GetCache("catalogcache");// Simple Get/Put catalog.Put("toy-101", new Toy("thomas", .,.)); // From the same or a different client Toy toyObj = (Toy)catalog.Get("toy-101"); // Region based Get/Put catalog.CreateRegion("toyRegion"); // Both toy and toyparts are put in the same region catalog.Put("toyRegion", "toy-101", new Toy( .,.)); Catalog.Put("toyRegion", "toypart-100", new ToyParts()); Toy toyObj = (Toy)catalog.Get("toyRegion", "toy-101");23

ResourcesBased on the paper Caching in the Distributed Environment published in the Microsoft Architecture Journal : Issue 171. Microsft Project Code Named Velocity by N. Sampathkumar, M Krishnaprasad and A. Nori 2.Transaction Processing : Concepts and Techniques by Jim Gray and Andreas Reuter [ISBN: 1558601902] 3. 4. The Locality Principle by Peter J. Denning , Communications of the ACM, July 2005, Vol 48, No 7 5. Caching Patterns and Implementation, by Octavian Paul Rotaru, Leonardo Journal of Sciences LJS: 5:8 , January-June 2006 6. Data Access Patterns: Database Interactions in Object-Oriented Applications, by Clifton Nock, Addision Wesley24

Open Forum !Abhijit Gadkari [email protected] Blog :