Вы находитесь на странице: 1из 32

The Little Redis Book ( Redis) AttributionNonCommercial 3.0 Unported. .

, , .
, , , (Karl Seguin),
.
.


- ,
. / ,
. Redis
. Redis
- mogade.com.
The Little MongoDB Book, MongoDB.
http://openmymind.net,
Twitter: @karlseguin.

, .
. .

. Twitter.
Redis
. .

:
- Dmitry Scriptin
-
- polinom


.
.
2



. ,
, ,
.
, , Redis .
? -, .
, ,
Redis. -, ,
. ? Redis
, . Redis
, . , Redis -, ,
, .
, Redis. , ,
, Redis
, , -
- . ,
. Redis .
Lucene, ,
. , Redis
.
- , Redis.
Redis
. ,
.


-: - , - ,
- . Redis , . Redis , ,
. ,
Redis .

Windows
Redis Windows,
. ,
- .
https://github.com/dmajkic/redis/downloads
( ).
3

, ,
64bit 32bit.

*nix MacOSX
*nix Mac, Redis .
, , http://redis.io/
download. 2.4.6.
:
wget http://redis.googlecode.com/files/redis-2.4.6.tar.gz
tar xzf redis-2.4.6.tar.gz
cd redis-2.4.6
make
( , Redis . ,
MacOSX Homebrew brew
install redis.) (

- . .)
,
src. , cd src.

Redis
, Redis .
Redis .
(DOS- ). . Windows,
redis-server. *nix/MacOSX ./redis-server
.
, ,
redis.conf . Redis ,
.
, Redis redis-cli (Windows)
./redis-cli (*nix/MacOSX).
6389- TCP- .
, , info .
-,
.
,
Redis.
4

Redis
, API Redis
. , ,
, . ,
,
. ,
.

1 -
Redis ? ?
? , , ,
- Redis.
Redis (
- . .) -
.
, .
Redis,
, ( ),
.
-. ,
, Redis .
, Redis ,
-. ,
, ,
, Redis.
, .
, ,
- .
. , ,
. , .
, , , .
,
? , , ,
(, , ). , ,
.
?
, ?
- , .
( : , , , - . .)
, Redis. , ,
, , , ?
exists(key)
O(1) ( ,
)?



Redis .
. -
6

.
Redis ,
0. , select.
select 1. Redis OK
- redis 127.0.0.1:6379[1]>.
, select
0.

,
, Redis , -,
, ,
. , ,
.
- , .
, , : users:leto.
leto.
,
.
- , . .
, , (
JSON, XML ). , Redis
, . ,
-. ,
(string), (integer) JSON.
. :
set users:leto "{name: leto, planet: dune, likes: [spice]}"
Redis . ,
set, . set : ,
, , . , ,
( ). ,
? , ( , ):
get users:leto
. , - get set.
, .
7


Redis, . - ,
- . , , Redis
. , ,
dune.
. ,
, Redis
. . Redis
. ,
Redis (- ). ,
, .
. ,
Redis.
- Redis .
-, .


, Redis
. , , Redis
, .
, X - ,
Y . , Redis
60 , 1000 , 15 ,
9 .
, ( ), Redis
(append mode). , ,
, (
). 60
.
. Redis . 5
- (slave) .
, Redis .
Redis:
.
, ,
. 5,5 .
, , ,
/ . (
/) ,
8

, .
- Redis,
. , ,
/, .
Redis . ,
( Redis)
.
, 5,5 2 . Redis
, , , ,
,
/ .


.
,
Redis, . ,
, Redis .
, - .
: , Redis , .
. , Redis
, .
? :
. Redis
. redis-benchmark
( , redis-server redis-cli), .
, ,
Redis. , , 5
. 150 Redis.
, ,
, .
Redis, ,
. SQL
. , Redis.
, , ,
Redis .
,
.
9


, Redis,
. , - - , , .
, ,
.
:

- , ()
- , Redis

Redis (
)
Redis ,

10

2 -
Redis. ,
, ,
.
, Redis , .
. Redis
, set?

. ,
set, (string). hset,
(hash). Redis,
.
Redis .
. ,
.
, .
, flushdb,
- !

(Strings)
Redis.
-, . (),
. , .
:
. ,
:

set users:leto "{name: leto, planet: dune, likes: [spice]}"

, Redis .
, strlen <> ,
; getrange <> <> <> ; append <>
<> (
, ). .
:

> strlen users:leto


(integer) 42
11

> getrange users:leto 27 40


"likes: [spice]"
> append users:leto " OVER 9000!!"
(integer) 54
, , , , .
( )
JSON-. - , ,
.
, Redis .

