redis is the answer, what's the question - tech nottingham

58
Garry Shutler | @gshutler

Upload: garry-shutler

Post on 21-Aug-2015

16 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Redis is the answer, what's the question - Tech Nottingham

Garry Shutler | @gshutler

Page 2: Redis is the answer, what's the question - Tech Nottingham

Garry Shutler | @gshutler

Page 3: Redis is the answer, what's the question - Tech Nottingham

@gshutler

What is it?

@gshutler

Page 4: Redis is the answer, what's the question - Tech Nottingham

@gshutler

What is it good for?

@gshutler

Page 5: Redis is the answer, what's the question - Tech Nottingham

The Redis protocol

Simple to implement

Fast to parse

Human readable

@gshutler

Page 6: Redis is the answer, what's the question - Tech Nottingham

@gshutler

The Redis protocol

COMMAND KEY ARGS...

@gshutler

Page 7: Redis is the answer, what's the question - Tech Nottingham

@gshutler

The Redis protocol

redis.command(key, *args)

@gshutler

Page 8: Redis is the answer, what's the question - Tech Nottingham

@gshutler

The Redis protocol

SET mykey 3

@gshutler

Page 9: Redis is the answer, what's the question - Tech Nottingham

@gshutler

The Redis protocol

redis.set("mykey", 3)

@gshutler

Page 10: Redis is the answer, what's the question - Tech Nottingham

@gshutler

The Redis protocol

SET mykey 3 EX 3600

@gshutler

Page 11: Redis is the answer, what's the question - Tech Nottingham

@gshutler

The Redis protocol

redis.set("mykey", 3, { "ex": 3600 })

@gshutler

Page 12: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Cache

@gshutler

Page 13: Redis is the answer, what's the question - Tech Nottingham

Cachefunction cached(key, fn) { var value = redis.get(key); if (!value) { value = fn(); redis.set(key, value); } return value;}

@gshutler

Page 14: Redis is the answer, what's the question - Tech Nottingham

Cachefunction expensiveThing() { return cached("expensive", function () { // do something that's expensive return expensiveValue; });}

@gshutler

Page 15: Redis is the answer, what's the question - Tech Nottingham

Cache

expensiveThing(); // missexpensiveThing(); // hitexpensiveThing(); // hit

@gshutler

Page 16: Redis is the answer, what's the question - Tech Nottingham

Cachefunction expensiveThing(param) { var key = "expensive:" + param; return cached(key, function () { sleep(param); return param; });}

@gshutler

Page 17: Redis is the answer, what's the question - Tech Nottingham

Cache

expensiveThing(1); // missexpensiveThing(1); // hitexpensiveThing(2); // missexpensiveThing(2); // hitexpensiveThing(1); // hit

@gshutler

Page 18: Redis is the answer, what's the question - Tech Nottingham

Cachefunction cached(key, expiry, fn) { var value = redis.get(key); if (value) { redis.expire(key, expiry); } else { value = fn(); redis.set(key, value, { "ex": expiry }); } return value;}

@gshutler

Page 19: Redis is the answer, what's the question - Tech Nottingham

Cachefunction expensiveThing(param) { var key = "expensive:" + param; return cached(key, 3600, function () { sleep(param); return param; });}

@gshutler

Page 20: Redis is the answer, what's the question - Tech Nottingham

or vs

Relative In 5 minutes

Absolute At 9am

@gshutler

Expiration

Page 21: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Expiration

my_report:last_hour

@gshutler

Page 22: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Expiration

my_report:2015051118

@gshutler

Page 23: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Data types

@gshutler

Page 24: Redis is the answer, what's the question - Tech Nottingham

@gshutler

String

@gshutler

Page 25: Redis is the answer, what's the question - Tech Nottingham

@gshutler

List

@gshutler

Page 26: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Set

@gshutler

Page 27: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Conditional GET

@gshutler

Page 28: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Conditional GETvar etagKey = "etag:" + url;

if (storedEtag = redis.get(etagKey)) { headers["If-None-Match"] = storedEtag;}

var response = http.get(url, headers);var etag = response.headers["ETag"];

redis.set(etagKey, etag, { "ex": 300 });

@gshutler

Page 29: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Touch values

@gshutler

Page 30: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Touch values

SET {KEY} 1 EX {TIME}

EXISTS {KEY}

@gshutler

Page 31: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Ephemeral lookups

@gshutler

Page 32: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Ephemeral lookups

Forgotten password IDs

OAuth access tokens

Voucher codes

@gshutler

Page 33: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Time-series counter

@gshutler

Page 34: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Time-series counter

INCR clicks:201505111830

@gshutler

Page 35: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Time-series counter

INCR clicks:201505111830INCR clicks:2015051118INCR clicks:20150511

@gshutler

Page 36: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Time-series counter

INCR clicks:201505111830EXPIRE clicks:201505111830 86400

INCR clicks:2015051118EXPIRE clicks:2015051118 2419200

INCR clicks:20150511

@gshutler

Page 37: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Time-series counter> MGET clicks:201505111826 clicks:201505111827 clicks:201505111828 clicks:201505111829 clicks:2015051118301)"5"2)(nil)3)"2"4)"10"5)"1"

@gshutler

Page 38: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Unique visitors

@gshutler

Page 39: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Unique visitors

SADD {KEY} {VALUE}

@gshutler

Page 40: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Unique visitors

SADD visitors:2015051118 1SADD visitors:2015051118 2SADD visitors:2015051118 2

@gshutler

Page 41: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Unique visitors

SCARD {KEY}

@gshutler

Page 42: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Unique visitors

SCARD visitors:2015051118

@gshutler

Page 43: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Queuing

@gshutler

Page 44: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Queuing

RPUSH {KEY} {VALUE}

@gshutler

Page 45: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Queuing

RPUSH queue 1RPUSH queue 2

@gshutler

Page 46: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Queuing

["1", "2"]

@gshutler

Page 47: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Queuing

LPOP {KEY}

@gshutler

Page 48: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Queuing

LPOP queue

@gshutler

Page 49: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Queuing

"1"

@gshutler

Page 50: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Queuing

["2"]

@gshutler

Page 51: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Queuing

LPUSH queue 3

@gshutler

Page 52: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Queuing

["3", "2"]

@gshutler

Page 53: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Queuing

@gshutler

RPUSH queue 4 RPUSH queue 5

queue

LPOP queue LPOP queue LPOP queue

Page 54: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Queuing

BLPOP {KEY}... {TIMEOUT}

@gshutler

Page 55: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Queuing

Encoded values

Reply queues

@gshutler

Page 56: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Have a go

@gshutler

Page 57: Redis is the answer, what's the question - Tech Nottingham

@gshutler

Have a go

Cached Fibonacci

Distributed processing

@gshutler

Page 58: Redis is the answer, what's the question - Tech Nottingham

Garry Shutler | @gshutler