5. Typical Web Architecture
âąNeed to get routed to
same machine (i.e. sticky
sessions)
Users
âąEach machine round trips
for data
âąData that is cached is
stored in the memory of
one server
Web
Tier
âąCPU and disk can get
saturated due to traffic
âąService access is slow
Data
Tier
9. Good butâŠ
ï Cache is scoped to machine / process
⊠Machines die
⊠Processes recycle
ï Cache memory is limited
10. What if?
ï You could have as much cache as you
wanted?
ï You could share a giant cache across
servers, services and even clients?
ï What if this was something you could
simply add to the platform for
1
free?
1Some features may require
certain editions of Windows
Server
12. Unified Cache View
What is AppFabric Caching?
ï An explicit, distributed, in-memory
application cache for all kinds of data
Caching clients
can be across
machines or
processes
Clients Access
the Cache as if it
was a large
single cache
Cache Layer
distributes data
across the
various cache
nodes
13. AppFabric Cache
âąNo need for sticky sessionsUsers
âąCached data available to all
servers
Web
Tier
âąMultiple machines means
scale and high-availability
âąLots of cache memory
Caching
Tier
âąReduces load on database
âąDuplicate Service Calls
eliminated
Data
Tier
14. Data Distribution - Partitioned Cache
⊠Web Tier
Cache
Tier
CacheService
CacheService
Cache
Service
A C EDB G IHF
ASP.Net App
Caching Client
ASP.Net App
Caching Client
ASP.Net App
Caching Client
AC ED BGI HF
ï Scale on Data Size
⊠More machines => More memory to cache
ï Scale on Cache Throughput
⊠More machines => keys distributed across more machines => better
throughput
21. Caching
Service
Caching
Service
Cachin
g
Service
Configuration Store
Install
AppFabric
Copy Client DLLs
âŠApplication Application Application<hosts>
<host name="BL1CDB8083714â
cachePort="22233"
cacheHostName="DistributedCacheService"/>
âŠ..
</hosts>
<localCache isEnabled=âtrue" ../>
<security ⊠/>
Caching Access
Layer
Caching Access
Layer
Caching Access
Layer
.NET 3.5 SP1 OR .NET 4
Configure
AppFabric
.NET 4
Update Web.config
Deployment
22. Usage Pattern â Cache Aside
(Explicit Caching)
// Read from Cache
Toy toyObj = (Toy)
catalog.Get("toy-101");
Application
Caching
Service
Database
// If Not present in the cache
if (toyObj == null)
{
// Read from backend..
toyObj = ReadFromDatabase();
// Populate Cache
catalog.Put("toy-101", toyObj);
return toyObj;
}
Caching Access
Layer
23. Administration
ï PowerShell cmdlets are used to administer
the cache cluster
ï Rich set of cmdlets for
⊠Cache cluster management
⊠Cache creation and monitoring
26. Security
ï Domain Based Security Option
⊠Domain Account / Local Account based
Authentication
⊠Only authorized servers can join the cluster
⊠Only authorized clients can connect to the
cluster
ï Transport Level Security
⊠Turn on/off Signing or Encryption
ï Can turn off Cache Security
⊠Use Firewalls, IPSec, VLANs to protect cache
grant-cacheallowedclientaccount RedDomainMachine1$
grant-cacheallowedclientaccount RedDomainJohn
27. Logical Hierarchy
ï Machine
ï Host
⊠Physical processes hosting
AppFabric Caching instance.
ï Named Caches
⊠Can span across machines
⊠Defined in the configuration file
ï Regions
⊠Physically co-located Container of
Cache Items
⊠May be implicit or explicitly created
ï Cache Item
⊠Key, Payload (Object ), Tags, TTL,
Timestamps, Version
Regions
Region A
Key Payload Tags
121 xxxx âToyâ âChildâ
123 yyyy âToyâ âChairâ..
AppFabric
Caching
Service
Named Cache : Product Catalog
Named Cache : Electronics Inventory
AppFabric
Caching
Service
AppFabric
Caching
Service
AppFabric
Caching
Service
28. AppFabric Caching API
// Create instance of cachefactory (reads appconfig)
DataCacheFactory fac = new DataCacheFactory();
// Get a named cache from the factory
DataCache catalog = fac.GetCache("catalogcache");
// Simple Get/Put
catalog.Put("toy-101", new Toy("Puzzle", .,.));
// 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("toy-101", new Toy( .,.), âtoyRegionâ);
Catalog.Put("toypart-100", new ToyParts(âŠ), âtoyRegionâ);
Toy toyObj = (Toy)catalog.Get("toy-101â,"toyRegion");
29. Access APIs â Tagging Items
Tag hotItem = new Tag("hotItem");
catalog.Put("toy-101", new Toy("Puzzle"),
new Tag[]{hotItem}, âtoyRegionâ);
catalog.Put("toy-102", new Toy("Bridge"), âtoyRegionâ);
// From the same or a different client
List<KeyValuePair<string, object>> toys =
catalog.GetAnyMatchingTag("toyRegion", hotItem);
30. Types of Data
Reference Activity Resource
Primary Read Only Read-Write Not
shared
Read-Write, Shared
Catalog Data Shopping Cart Auction Data/Seat
Assignment
Web Tier
Distributed Cache
Shopping Cart
Grocery
Catalog
Grocery
Inventory
Grocery Shop
31. Application
Application
AppFabric Caching Client
Reference Data â Performance
ï Catalog data doesnât change often
ï Unnecessary network cost to access from different machines
ï Solution â Local Cache
Put(K2, v3)
Routing Table
Cache2Cache1
Primary for K2,V2
K2, V2
Primary for K1,V1
K1, V1
Cache3
Primary for K3,V3
K3, V3
AppFabric Caching Client
Local Cache
Routing Table
K2,
V2
Get(K2)
Get(K2)
K2, V3
32. Reference Data â Bulk Get
ï Bulk Fetch from region
⊠200-300k ops per second
⊠Fewer network calls
Catalog.BulkGet(
new List<string>(){âtoy-101â, âtoy-102â} ,
âtoyRegionâ);
33. Activity Data â Session Integration
âŠ
Session State
stored in
AppFabric
Caching
CacheService
Caching
Service
Load Balance Requests
No more sticky routing
CachingService
Scale your
Session Store
Dynamically
Highly
Available
Drop in AppFabric
Caching
SessionStoreProvider
Allows session state to be shared amongst multiple applications
Application
Caching Access
Layer
Application Application
Caching Access
Layer
Caching Access
Layer
<sessionState mode="Customâ
customProvider="SessionStoreProvider">
<providers>
<add name="SessionStoreProvider"
type=âMicrosoft.Data.Caching.DataCacheSessionStoreProvider,
ClientLibraryâ
cacheName="<YourNamedCache>"/>
</providers>
</sessionState>
34. Application
Application
(K2, V2)
Cache2Cache1 Cache3
Primary for
Activity Data - Availability
Get(K2)
Primary for Primary for
K3, V3
AppFabric Caching Client
Routing Table
K2,V2
PUT
Secondary for
K2, V2
K1, V1
Secondary for
K3, V3
Secondary for
K1, V1
AppFabric Caching Client
Routing Table
K2, V2
Replicatio
n AgentK2, V2
35. Resource Data - Optimistic Locking
ï GetCacheItem returns a version object
ï Every update to an object internally increments it's version
ï Supply the version obtained along with the Put/Remove
ï Put/Remove will succeed only if the passed in version matches
the version in the cache
Version Based Update
Time Client1 Client2 (Different Thread or
process)
T0 CacheItem item =
catalog.GetCacheItem(âPlayerRe
gionâ, âZuneâ);
CacheItem item =
catalog.GetCacheItem(âPlayerRe
gionâ,
âZuneâ);
T1 ((ZuneObject)item.Object).inven
tory --;
((ZuneObject)item.Object).invent
ory--;
T2 catalog.Put(âPlayerRegionâ,
âZuneâ,
item.Object,
item.Version);
T3 catalog.Put(âPlayerRegionâ,
âZuneâ,
item.Object,
item.Version);
// Version mismatch
// Client must retry again
Two clients access the same
item
Both update the item
Second Client gets in first;
put succeeds because item
version matches; atomically
increments the version
First client tries put;
Fails because the versions
donât match
36. K1
Resource Data - Pessimistic Locking
ï Take locks on non-existent keys
⊠Allows you to co-ordinate calls for data
Client1:
GetAndLock ("k1")
Client2:
GetAndLock ("k1")
Client3:
Get ("k1")
Regular Get
succeeds
GetAndLock gets lock
handle
Other GetAndLock on
same item fails
In the future we will be bringing these capabilities to the cloud and bringing some of the cloud capabilities such as service bus and access control to the on-premise server solution as well.