.
,
, .
append getrange
- ( / - . .)
incr, incrby, decr decrby.
/ (/ ) (
,
(scalars) ,
- . .):
> incr stats:page:about
(integer) 1
> incr stats:page:about
(integer) 2
> incrby ratings:video:12333 5
(integer) 5
> incrby ratings:video:12333 3
(integer) 8
, Redis .
users:leto (-) , (
).
setbit getbit. ,
Spool (Spool - , - . .)

?. 128
50 16 .
12

, Spool ,
, Redis ,
. ,
, : ( ) .
, ,
.

(Hashes)
- , Redis -
. .
, - (fields).
set get :

hset users:goku powerlevel 9000


hget users:goku powerlevel

,
, :

hmset users:goku race saiyan age 737


hmget users:goku race powerlevel
hgetall users:goku
hkeys users:goku
hdel users:goku age

, , . ,
,
. ,

.
, ,
, . ,
. , ,
.
, .

(Lists)
.
,
.
.
13

newusers,
:

rpush newusers goku


ltrim newusers 0 50
,
, 50 . .
ltrim O(N), N - ,
. ,
, O(1) ( N
).
,
.
10
, :
keys = redis.lrange('newusers', 0, 10)
redis.mget(*keys.map {|u| "users:#{u}"})
Ruby
, .
, .
. ,
. ,
.

(Sets)

- , . ,
.
:
sadd friends:leto ghanima paul chani jessica
sadd friends:duncan paul jessica alia
, , (O(1))
, userX userY , .
sismember friends:leto jessica
sismember friends:leto vladimir
14

, , :
sinter friends:leto friends:duncan
:
sinterstore friends:leto_duncan friends:leto friends:duncan
(tagging, - . .) ,
( , ,
).

(Sorted Sets)
.
, , ,
.

. ,
:
zadd friends:leto 100 ghanima 95 paul 95 chani 75 jessica 1 vladimir
, 90 leto?
zcount friends:leto 90 100
chani?
zrevrank friends:leto chani
zrevrank zrank, Redis
( ).

. ,
, ,
,
.

, (
),
.
15


Redis.
Redis , ,
. , ,
. ,
, Redis .
, ,
Redis .
.

16

3 -

, .
, ,
.

( O)
(
) ( ) O(N) O(1).
, ,
. Redis ,
,
. ( . Redis,
, , , ,
- . .)
Redis
. , .
.
O(1) - . ,
5 5 . ,
. sismember, ,
, O(1).
sismember - ,
, , . Redis
.
- O(log(N)) - ,
.
, ,
. zadd O(log(N)),
N - , .
- O(N).
, ltrim
. , ltrim N -
, . ltrim 1
, 10 ,
. ( , , ,
.)
zremrangebyscore,
,
17

O(log(N)+M). . ,
, N - , M -
. , , ,
, .
sort, ,
O(N+M*log(M)). , , ,
Redis.
, - O(N2)
O(CN). N, N. (
, - . .) Redis
.
, O .
, , O(N),
, ,
.

-
, ,
. ,
( ,
) ( ).
:

set users:leto@dune.gov "{id: 9001, email: 'leto@dune.gov', ...}"


set users:9001 "{id: 9001, email: 'leto@dune.gov', ...}"

, ,
.
, Redis ,
( ). Redis (API).
( , ,
) , , Redis
- .
, :

set users:9001 "{id: 9001, email: leto@dune.gov, ...}"


hset users:lookup:email leto@dune.gov 9001
18

-,
, . ,
get:

get users:9001

,
hget, get ( Ruby):

id = redis.hget('users:lookup:email', 'leto@dune.gov')
user = redis.get("users:#{id}")

, , , .
, , ,
.


, .
, , ,
, .
.
, ,
, . Redis
.
, :

sadd friends:leto ghanima paul chani jessica

,
. , chani
? , :

sadd friends_of:chani leto paul

, , ,
,
.
, -
( ).
19

, ,
. ,
.

( ,
).
Redis .

. , , .


,
Redis. ,
, .
, ,
, .
mget, :

keys = redis.lrange('newusers', 0, 10)


redis.mget(*keys.map {|u| "users:#{u}"})

sadd, :

sadd friends:vladimir piter


sadd friends:paul jessica leto "leto II" chani

Redis . ,
Redis, , .
,
.
.
Redis ,
.
, , , ,
. ,
50 , , ,
.
, ,
. Ruby pipelined:
20

redis.pipelined do
9001.times do
redis.incr('powerlevel')
end
end

,
!

Redis , , .
, Redis .
, , Redis ,
() .
, . (
.) , , .
:
incr - , , get set
getset
setnx , ,
,
, ,
. ,
multi, , ,
exec , , discard,
. Redis ?


, (
)
,

, , . ,

.
21

multi
hincrby groups:1percent balance -9000000000
hincrby groups:99percent balance 9000000000
exec

, Redis ( ) ,
, () (-). ,
.
incr,
, exec. :

