Ingo Rammer thinktecture [email protected].

30
Ingo Rammer thinktecture [email protected] Verteiltes Caching mit Windows Server AppFabric

Transcript of Ingo Rammer thinktecture [email protected].

Page 1: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Ingo [email protected]

Verteiltes Caching mit Windows Server AppFabric

Page 2: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Ingo Rammer und thinktecture

Unterstützung und Consulting von Software-Entwicklern und –Architekten im Windows- und .NET-Umfeld

• Entwickler-Coaching und –Mentoring• Architektur-Consulting und –Prototyping• Architektur- und Code-Reviews• Anwendungsoptimierung, Troubleshooting, Debugging

http://[email protected]

Page 3: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Caching in 30 Seconds

•AppFabric Caching (Velocity) is a Windows Service for highly scalable distributed caching•Multiple physical nodes are combined to form one big virtual cache

Page 4: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

So first … let‘s start the virtual machines …

Page 5: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

WebServer #1

WebServer #2

WebServer #3

WebServer #n

Load Balancer

Data

User

Page 6: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Web Server

Data

Application

Session State

LocalCaching

Page 7: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Today‘s Issues

Session stateInProc, Session server, database?Availability, Single-Point-Of-Failure, Performance

CachingCache consistency when using multiple servers

Page 8: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

WebServer

#1

WebServer

#2

WebServer

#3

WebServer

#n

Load Balancer

CacheServer

#1

CacheServer

#2

AppFabric Caching

Page 9: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

WebServer

#1

WebServer

#2

WebServer

#3

WebServer

#n

Load Balancer

CacheServer #1

CacheServer

#2

Page 10: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Configuring AppFabric

AdministrationPowershellCluster config centrally in XML file or in SQL Server

Page 11: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Notebook directly: Windows 7 with IIS and Visual StudioFour virtual machines:

VCacheDC

Domain ControllerSQL Server

VCache01

AppFabric

VCache02

AppFabric

VCache03

AppFabric

Page 12: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Named Caches

VCache01 VCache02 VCache03

Cache: default

Cache: sessiondata

Additional caches … with different configuration

Page 13: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Cache Administration

Administration via PowershellLoading the module

Import-Module DistributedCacheAdministrationGet-CacheHelp

Creating a new cache: New-Cache democacheStarting the Cluster: Start-CacheClusterStatus: Get-CacheHost

Page 14: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

AppFabric Caching w/o Security

Export-CacheClusterConfig c:\test.xmlAdd advancedProperties

Import-CacheClusterConfig c:\test.xml

<dataCache> <advancedProperties> <securityProperties mode="None" protectionLevel="None" /> </advancedProperties> <caches>…</caches> <hosts>…</hosts></dataCache>

Page 15: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

AppFabric Caching „Hello World“

Configuring the client

<configuration> <configSections> <section name="dataCacheClient" type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /></configSections>

<dataCacheClient> <hosts> <host name="VCache01" cachePort="22233“ /> </hosts> <securityProperties mode="None" protectionLevel="None"/></dataCacheClient>

Page 16: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

AppFabric Caching „Hello World“

•Adding References from C:\Windows\System32\AppFabricMicrosoft.ApplicationServer.Caching.Client and Microsoft.ApplicationServer.Caching.Core

Have to be copied firstDataCacheFactory fact = new DataCacheFactory();DataCache myCache = fact.GetCache("democache2");

string someKey = "myKey";string someValue = "testing"; // anything [Serializable]

myCache.Put(someKey, someValue);

string result = (string) myCache[someKey];

Page 17: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

VCache01 VCache02 VCache03

Cache: default

Storing and Distribution.NET Client

Prod-1Prod-2

Prod-4 Prod-3

DataCache defaultcache = GetCache("default");Product prod = GetProduct(...);defaultcache.Put("Prod-1",prod);

defaultcache.Put("Prod-2",prod2);defaultcache.Put("Prod-3",prod3);defaultcache.Put("Prod-4",prod4);

Other named caches …

Page 18: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Scaling the Cache

Scaling sizeMore machines == more memory

Scaling throughputMore machines == higher throughput

„Local Cache“Directly at client == massively lower latencyHigher throughput when readingBut also: items are older, can get stale

Page 19: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Item Routing

Velocity #1 Velocity #2 Velocity #3

Prod-1

.NET Client

Dynamic Routing

Table

Prod-2

Prod-4Prod-3

defaultcache.Put("Prod-1",prod);

Page 20: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Node Failure

Velocity #1 Velocity #2 Velocity #3

Prod-1

.NET Client

Dynamic Routing

Table

Prod-2

Prod-4Prod-3

object p1 = defaultcache["Prod-1"];object p1 = defaultcache["Prod-1"];

if (p1==null){ p1 = GetProductFromDatabase(...) defaultcache.Put("Prod-1", p1);}

Prod-1

Page 21: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Regions and Tags

•Optional•All elements in a region live on the same node•Can be tagged•Allow BulkGet and bulk delete

Page 22: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Kinds of Data

Reference Data: "never" changes (catalog data, price lists, …)Activity data: non-concurrent changes (session data, shopping cart, …)Resource data: high concurrency data, constantly changing (inventory level, …)

Page 23: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Analysing DataPerformance – Throughput and latencyConsistency – Guaranteed or eventualRecoverability – Can the data be restored from DB (cache eviction?)Availability – What happens at node failure

Page 24: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Local CacheChosen in config or codeIn practice: often code, because only specific parts should be cached locally (DataCacheFactory)Good for reference dataSynchronization

Age of itemsNotification

Page 25: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Notifications

Pub/sub on cache item changesCallbacks for complete cache, for a region or for a single itemAre currently (beta 2) queued at server side and regularily polled by the clientCache has to be configured for notifications

New-Cache <name> -NotificationsEnabled true

Page 26: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Concurrency

•Optimistic and pessimistic locking

DataCacheItemVersion ver = default(DataCacheItemVersion);object itm = defaultCache.Get("region", "key", ref ver);

// after change (throws an exception if modified in meantime)defaultCache.Put("region","key", itm, ver);

DataCacheLockHandle lockHandle;object itm = defaultCache.GetAndLock("region", "key", TimeSpan.FromSeconds(30), out lockHandle);

// after change:defaultCache.PutAndUnlock("region", "key", itm, lockHandle, null);

Page 27: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Session State

<sessionState mode="Custom" customProvider="Velocity"> <providers> <add name="Velocity" type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider, Microsoft.ApplicationServer.Caching.Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" cacheName="sessiondata" retryCount="5"/>

</providers></sessionState>

Page 28: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

High Availability

Velocity #1 Velocity #2 Velocity #3

Sess-1(Primary)

sessionCache.put("Sess-1", se1);

Sess-1 (Secondary)

Sess-2(Primary)

Sess-2 (Secondary)

sessionCache.put("Sess-1", se1);sessionCache.put("Sess-2", se2);

Page 29: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Failover with High Availability

Velocity #1 Velocity #2 Velocity #3

Sess-1(Primary)

sessionCache.put("Sess-1", se1);

Sess-1 (Secondary)

Sess-2(Primary)

Sess-2 (Secondary)

sessionCache.put("Sess-1", se1);sessionCache.put("Sess-2", se2);

Sess-2(Primary)

Sess-2 (Secondary)

Sess-1(Secondary)

Page 30: Ingo Rammer thinktecture ingo.rammer@thinktecture.com.

Configuring High Availability

•Powershell• New-Cache <name> -Secondaries 1 •Maybe also:• –Eviction None • –NotExpirable true

Starting with Beta2: Only supported on Enterprise (and Data Center) operating system editions