What is AppFabric Caching?
• An explicit, distributed, in-memory application cache for
all kinds of data (CLR objects, XML, Binary data, etc.)
– Flows "memory" across machines into a unified cache
Clients can be spread
across machines or
processes
Unified Cache View
Clients Access the
Cache as if it was a
large single cache
Cache Layer
distributes data
across the various
cache nodes
Why Velocity?
* Share data across applications
No more sticky routing
* Peformance
Operation
Read 2k
MSDN Forums
Throughput
Latency
30,000 / sec
3 – 4 ms
Write 2k
18,000 / sec
5 Velocity Servers, 40GB Cache
98% of all calls to database come from cache
Result is from 60% load on SQL Server, down to 2%
* Scale out
3 ms
Operation
Throughput
Read 2k
1
30,000 / sec
Read 2k
2
58, 600 / sec
Read 2k
Velocity
Servers
3
85, 500 / sec
Basic terminology in Velocity
•
•
•
•
•
•
•
Cache host
Cache cluster
Cluster configuration storage
Machine 1
Machine
Machine
Named cache
2Cache
2Cache
Cache
Cache
host A
host B
host C
host D
Region
Named cache : Product catalog
Named cache : ShoppingCart
Cache item
Tags
Region A
Working with Velocity
// Create instance of CacheFactory, which reads app.config
DataCacheFactory factory = new DataCacheFactory();
// Get a named cache from the factory
DataCache cache = factory.GetCache("default");
// Cache.Put(string key, object value)
cache.Add("SDN", new SDNSession());
// Cache.Get(string key);
var meeting = (SDNSession)cache.Get("SDN");
// Via indexers is also an option
cache["what"] = new Object();
Object o = cache["what"];
Tags
var starWarsTag = new DataCacheTag("StarWars");
var tags = new List<DataCacheTag>();
tags.Add(starWarsTag);
tags.Add(new DataCacheTag("Force"));
tags.Add(new DataCacheTag("Sith"));
cache.Add("MyKey", "A New Hope", tags, "StarWarsRegion");
var result =
cache.GetObjectsByTag(starWarsTag, "StarWarsRegion");
foreach (var item in result)
{
Console.WriteLine("{0} has value of {1}",
item.Key, item.Value);
}
Optimistic locking
DataCacheItemVersion versionWillChange;
DataCacheItemVersion versionWithError;
// First get the current version 2 times
cache.Get("MyKey", out versionWillChange);
cache.Get("MyKey", out versionWithError);
// We change the key, version hasn't changed in Velocity yet.
cache.Put("MyKey", "MyNewValue", versionWillChange);
// Version has changed with previous update, this will #fail
cache.Put("MyKey", "MyErrorValue", versionWithError);
Pessimistic locking
DataCacheLockHandle lockHandle = null;
DataCacheLockHandle secondLockHandle = null;
// Lock our object
cache.GetAndLock("MyKey", new TimeSpan(0, 0, 10), out
lockHandle);
// This will still work
string result = (string)cache.Get("MyKey");
// Try to lock for 2nd time -> #fail
cache.GetAndLock("MyKey", new TimeSpan(0, 0, 10), out
secondLockHandle);
// This will break the lock!!!
cache.Put("MyKey", "MyNewValue");
ASP.NET Session integration
• SessionStoreProvider class
– Plugs into ASP.NET Session store
– Stores session state in Velocity
• Scale
– Session information available at all ASP.NET
Nodes
• High availability
– Session data is backed up on addditional
machines
– Resilient to machine or process failures
Access Control
• Two new protocols
– Created by Microsoft, Google, Yahoo, etc
– Oauth WRAP & SWT
– Authentication in HTTP Headers
– Usable even via JavaScript
– Ready for Geneva/WIF
Access Control
•
•
•
•
The future
No WS* available yet… or no longer?
CardSpace
Support for other web identity providers
– Windows Live
– Google
– Yahoo
– OpenId
Azure Pricing
• Access Control
– $1.99 / 100K transactions
• Service Bus Connections
– $3.99 per connection
– $9.99 for a pack of 5 connections
– $199 for a pack of 100 connections
• Data Transfers
– $0.10 in / $0.15 out per Gigabyte
Example architecture
SB
AC
The near future?
REST Based Service Layer
Website
Website
Sync
Framework
Website
AppFabric Hosting
Backen
d
Servic
e
MSMQ
Cache
SQL
N
L
B