redis.multi()
current = redis.get('powerlevel)
redis.set('powerlevel', current + 1)
redis.exec()

Redis . , watch powerlevel,


:

redis.watch('powerlevel')
current = redis.get('powerlevel')
redis.multi()
redis.set('powerlevel', current + 1)
redis.exec()

powerlevel , watch
, . ,
. , .

-
,
. .
, - keys.
, .
, .
?
. , .
?
, - -
( ). id_

22

bug:id_:id_. -
(
), , , ( -) keys:

keys bug:1233:*

. ,
, :

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


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


hkeys bugs:1233.
hdel bugs:1233 2, - del bugs:1233.


, ,
Redis . ,
,
,
, ,
.

23

4 -
, Redis,
, . : info,
select, flushdb, multi, exec, discard, watch keys.
.


Redis .

Unix (Unix timestamp, ,
1 1970 ) .
, ,
.

expire pages:about 30
expireat pages:about 1356933600

( ) 30 .
12:00, 31 2012 .
Redis . ,
, ttl,
persist:

ttl pages:about
persist pages:about

, setex,
(
):

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

( ,
- . .)


Redis blpop brpop, ,
, , ,
.
.
24

, Redis
( , , ..
- . .). , redis-cli
. ( warnings):

subscribe warnings

. , ,
warnings:

publish warnings "it's over 9000!"

, ,
warnings.
(subscribe channel1 channel2 channel3 ...)
, (psubscribe warnings:*),
unsubscribe punsubscribe
.
, , publish 1.
, .


monitor , Redis .
,
Redis. redis-cli ( ,
unsubscribe )
monitor. (, get set).
.
monitor ,
. ,
. .
monitor, Redis slowlog,
( - . .).
, ,
. , Redis,
, :

config set slowlog-log-slower-than 0


25

, .
, :

slowlog get
slowlog get 10

, slowlog
len
:


( Unix),

,
.
1024 .

Redis - sort.
, (
, ).
, :

rpush users:leto:guesses 5 9 10 2 4 10 19 2
sort users:leto:guesses

, .
:

sadd friends:ghanima leto paul chani jessica alia duncan


sort friends:ghanima limit 0 3 desc alpha

, ( limit),
( desc), ,
( alpha).
26

sort .
, ,
Redis. sort
. , (), .

,
:

sadd watch:leto 12339 1382 338 9338

( ),
. ,
Redis, . , ,
:

set
set
set
set

severity:12339 3
severity:1382 2
severity:338 5
severity:9338 4

,
:

sort watch:leto by severity:* desc

Redis (
) * . , Redis
.
Redis, ,
. ,
. , ,
:

hset bug:12339 severity 3


hset bug:12339 priority 1
hset bug:12339 details "{id: 12339, ....}"
hset bug:1382 severity 2
hset bug:1382 priority 2
27

hset bug:1382 details "{id: 1382, ....}"


hset bug:338 severity 5
hset bug:338 priority 3
hset bug:338 details "{id: 338, ....}"
hset bug:9338 severity 4
hset bug:9338 priority 2
hset bug:9338 details "{id: 9338, ....}"

,
severity () priority (),
sort, :

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

, Redis
->
. get,
, (details) .
sort . ,
sort :

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

( store) sort
, , .


, . ,
.
,
, ,
. , . ,
. ,
, .

28

5 -
Redis.
.
, Redis.

Redis, , redis.conf
.
Redis. redis.conf
release- Redis. ,
,
. https://github.com/antirez/redis/
raw/2.4.6/redis.conf.
Redis 2.4.6. 2.4.6
URL . ,
info.
,
.
Redis redis.conf, config set
.
,
slowlog-log-slower-than 0.
config get,
. .
, :
config get *log*

Redis , .
requirepass ( redis.conf config
set). requirepass - (
), auth password .
,
. flushall, .
:
rename-command CONFIG 5ec4db169f9d4dddacbfb0c26ea7e5ef
rename-command FLUSHALL 1041285018a942a4922cbf76623b741e
29

,
.


Redis, :
?. ,
(, )
. ,
.

Redis , , ,
Redis ( master)
( slave). slave- slaveof
(,
master-).
, .
, ..
slave-.
, .
, Redis
. master- ,
slave-. ,
master-,
.


Redis Redis
(Amazon S3, FTP, ). , Redis dump.rdb.
.

master- slave-.
master- slave-
, .

Redis Cluster
,
. , ( sort),
slave-
.
30

, - Redis
Redis ( -
, Redis ). , ,
- (
). ( - , -
- .
.) ,
. ,
, ,
.
, Redis Cluster.
,
,
.
,
. ,
Redis Cluster .


, Redis,
,
. , ,
,
. Redis Cluster, ,
.

31


Redis .
, .
Redis .
, Redis .
, : Redis
. , ,
. , Redis
, , ,
Redis.

32