everything you wanted to know about velocity (but were afraid to … · 2010. 4. 12. · everything...

40
Everything you wanted to know about Velocity (but were afraid to cache) (but were afraid to cache) Scott Colestock [email protected] MarcatoPartners, LLC

Upload: others

Post on 09-Sep-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Everything you wanted to know about

Velocity

(but were afraid to cache)(but were afraid to cache)

Scott Colestock

[email protected]

Marcato Partners, LLC

Page 2: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

What is it?

Velocity is a distributed in-memory key/value cache that provides .NET developers with a way to increase

performance and scalability when writing data-centric applications.

Page 3: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

What is it? (2)

• The combined RAM available to all servers in a

Velocity cluster is presented to Velocity clients

as a unified whole

• Any serializable CLR object can be stored• Any serializable CLR object can be stored

– Actual location within cluster is transparent

– Client is a simple key/value API at heart

• Run as a service accessed across the network

• Additional servers can be added on demand

Page 4: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

What we’ll cover

• What motivates this product/technology

• Terms / Pictures / Concepts

• Deploy / Install Process

• A lap around the API & Admin model• A lap around the API & Admin model

• Demos

• Gotchyas

Page 5: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Motivation

• Data-centric applications have been the norm for a long while– Relational data

– More recently, “service-obtained” data

• Velocity is about increasing performance by bringing the data physically closer to the consumer

• Velocity is about increasing performance by bringing the data physically closer to the consumer– Reduce pressure on underlying data stores/services

• Velocity can be about storing data in value-added form (logically closer to the consumer)– Object graphs

– Output caching (not explicit in V1)

– Aggregated data in xml or other transformed formats

Page 6: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Motivation (2)

• Databases are always a point of high contention

as you scale out, and tuning is expensive

– Are your data retrieval sprocs getting harder to

maintain - excessive sql chops required?maintain - excessive sql chops required?

• Service calls for reference data (internal/external)

are often slow or intentionally throttled

• Caching has always been considered a solution

for these issues…

Page 7: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Motivation (3)

• Machine-local caching solutions (like Microsoft’s “Enterprise Library Caching Application Block”) can provide partial answer– Easy key/value API

– Flexible store (memory, disk-backed, etc.)

– Flexible expiration and eviction policy– Flexible expiration and eviction policy

• Limitations:– Limited by the memory available to a single node…

– Application recycles typically mean you lose the cache

– In a load-balanced environment, a large data set means you will frequently “miss” when attempting to load from cache…

Page 8: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Motivation (4)

Key 3,5,23

Machine-local caches wind

up being sparsely populated

when used with a load

balancer (if the data set has

many keys)

Load Balancer

Key 7,11,47

Key 12,16,33

Page 9: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Motivation (5)

• Without a distributed cache, you have no central place to update/delete

• This means you can only cache data that can afford to be stale by some time period

– If the time period is short, you need a low TTL (time-to-– If the time period is short, you need a low TTL (time-to-live, aka expiration) which means more cache misses

• You can’t cache data that must have changes visible to the system in (near) real time

• With a distributed cache, you have one cache to shoot in the event of an update/delete

– Might be able to live with no expiration

Page 10: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

What we’ll cover

• What motivates this product/technology

• Terms / Pictures / Concepts

• Deploy / Install Process

• A lap around the API & Admin model• A lap around the API & Admin model

• Demos

• Gotchyas

Page 11: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Windows Server AppFabric Caching

• History: AppFabric caching was a separate component

– Public debut at TechEd 2008 (earlier?)

– Codename: Velocity– Codename: Velocity

• “Dublin” was a separate effort, focused on providing a hosting and management environment around WCF/WF

• November 2009: Technologies grouped under heading of “Windows Server AppFabric”

Page 12: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Relationship to Windows Azure

AppFabric• Service bus: Handle communication and authentication

for accessing applications– Expose apps through firewalls, NAT gateways, etc.

– Assist cloud-based apps talking to on-premise apps

– Other composite app scenarios; pub/sub

• Access Control Service: Allow you to avoid setting up • Access Control Service: Allow you to avoid setting up federated identity agreements just to grant partner/customer access to your cloud-based or on-premise apps.

•Today: Only common

marketing/branding with Windows

Server AppFabric.

•Later: Common services for both

Page 13: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Cache-Aside Pattern

• In the current version, the out-of-box support

is for the “cache-aside” pattern.

– Check cache

– If miss, retrieve data, then populate the cache– If miss, retrieve data, then populate the cache

• Lots of other patterns you might contemplate

(and simulate) with what is provided

– Read-through/Write-through

– Refresh-ahead/Write-behind

Page 14: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Cache-Aside Pattern

Page 15: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Cache Cluster

Logical Hierarchy

Server A

Cache Host A

Server B

Cache Host B

Server C

Cache Host C

Client apps work with a

single logical unit of cache

Regions can

be implicit

or explicit.

Use explicit

only for

Named Cache: Product Catalog

Default Cache

Region: Sports

Region 1 Region 3

Server process is

DistributedCacheService.exe

Caches

explicitly

created

with TTL,

expiration,

HA policy

Regions represent a partition of

data (subset of key/value pairs).

Live on one node. Unit of

replication/failover.

only for

bulk gets or

searching.

Page 16: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Logical Hierarchy

ID (Key) Payload

(Value)

Tags/VersionInfo

1 Foo …

2 Bar …

3 Baz …

Named Cache: Product Catalog

Default Cache

Region: Sports

Region 1

Page 17: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Cache Cluster

Physical Layout

Web Server A

IIS 7.x

Web Server B

IIS 7.xLoad

Balancer

Cache Server A

Cache Host

Cache Server B

• Cache servers designed to run in a domain

• Caches can have access control applied…

• Consider the nature of data stored in cache, and secure appropriately (don’t let cache be weakest link)

IIS 7.x

Web Server C

IIS 7.x

BalancerCache Host

Cache Server C

Cache Host

Page 18: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Combined Deployment

Web Server A

IIS 7.x

Web Server B

Cache Host

Web Server B

IIS 7.x

Web Server C

IIS 7.x

Load

Balancer Cache Host

Cache Host

Page 19: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Physical LayoutCache Cluster

Web Server A

IIS 7.x

Web Server B

IIS 7.xLoad

Balancer

Cache Server A

Cache Host

Cache Server B

Cache Host

Config

Store

(File share or

Sql Server)

• Configuration store contains cache policies and global partition map (how keys divide into regions, which servers have which regions)

• If Sql config store, servers will send heartbeat to Sql. Otherwise, heartbeat goes to one or more “lead hosts”

• Partition map used by “Global Partition Manager” (one node in the cluster, but auto failover) to communicate routing information to Velocity clients

Web Server C

IIS 7.x

Cache Host

Cache Server C

Cache Host

Sql Server)

Page 20: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Regions as unit of replication/failover

(Global Partition Manager in action)

Cache Cluster

Server A

Cache Host A

Server B

Cache Host B

Server C

Cache Host C

Named Cache: Product Catalog

Default Cache

Region: Sports

Region 1

Page 21: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Regions as unit of replication/failover

(When using Secondaries)

Cache Cluster

Server A

Cache Host A

Server B

Cache Host B

Server C

Cache Host C

Named Cache: Product Catalog

Default Cache

Region: Sports

Region 1

Sports secondary

Region 1 secondary

(Updates done synchronously)

Page 22: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Local CacheCache Cluster

Web Server A

IIS 7.x

Web Server B

IIS 7.xLoad

Balancer

Cache Server A

Cache Host

Cache Server B

Cache Host

Local

Cache

Local

Cache

• Local cache is an option that can be enabled when creating the cache client (DataCacheFactory)

• Allows a local cache to be populated that will prevent network hop (and serialization) if request

can be satisfied locally

• Best when data set is (relatively) small, changes infrequently, and stale data is acceptable

• Can expire via TTL or notifications (which might be late/lost)

• Can specify max object count before evicting LRU

Web Server C

IIS 7.xCache Server C

Cache HostLocal

Cache

Page 23: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Data Types and Caching

Considerations• Reference Data: Product catalogs, “lookup” tables, other

slow-moving content– Safe to cache for a defined period of time because you probably

live with staleness already

– “Local” cache option might be desirable for small data sets

• Activity Data: Shopping carts or other transient transaction • Activity Data: Shopping carts or other transient transaction state– Accessed for read and write operations, but not shared.

Low/No concurrency considerations – exclusive write.

– Safe to cache for reads and keep in cache for writes

• Resource Data: Inventory, Orders, and other core transactional data– Accessed concurrently for read and write

– Caching will require a concurrency model to be chosen and managed

Page 24: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

What we’ll cover

• What motivates this product/technology

• Terms / Pictures / Concepts

• Deploy / Install Process

• A lap around the API & Admin model• A lap around the API & Admin model

• Demos

• Gotchyas

Page 25: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Deploy/Install Considerations

• Windows “Application Server” Role required

• Hotfix required for Vista/Win2k8; not for Win7/Win2k8R2

• You’ll need Powershell 2 (already in Win7/Win2k8R2)

• You’ll need Powershell 2 (already in Win7/Win2k8R2)

• .NET3.5SP1 for cache clients; .NET4 for servers

• Windows XP cannot be a client…

• “Install” and “Configure” for AppFabric are two distinct steps (much like BizTalk)

Page 26: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Deploy/Install Considerations

• Primary screen of

interest is choosing your

configuration store:

– XML/File share

– Sql-Based

• File share avoids the

need for Sql Server, but

requires that some requires that some

nodes in the cache

cluster be special (“Lead

Hosts”)

• Using Sql as the

configuration store is

the better engineering

choice for production –

you may have other

reasons to avoid it.

Page 27: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Deploy/Install Considerations

• As you build out your Velocity Cache Cluster,

you will do “New Cluster” on the first node,

and “Join Cluster” on subsequent nodes

• Ultimately, all of Windows Server AppFabric is • Ultimately, all of Windows Server AppFabric is

a set of features underneath the Application

Server Role – so standard command line

installations work.– Setup.exe /i CacheAdmin,CacheService,CacheClient

Page 28: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

AppFabric as Application Server

“Role Service”

Page 29: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Deploy/Install Considerations

• Can do a “Cache client” install for clients, or

for internal apps, just incorporate client

assemblies in your own build/deploy processMicrosoft.ApplicationServer.Caching.Core.dll

Microsoft.ApplicationServer.Caching.Client.dllMicrosoft.ApplicationServer.Caching.Client.dll

Microsoft.WindowsFabric.Common.dll

Microsoft.WindowsFabric.Data.Common.dll

Page 30: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

What we’ll cover

• What motivates this product/technology

• Terms / Pictures / Concepts

• Deploy / Install Process

• A lap around the API & Admin model• A lap around the API & Admin model

• Demos

• Gotchyas

Page 31: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Caching Classes

DataCacheFactory

DataCacheFactory()

DataCacheFactory(configuration)

DataCache GetCache(string cache)

GetDefaultCache()

DataCache

Add

Adds a new object to the

cache. Exception if the item

is already in the cache.

DataCacheFactoryConfiguration

LocalCacheProperties

NotificationProperties

SecurityProperties

DataCacheServerEndpoint[] Servers

(Can set these via configuration)

is already in the cache.

Put

Adds a new object to the

cache. Replaces if already in

cache.

GetReturns an object from the

cache.

RemoveRemoves an object from the

cache.

Page 32: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Caching Classes

Page 33: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

DataCache with DataCacheItemVersion

• GetCacheItem: returns tags and version info

• GetIfNewer: lets you use that version info!

• Put and Remove have overloads that takes

version infoversion info

– Allows for an optimistic concurrency model

– Will only succeed if version information matches

what is current for the cached item

Page 34: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

DataCache and Locking

• GetAndLock: Allows you to lock a cache item

for a specified time period, even if not present

– (Will fail if already locked)– public Object GetAndLock (string key, TimeSpan timeout, – public Object GetAndLock (string key, TimeSpan timeout,

out DataCacheLockHandle lockHandle, bool forceLock)

• PutAndUnlock: Unlock an item, with given key

and lock handle

• Unlock: Explicitly unlock, optional extend TTL

Page 35: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

DataCache and Tags/Regions

• Explicitly created regions live on a single

node…can create a hot spot for both call

volume and memory growth

• But they offer bulk retrieval and flexible tag-• But they offer bulk retrieval and flexible tag-

based retrieves

• Instead of regions: can simulate secondary

indexes with your own secondary-to-primary

mapping

Page 36: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Administrative Model

• Administration for AppFabric Caching done purely through PowerShell

• Can administrate entire Cache Cluster from wherever administrative portion of install has wherever administrative portion of install has been done – all nodes addressable from single command line location

• Use-CacheCluster points the shell at a particular cluster to administrate

• Remember: Get-CacheHelp ☺

Page 37: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

What we’ll cover

• What motivates this product/technology

• Terms / Pictures / Concepts

• Deploy / Install Process

• A lap around the API & Admin model• A lap around the API & Admin model

• Demos

• Gotchyas

Page 38: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

What we’ll cover

• What motivates this product/technology

• Terms / Pictures / Concepts

• Deploy / Install Process

• A lap around the API & Admin model• A lap around the API & Admin model

• Demos

• Gotchyas

Page 39: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Gotchyas

• Not a gotchya: AppFabric provides a SessionStoreProvider class that plugs into the ASP.NET session storage provider model

• Balance number of nodes in cluster with memory per node. – Too many nodes = cluster overhead, too much memory per node = GC

overhead

• If you don’t use Sql Config Store, you need to manually run Start-CacheHost after rebootCacheHost after reboot

• Sql Config Store requires high Sql privileges right now at point of install

• Currently service runs as network service account

• Consider what you will do when cache is down– You can go after source of truth

– How do you avoid leaving stale data in the cache?

Page 40: Everything you wanted to know about Velocity (but were afraid to … · 2010. 4. 12. · Everything you wanted to know about Velocity (but were afraid to cache) Scott Colestock

Thank you -

Questions?