Caching in The Distributed Environment
Caching in The Distributed Environment
Abhijit Gadkari
Different types of cache like temporal , spatial , primed and demand cache
Some Examples
Open Forum !
3
Basics
Storage Size On Board Cost per byte
Latency
RAM
Persistence
Hard Disk
Cloud
Data is stored in memory – i.e. L1, L2, L3 etc. known as cache. This
concept is extensively used in the von Neumann Architecture.
Activity Resource
Reference
Data Data
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 Availability
Data Type [1] Caching Strategy [1]
Reference Data 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 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.
Resource Data 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.
Unknown 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”[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 System’s
Virtual Memory [4]
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
7
Temporal Cache
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
In dynamic environment
Adaptive Caching Strategies
can be very effective
12
Caching in the ORM World!
Customer
I
M
cust_id type credit_allowed
P
3456 gold 1 E
D
7890 bronze 0 A
N
C
E
M
I Gold Silver Bronze
RDBMS S
M
A
T
RDBMS – persistent storage C In memory object graph
H
15
Transactional Cache
17
18
Chasing the Right Size Cache
Remember the 80-20 rule a.k.a. Pareto principle and the bell shaped
graph
19
Microsoft project code named Velocity [1]
http://msdn.microsoft.com/fi-fi/library/cc645013(en-us).aspx
Application Application
Database
Velocity
Physical
Named Cache implementation
Regions
Regions
Named Cache
Regions 21
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
22
Example [1]
// Create instance of cachefactory (reads appconfig)
CacheFactory fac = new CacheFactory();
// Simple Get/Put
catalog.Put("toy-101", new Toy("thomas", .,.));
24
Open Forum !
Abhijit Gadkari
[email protected]
Blog : http://soaas.blogspot.com/
25