redis and its many use cases

Post on 28-Nov-2014

8.029 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Talk given at NodeMTL #5

TRANSCRIPT

Redis and its many usesChristian Joudrey - @cjoudrey

Redis is an open source, advanced key-value store.

‘‘

’’

Key-value what?!

SET name "fido"

GET name

=> "fido"

EXPIRE name 120

So it's like memcached?

Atomic operations

SET count 0

INCR count

=> 1

Ordered lists

LPUSH friends "Tom"

LPUSH friends "Sam"

LLEN friends

=> 2

LPUSH

TomSam

RPUSH

LRANGE friends 0 0

=> ["Sam"]

LRANGE friends 0 -1

=> ["Sam", "Tom"]

Sets, Hashes,Sorted Sets

Save to disk

Download ithttp://redis.io/download

... and try it ...$ redis-cliredis> SET name "Chris"OKredis> GET name"Chris"

What about node?!

GitHubhttp://git.io/redis

... or via npm ...$ npm install redis

Use case #1:

Caching

var redis = require('redis'), client = redis.createClient();

function getSomeValue (callback) { client.get('someVal', function (err, val) { if (val) return callback(null, val); // Do call directly to Db // and cache result. });};

Use case #2:

Session store

var app = require('express').createServer();var RedisStore = require('connect-redis')(express);

app.use(express.session({ secret: 'keyboard cat', store: new RedisStore}));

app.get('/', function(req, res){ res.send('hello world');});

app.listen(3000);

http://expressjs.com/guide.html#session-support

Use case #3:

Pub/Sub

ChatServer

ChatServer

LoadBalancer

Use case #4:

Job Queue

Image resize APIi.e.:

NodeServer

GET /image_resize?url=http://...

1) Download image2) Resize image with GD

NodeServer

GET /image_resize?url=http://...

JobQueue

orkerResize

Workers

1) Download image2) Resize image with GD

Kuehttps://github.com/learnboost/kue

There's a lot more!11 Common Web Use Cases Solved in Redis

http://bit.ly/pgDmXn

Questions? :)

top related