Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

46
Get more than a cache back! The Microsoft Azure (Redis) Cache Maarten Balliauw @maartenballiauw

description

The Microsoft Azure Redis Cache must be the fourth or fifth generation of a hosted cache service on Azure. This time, an open-source solution has been embraced: Redis. In this session, we’ll see that it’s more than just an in-memory cache system we can use in our applications. Let’s explore what Redis is, what the different data types are and why we should care. And once we grasp how Redis stores its stuff, we’ll delve into how we can use it to its fullest extent: searching the key-value store, transactions, pub/sub support and scripting.

Transcript of Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Page 1: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Get more than a cache back!The Microsoft Azure (Redis) CacheMaarten Balliauw@maartenballiauw

Page 2: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Thank you CloudBurst sponsors!

Page 3: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014
Page 4: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Talk Like a Parrot Dayhttp://www.howpilgrim.com/how-talk-like-parrot-day.htm

September 20th

Page 5: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Who am I?

Maarten Balliauw Antwerp, Belgium Developer Advocate, JetBrains Founder, MyGet AZUG Focus on web ASP.NET MVC, Azure, SignalR, ... MVP Azure & ASPInsider

Big passion: Azure http://blog.maartenballiauw.be @maartenballiauw Shameless self promotion: Pro NuGet - http://amzn.to/pronuget2

Page 6: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Agenda

Azure Cache Redis Data types Transactions Pub/sub Scripting Sharding/partitioning

Patterns

Page 7: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Azure Cache

Page 8: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Caching on Azure

Windows Server: AppFabric (“Velocity”) (2009-2010) Azure (ASP.NET cache) “Shared Cache” (AppFabric cache, multi-tenant) Self-Hosted Dedicated Cache Managed Cache (the above, but managed by Microsoft and

with an SLA)

A bit of history...

Page 9: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Caching on Azure

1920: Cache used to be a cache 2012: Cache became the go-to datastore Twitter stores 800 tweets / timeline in Redis

New application types on Azure: PHP, Node, Java, ... Too much work to write AppFabric clients for all! http://www.redis.io/clients Way easier: just make it work on Windows and Azure

New ways to develop applications

Page 10: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Redis

Page 11: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Redis

“Redis is an open source, BSD licensed, networked, single-threaded, in-memory key-value cache and store. It is often referred to as a data structure server since

keys can contain strings, hashes, lists, sets, sorted sets, bitmaps and hyperloglogs.”

Page 12: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Redis

Key-value cache and store (value can be a couple of things) In-memory (no persistence, but you can) Single-threaded (atomic operations & transactions) Networked (it’s a server and it does master/slave) Some other stuff (scripting, pub/sub, Sentinel, snapshot, …)

Things to remember

Page 13: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

So no persistence?

Possible using a lot of I/O AOF (append-only files) RDB (Redis DB snapshots)

With or without: all your data must fit in memory

Page 14: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Redis 101demo

Page 15: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

The Little Redis Book

http://openmymind.net/redis.pdf

By Karl Seguin

Page 16: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Data types

Type Example Key Example value

String cache:/home <html><head><title>Home page</title>

Hash categories:1 Fieldnamedescriptionnumproducts

MemberBooksBooks for sale50000

Set categories:1:products 20 11 56 89 32 4

List products:20:comments [0] -> “...”[1] -> “...”[2] -> “...”

Sorted set products:bestsellers FieldEye PatchParrot

Score8463282120

+ Bitmap, Hyperloglog

Page 17: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Data typesdemo

Page 18: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Data types

Type Example Key Example value

String cache:/homeuser:nextid

<html><head><title>Home page</title>2

Hash user:1 Fieldnamehandle

MemberMaarten@maartenballiauw

Set user:1:followers 10 42 99 85

List user:1:timeline [0] -> “...”[1] -> “...”[2] -> “...”

Sorted set trending:se:tags Field#cloudburst#pirate

Score8463282120

+ Bitmap, Hyperloglog

Page 19: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Keys

Good practice: use a pattern for keys E.g. users:1:maarten

Get by id: KEYS users:1:* GET ......

Get by name: KEYS users:*:maarten

GET ......

O(N) operation – use with caution! (SCAN)

Page 20: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

But... .NET?

Lots of options! http://www.redis.io/clients NuGet: ServiceStack.Redis StackExchange.Redis RedisAspNetProviders

Page 21: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Connecting to Redis from .NETdemo

Page 22: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

But... Azure?

