This action might not be possible to undo. Are you sure you want to continue?
Based on the article published in the Microsoft Architecture Journal : Issue 17 Available on-line at http://www.msarchitecturejournal.com/pdf/Journal17.pdf
Background 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 !
Storage 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 
Types of 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. 
Why ? – For Performance and Availability
Data Type  Reference Data Caching Strategy  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. don’t 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]
“Keep a data item in electronic memory if its access frequency is five minutes or higher, otherwise keep it in magnetic memory” Wikipedia defines cache as “a temporary area where frequently accessed data can be stored for rapid access”
Principle of Locality Based on work done in 1959 on Atlas System’s Virtual Memory 
Temporal Cache Good for frequently accessed , relatively nonvolatile data. For example, drop-down list on a web page
Spatial Cache Data adjacent to recently referenced data will be requested in near future. For example, GridView paging
public sealed class Cache : IEnumerable
In .NET, cache can be synchronized using SqlCacheDependency
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 application
In .NET ICachedReport interface can be used to store the pre-populated reports. The primed cache results in an almost constant size cache structure
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 effective
Caching in the ORM World!
cust_id 3456 7890 type gold bronze credit_allowed 1 0
I 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 candidate
Layered Cache Architecture
The layering principle is based on the explicit Separation of responsibilities
Cache layering is prevalent in many ORM solutions. For Example, Velocity, Hibernate
The first layer represents the transactional cache and the Second layer is the shared cache designed as a process or clustered cache
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 solution
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 ICachedReport
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  http://msdn.microsoft.com/fi-fi/library/cc645013(en-us).aspx
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 database
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
Regions Named Cache Regions
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] logic
Concurrency 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 application
// 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");
Based on the paper “Caching in the Distributed Environment” published in the Microsoft Architecture Journal : Issue 17
1. 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. http://en.wikipedia.org/wiki/Cache 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 Wesley
Open Forum !
Abhijit Gadkari Abhijit.Gadkari@gmail.com Blog : http://soaas.blogspot.com/
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue reading from where you left off, or restart the preview.