redis

15
Redis Installing Basics Data structures Pseudo Multi Key Queries References and Indexes Round Trips and Pipelining Transactions Keys Anti pattern, Use SCAN Expire, Pub-Sub Monitor, Sort Administration A few commands Big O Reference

Upload: puneet-kumar

Post on 06-May-2015

147 views

Category:

Documents


8 download

TRANSCRIPT

RedisInstalling

Basics

Data structures

Pseudo Multi Key Queries

References and Indexes

Round Trips and Pipelining

Transactions

Keys Anti pattern, Use SCAN

Expire, Pub-Sub

Monitor, Sort

Administration

A few commands

Big O

Reference

iNSTALLING

• Download redis-2.8.7.tar.gz, untar

• $ make $ make test $ cd src

• $ redis-server #server accepts connections on port6379

• $ redis-cli

• $$ info

• $ redis-benchmark

basics

• In-memory persistent key-value store 5 data structures

• $select 1 # many databases

• $ set users:leto '{"name": "leto", "planet": "dune", "likes": ["spice"]}’ # Every data struct has key eg users:leto

• $ get users:leto # Values are arbitrary byte arrays that Redis doesn’t care about

Data Structures

• Strings $strlen $getrange $append

• Hashes $hset users:goku powerlevel 9000

• Lists $ lpush newusers goku

• Sets $sadd friends:leto ghanima paul chani jessica

• Sortd Sets $ zadd friends:duncan 70 ghanima 95 paul 95 chani 75 jessica 1 vladimir

Pseudo Multi Key Queries

• $set users:9001 '{"id": 9001, "email": "[email protected]", ...}’

• $hset users:lookup:email [email protected] 9001

• $get users:9001

• $id = redis.hget('users:lookup:email' , '[email protected]' ) # ruby

• $user = redis.get("users:#{id}" ) # ruby

References and Indexes

• $sadd friends:leto ghanima paul chani jessica

• $sadd friends_of:chani leto paul

• # having to manually deal with references in Redis is unfortunate.

Round Trips and Pipelining

• #Redis also supports pipelining. Normally when a client sends a request to Redis it waits for the reply before sending the next request. With pipelining you can send a number of requests without waiting for their responses.

Transactions

• $multi

• $hincrby groups:1percent balance -9000000000

• $hincrby groups:99percent balance 9000000000

• $exec # Or use command discard

• $ redis.watch('powerlevel' ) #ruby

• $ current = redis.get('powerlevel' ) #ruby

• $ redis.multi() #ruby

• $ redis.set('powerlevel' , current + 1) #ruby

• $ redis.exec() #ruby

Keys Anti pattern, Use SCAN

• $ keys bug:1233:*

• #command takes a pattern and finds all the matching keys

• #Should never be used in production code. Linear scan through all the keys.

• Better:

• $hset bugs:1233 1 '{"id":1, "account": 1233, "subject": "..."}’

• $hset bugs:1233 2 '{"id":2, "account": 1233, "subject": "..."}’

• $ scan 0 match bugs:* count 20 $ hscan $sscan $zscan

Expire, Pub-Sub

• $expire pages:about 30

• $ ttl pages:about

• $ persist pages:about

• $ setex pages:about 30 '<h1>about us</h1>....’

• $ subscribe warnings $unsubscribe $punsubscribe

• $ publish warnings "it's over 9000!” $psubscribe warnings:*)

• $blpop $brpop

Monitor, Sort

• $ monitor

• $ config set slowlog-log-slower-than 0

• $ slowlog $ slowlog len

• $ sort users:leto:guesses

• $ sort friends:ghanima limit 0 3 desc alpha

• $ sort watch:leto by severity:* desc

• $ sort watch:leto by bug:*->priority get bug:*->details

• $ sort watch:leto by bug:*->priority get bug:*->details store watch_by_priority:leto

Administration

• $ config get *log* # http://download.redis.io/redis-stable/redis.conf

• $ requirepass $ auth password

• $ rename-command FLUSHALL 1041285018a942a4922cbf76623b741e

• $ slaveof # Replication

A few commands

• Auth, echo, ping, quit, select

• Del, dump, exists, expire, expireat, keys, migrate, move, object, persist, pexpire, pexpireat, pttl, randomkey, rename, renamenx, restore, sort, ttl, type, scan

• Append, bitcount, bitop, bitops, decr, decrby, get, getbit, getrange, getset, inc, incrby, incrbyfloat, mget, mset, msetnx, psetex, set, setbit, setex, setnx, setrange, strlen

Big O

• O(1) sismember

• O(log(N) zadd ltrim

• O(log(N)+M) zremrangebyscore

• O(N+M*log(M)) sort

• O(Nˆ2) and O(CˆN) no commands

reference

• http://github.com/karlseguin/the-little-redis-book