“It’s just Redis!” Main differences: auth mechanism and SSL support Use local for development from GitHub or NuGet

(Redis-32 or Redis-64)

Page 23: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Azure Redis Cachedemo

Page 24: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Azure Redis Cache

Redis + Auth + SSL Monitoring Alerts Awesome tool: www.redsmin.com

Page 25: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Transactions

MULTI, EXEC, DISCARD, WATCH No rollbacks (just discard queue) Failures are because of you, not Redis

Optimistic locking with WATCH Only execute transaction queue if watched keys did not

change

Page 26: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Transactionsdemo

Page 27: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Pub/Sub

(P)SUBSCRIBE, UNSUBSCRIBE, PUBLISH Subscribe to a queue SUBSCRIBE news (or PSUBSCRIBE news:*)

Send data to a queue PUBLISH news “This just in!”

(optional) Keyspace notificationshttp://www.redis.io/topics/notifications

CONFIG SET notify-keyspace-events KEA PSUBSCRIBE '__key*__:*' (or __keyspace@0__:foo)

Page 28: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Pub/Subdemo

Page 29: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Scripting

Run Lua scripts on Redishttp://www.redis.io/commands/eval

EVAL ‘return “Hello, World!”’ 0

Scripts are cached (SCRIPT FLUSH) Scripts can be used as functions Although we must pass the body all the time (or use SCRIPT LOAD +

EVALSHA)

Helper functions available! Base, table, string, math, debug, struct, cjson, cmsgpack,

redis.sha1hex

Page 30: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Scriptingdemo

Page 31: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

What if I need more memory?

Sharding! On client (consistent hashing) On server (Redis Cluster, not on Azure) Using a proxy (Twemproxy - https://

github.com/twitter/twemproxy)

var options = new ConfigurationOptions { EndPoints = { "my-server" }, Proxy = Proxy.Twemproxy };

Page 32: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Patterns

Page 33: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

When can I use Redis?

ASP.NET Output Caching (RedisAspNetProviders) ASP.NET Session State General-purpose cache Table Storage secondary index “Cache with benefits” Pub/sub Generally: when use case maps and data fits in RAM

Page 34: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

When should I avoid Redis?

More data than can fit in RAM Can use multiple, but even then: SUM(RAM) == max. data

size

Data and query model fits well in a relational model

Materialize certain queries in Redis if needed

Page 35: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Counting stuff

How would you count “likes” if you were Facebook?

Table Storage 1 entity per likeable entity + per server instance for writes, sum entities for read I/O and CPU time... SQL UPDATE ... Locking... Redis INCR post:12345 (Every once in a while, check keys to persist)

Page 36: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Counting stuff (2)

How would you count “likes” if you were Facebook? And how would you get the 5 most popular posts?

Use a sorted set Elements are scored (= # of likes) We can use ZREVRANGE to get the top X posts ZREVRANGE posts:likes 0 5 withscores

Page 37: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Get the latest 5 product reviews

No need to go to the database Use a Redis List, truncated at 5 items Need more? Query the database

Page 38: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Rate limiting

API should only allows 5 requests per 60 seconds Redis List

Record 5 latest requests If we’re at 5, check the leftmost item versus current time

1 2 3 4 5

12:20:25 12:20:22 12:20:21 12:20:18 12:19:50

Page 39: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

What’sUp?

Sending short messages between parties, how to implement? Pub/Sub: Every user gets a subscription, others can end to that

subscription Server listens on all subscriptions to archive/persist messages When user comes online, server can publish messages since

last visit from archive/a Redis list

Page 40: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Autocompletion

How to provide autocompletion over our million-records product catalog?

SQL “LIKE” Lucene.NET / ElasticSearch Redis Sorted Set

Page 41: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Intersecting collections

“Which friends do we have in common?”

Algorithm could be: Find friends that we have in common Find friends they have in common Score the # of connections

Page 42: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Sort

We have a series of bugsPriority, Status, Title, ...

A user follows a few of these bugsHow to store?How to query? (give me my top 5 bugs by priority)

Page 43: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Conclusion

Page 44: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Conclusion

Azure Cache Redis is not just a cache Data types Transactions Pub/sub Scripting Sharding/partitioning

Patterns 1 thing to remember: http://openmymind.net/redis.pdf

Page 45: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014

Thank you!http://blog.maartenballiauw.be@maartenballiauwhttp://amzn.to/pronuget2

Page 46: Get more than a cache back! The Microsoft Azure (Redis) Cache - CloudBurst 2014