20

Click here to load reader

A Brief Introduction to Redis

Embed Size (px)

DESCRIPTION

This is a brief, high-level overview of Redis, a networked data structure server.

Citation preview

Page 1: A Brief Introduction to Redis

A Brief Introduction to Redis

Charles Anderson !

Salem Dynamic Languages User Group January 2014

Page 2: A Brief Introduction to Redis

Overview

• Redis is a networked “data structure server”

• Fast, powerful, simple

Page 3: A Brief Introduction to Redis

Abstract Data Types

• In the Before Time, we didn’t have classes and objects - we had abstract data types

• and, we liked it!

• Procedural interface

• First (or last) parameter is the ADT

Page 4: A Brief Introduction to Redis

Example: Stack

• push(stack, item)

• pop(stack): item

• size(stack): int

• etc.

Page 5: A Brief Introduction to Redis

Stack ADT

• We had a single, global name space. So:

• stack_push(stack, item)

• stack_pop(stack)

• stack_size(stack)

• This is what the Redis API looks like

Page 6: A Brief Introduction to Redis

Redis Types

• String - scalar value

• List

• Set

• Sorted Set

• Hash

Page 7: A Brief Introduction to Redis

Redis List

• A small subset of the operations:

• LPUSH key value

• LPOP key

• LLEN key

• LRANGE key start stop

Page 8: A Brief Introduction to Redis

Redis API

• Every object has a name/key

• First parameter to each call

• Every object has a type

• Operation must match the type of object

Page 9: A Brief Introduction to Redis

Using API via CLI$ redis-cliredis 127.0.0.1:6379> lpush mylist 1(integer) 1redis 127.0.0.1:6379> lpush mylist cat(integer) 2redis 127.0.0.1:6379> lpush mylist 3.14(integer) 3redis 127.0.0.1:6379> llen mylist(integer) 3redis 127.0.0.1:6379> lrange mylist 0 11) "3.14"2) "cat"redis 127.0.0.1:6379> lrange mylist 0 -11) "3.14"2) "cat"3) "1"

Page 10: A Brief Introduction to Redis

Redis Clients

• The wire protocol for Redis is dead-simple

• Therefore, lots of clients for lots of languages

Page 11: A Brief Introduction to Redis

Ruby Client Demo

irb(main):015:0> r.lpush('mylist', 1)=> 1irb(main):016:0> r.lpush('mylist', 'cat')=> 2irb(main):017:0> r.lpush('mylist', 3.14)=> 3irb(main):019:0> r.lrange('mylist', 0, -1)=> ["3.14", "cat", "1"]

Page 12: A Brief Introduction to Redis

Complete Redis API

• http://redis.io/commands

• Each command includes the time complexity of the operation - unique

Page 13: A Brief Introduction to Redis

Set

• SADD key member, SREM key member

• SCARD key

• SISMEMEBER key member

• SINTER, SUNION

Page 14: A Brief Introduction to Redis

Sorted Set

• ZADD key score member

• ZSCORE key member

• ZRANK key member

• ZRANGE key start stop

• Use case: leader board

Page 15: A Brief Introduction to Redis

Hash

• HSET key field value

• HGET key field

• HEXISTS key field, HKEYS, HLEN

• Use case: a structured record like User

Page 16: A Brief Introduction to Redis

Other Stuff

• Key Expiration

• Transactions/batches

• Publish-Subscribe

• Server-side scripting via Lua

• Replication

• Clustering

Page 17: A Brief Introduction to Redis

Implementation Details

• Everything is stored in memory

• Hella fast

• Not memcache - expiration is not LRU

• Persistent - tunable disk storage

• Single-threaded - simple, fast, single CPU

• Support (@antirez) is awesome

Page 18: A Brief Introduction to Redis

But, is it Web Scale?

• NoSQL key/value store

• Kickass benchmarks

• Simpler (smaller scope) than HBase or Cassandra

• Sharding done by clients

• Not usually a system of record

• I already work on a farm

Page 19: A Brief Introduction to Redis

More Information

• http://redis.io

• “The Little Redis Book” Seguin

• “Redis in Action” Carlson

• “Redis Cookbook” Macedo and Oliveira

Page 20: A Brief Introduction to Redis

Questions

• ?