CaaS and Redis Cachingx

Download Report

Transcript CaaS and Redis Cachingx

CaaS and Redis Caching
Practical Data Driven Configuration Services with Redis Cache Examples
Copyright © 2015 Monza Cloud, LLC
Agenda
• Who we are, Monza Cloud, LLC.
• How we use Caas (Configuration as a Service) with database driven configuration
repositories
• Quick introduction to AzStudio, our cloud APIs and Tools
• Demonstrate the basic steps to work with storage using MSDE code
• Practical Demonstrations :
•
•
•
•
CaaS at work – Shortcuts and environments explained
Blob storage creation, routing, and testing
Queue creation, routing and testing. Demo BigItem
Table Storage creation, routing and testing. Demo BigItem
• Closing statement
• Q&A, reference to resources
Monza Cloud, LLC
• Founded in 2015
• Team worked for several years on various Azure projects before forming
company
• We specialize in rapid development of SaaS offerings that take
advantage of our Azure framework
• We’re not a big “move this VM” or “Modify this existing app” team, we’re
here to write your apps in the cloud for the cloud
• Extensive experience deploying “internet of things” solutions
CaaS
• CaaS is the first portion of our configuration system
• CaaS can be considered the “bootstrap” that our deployments look to
• CaaS then points the service to a database, where deep configuration
information is maintained
• Configuration manager then brokers the config information to the rest
of the code
CaaS Advantages
• No more config files. No need for specialized tools to manage these
files
• Database driven config means referential integrity checks and data
enforcement, easy to back up and restore
• Configuration data can be segmented and management of specific
portions of configuration can be distributed to different personal
• Configuration changes can be picked up on-the-fly (runtime) by your
services without recycling or redeploying the service
• Can be made exceptionally redundant, can gracefully handle multiple
layers of failure without redeployment
CaaS - Bootstrap
List of
Master
endpoints
No more
endpoints!
Service Fails
Service Starts
Pop next
endpoint
Missing lookup
or service
request fails
Call endpoint,
asking for
configuration
data
Configuration
service uses
reverse DNS /
keys to lookup
config
Service sets
environment
values and
config database
info (singleton)
Return
ENVIRONMENT
settings and
DATABASE
connection
Service Runs
Service – Post Bootstrap
• CaaS response has now set the global property to an environment id.
This drives ALL configuration lookup operations
• Application now consumes services / functionality as necessary.
• When a connection is needed, the internal manager uses the CaaS
environment Id and the config database to lookup connection string(s)
or setting
• Programmers ignore all of this, they use shortcuts
Caas – Shortcuts and Environments
• CaaS has assigned your service an environment Id (Dev, QA,
Production, FangBook, etc..)
• Programmers don’t really care about environment. They care about the
resource shortcut name
• We configure shortcuts, then map them in different environments
• The config database the service is connected to holds routing
information
• Config databases can be segmented and partitioned
AzStudio : Quick Overview
• For today’s demos we’re using our Azure toolset, AzStudio
• AzStudio API’s == JQuery or Angular in the HTML world
• AzStudio == Visual toolset used by programmers and DevOps
• If you’re developing in Azure you WILL develop something like this in
your environment. Either using a collection of tools / scripts or
something consolidated like our product
• You will use these patterns and codify them in your organization in
order to survive the rapid decomposition of resources in a SaaS / Paas
Environment
When you Need Caching
• When multiple roles need to store small pieces of persistent data that
they quickly want to access
• Relatively high transaction volume
• You want to reduce load on other storage technologies (SQL, Table
Storage, Blob, etc..)
• Data is non-persistent over long periods of time
• Example: Session or extended session information
Caching Technologies Available in Azure
Local Cache
In-Role Cache
Instance Only
•
•
•
•
No Lifetime Management
Concurrency considerations
Lightweight
Fastest
Role Shared
•
•
•
•
No Lifetime Management
Difficult to configure and
manage
Inconsistent operation
Depreciated
•
•
•
Managed Cache Service
Redis
Data Center
Data Center
Lifetime Management
Inconsistent operation
Depreciated. You cannot
create this service going
forward.
•
•
•
Lifetime Management
Microsoft Recommended
Stable
Redis Caching: What is it?
• Redis cache is a separate server you use to store values
• A key is used to reference your value
• Values can be manipulated as strings or as array of bytes
• Values have a lifespan, usually
• The server is optimized for high-speed/ volume transactions
Caching: Have a Backup Strategy
Table Storage
Blob Storage
Database Cache
Re-Hydrate
Use table storage for fast
cache backup.
We like this solution better
than SQL.
Not as flexible.
If you use a consistent keying
strategy then store the JSON
as a blob.
Use a simple SQL table to
store a copy of your JSON
data.
Slowest but necessary. Your
code must know how to rehydrate its state from original
data.
Set up Redis Caching in Azure
Set up Redis Caching in Azure
Watch Expenses!
Examine each offering, as it’s easy to select a caching size
that’s much more than you need. We find the B1 service
works pretty well for most businesses.
Also keep in mind you need THREE of these caches (Dev,
QA and Production) but your Dev and QA caches should be
B0 tier in almost all cases.
Scale up your cache before you federate. Carefully consider
if you need replication or an SLA, as those cost more.
Using the failsafe methods mentioned earlier are often
more cost effective.
Set up Redis Caching in Azure
Set your Resource Group and Location
This let’s Azure know where you want the physical instance located.
Select Create
A tile will appear on your portal that looks like this:
Wait for Deployment
The portal will inform you when your cache is ready. This takes a bit of
time.
Set up Redis Caching in Azure
Configuration Strategy
You’ll want to have your configuration strategy planned out at this point. For this demonstration, we’re going to
use our CaaS strategy. There are many online tutorials on how to use configuration files and slightly different C#
to accomplish the same tasks.
Information you Need
Note the Hostname and access key. We find using secondary key to be the best practice.
Configuration String
Configuration String format:
[hostname],ssl=true,password=[accesskey]
Demo’s connection string would look like this:
presentation.redis.cache.windows.net,ssl=true,password=Mn4n/YsspnuKBSpK4VG/DXAa3TOuLlVP1mcmiADzN3B=
Add Caching to Your Project
Install Json.NET if you plan to serialize objects (we do) and StackExchange.Redis, the Redis cache C# library.
Create a Caching Class
Create a Project and Class
In your C# code, add the StackExchange.Redis and Newtonsfot.Json references.
How AzStudio Does Caching
Static Class
We use a static class that manages different cache instances. The cache instances each maintain a persistent cache
connection and manage writing and reading from the Redis Instance.
The ConnectionMultiplexer is the key class. Connect uses your connection string and produces a connection
object. You should persist the connection object as it is meant to have a long life-cycle (it’s a heavy object to
create).
Create a Set Method
Now we have a connection. We can leverage the connection to write our own Get and Set methods. We like to use JSON encoders to serialize
our object in and out of the cache. This seems to work for us fairly well. You can also serialize into a byte stream
Create a Get Method
Get is pretty straightforward. We attempt to read the cache. If we find the value in the cache we de-serialize it
into <T>. If the key is null or missing…or we encounter an exception de-serializing then it returns default(T).
Redis Cache Quick Facts
Maximum Key Size
You’ll want to keep your key size as small as possible. The Key and Value size count together A hard limit of 512
MB on key size.
Maximum Value Size
Max value is 512 MB for a serialized string. Keep in mind the key size counts towards this limit. But really, what are
you doing caching a 512MB string?!
Concurrent Connections?
Depends on cache size. Keep in mind that a 250MB cache runs on a A-0 size machine. The larger the cache size
the larger the dedicated box to host the cache will be, so load test your app. The absolute limit is 10K.
Note: We found that a 1GB cache size was sufficient to handle most of our smaller to mid-sized client’s modest
caching needs.
Lifespan
Set the MAXMEM policy in your cache settings, all settings. The default is volatile LFU. That is old values will be
deleted after several minutes or if the cache needs to make room, the Least Frequently Used item will be deleted.
Live Demo
Now it’s time to show you a live demo
Conclusion
• You’re cloud development and deployment can get overly complicated
with configuration files. Find a strategy to overcome this early in your
project
• Caching is easy. Planning caching configuration is less so. Embedding
your caching strategies behind a single CACHE class will make your
coding experience far less painful
• Challenge: In the next 24 hours and set up a basic cache test harness
and use it. Load test it, play with different access strategies and imagine
how you’d tackle federation and failsafe patterns using this technology
• Now we’ll open up to Q&A