際際滷

際際滷Share a Scribd company logo
Redis by Svilen Ivanov
舒从于仂 舒于舒 仍亠仆仂
 Redis?
弌于亳仍亠仆 于舒仆仂于
VarnaConf
20 ミ姉 2013
舒 仄亠仆
2 / 178
 弍亳舒 亟舒 仗仂亞舒仄亳舒
 仆舒亠 丕仂亟, 从亠仍,
仆亠仆亠...
 ... 仆仂 仗亠亟仗仂亳舒 Java,
Perl, JavaScript, Ruby...
 仆舒亠 舒亰仍亳从舒舒 仄亠亢亟
Java 亳 JavaScript
svilen.ivanov@gmail.com
@buhtum
舒从于仂 亠 Redis?
 弌于 亰舒 舒仆亠仆亳亠 仆舒 key/value 亟舒仆仆亳
  仂仗亠舒亳于仆舒舒 仗舒仄亠
  于亰仄仂亢仆仂 亰舒 仗亠亳仂亟亳仆仂 亳仆仂仆亳亰亳舒仆亠 
亟亳从舒*
 弌仂亶仆仂亳亠, 仂于亠仆 string, 仄仂亞舒 亟舒 弍亟舒:
 List (仗亳从)
 Hash (亠 舒弍仍亳舒)
 Set (仄仆仂亢亠于仂 弍亠亰 仗仂于仂亠仆亳e)
 Sorted Set (仗仂亟亠亟亠仆仂 仄仆仂亢亠于仂 弍亠亰 仗仂于仂亠仆亳亠)
 亰仗仍仆亠仆亳亠 仆舒 从仂仄舒仆亟亳 于 舒仆亰舒从亳
3 / 178
亳亟仂于亠 仂亶仆仂亳
4 / 178
仆仂于仆亳 从仂仄舒仆亟亳 (1)
 SET/GET 亳仍亳 MSET/MGET
 INCR/INCRBY, DECR/DECRBY
 亳仍仂亢亠仆亳亠: 弍仂亳
 GETSET  亰舒仗亳于舒 仆仂于舒 仂亶仆仂, 于舒
舒舒舒
 亳仍仂亢亠仆亳亠: 仆仍亳舒仆亠 仆舒 弍仂
 SETNX  亰舒仗亳于舒 仂亶仆仂, 舒从仂 仆礆舒 舒从于 从仍ム
 亳仍仂亢亠仆亳亠: 亰舒从仍ム于舒仆亠
 SETBIT, BITCOUNT
 亳仍仂亢亠仆亳亠: 舒亳亳从亳 于 亠舒仍仆仂 于亠仄亠
亳仍仂亢亠仆亳e: 亰舒从仍ム于舒仆亠
 弌仆亳仂仆亳亰舒亳 仄亠亢亟 舒亰仗亠亟亠仍亠仆亳 仗仂亠亳
 仂仍舒于舒仆亠:
 SETNX lock.foo
<current Unix time + lock timeout + 1>
 仂于亠从舒:
 GET lock.foo
+ 仗仂于亠从舒 亟舒仍亳 仆亠 亠 亳亰亠从仍舒
 于仂弍仂亢亟舒于舒仆亠
 GETSET lock.foo
<current Unix timestamp + lock timeout + 1>
+ 仗仂于亠从舒 亟舒仍亳 仆亠 亠 亳亰亠从仍舒
 DEL
仍ム仂于亠 亳 仂亶仆仂亳
 仍ム仂于亠
 仆礆舒 从仂仆从亠仆亳 亳亰亳从于舒仆亳
 仗亳仄亠: 亳仗 仆舒 仂弍亠从舒, 亳亟亠仆亳亳从舒仂 亳/亳仍亳 亟亞舒
亳仆仂仄舒亳, 仗仂 从仂仂 亠 仆舒仍舒亞舒 亟舒 亠 亳
 user:123, logins:2013-03-31,
user:123:followers, etc.
 弌仂亶仆仂亳
 仂弍亳从仆仂于亠仆亳 仆亳亰仂于亠 (从仂仆于亠亳舒仆亳 亟仂 弍舒亶仂于亠)
 亠亳舒仍亳亰亳舒仆 仂弍亠从: JSON, Protocol Buffers...
丐亳仗: list (仗亳从)
丐亳仗: list (仗亳从)
亳仍仂亢亠仆亳亠 仆舒 仗亳亳亠
 仗舒从舒 亰舒 舒亳仆仂仆仆仂 亳亰仗仍仆亠仆亳亠 仆舒
亰舒亟舒亳 (job queue)
 RPUSH, LPOP
 BRPUSH, BLPOP: 弍仍仂从亳舒亳 舒仆舒仍仂亰亳
 亠舒仍亳亰舒亳 仆舒 timeline
 LPUSH 亰舒 亟仂弍舒于礌亠
 LRANGE 亰舒 亳亰于仍亳舒仆亠 仆舒 仗仂仍亠亟仆亳亠 N 仗仂舒
 Recently viewed
 LPUSH
 LTRIM n  亰舒 仗亠仄舒于舒仆亠 仆舒 亳亰仍亳仆亳亠 亟仆亳
丐亳仗: set (仄仆仂亢亠于仂)
丐亳仗: set (仄仆仂亢亠于仂)
 仗亠舒亳亳 于 仄仆仂亢亠于舒舒
 SINTER  亠亠仆亳亠
 SINTERSTORE
 SUNION  仂弍亠亟亳仆亠仆亳亠
 SUNIONSTORE
 SDIFF  舒亰仍亳从舒
 SDIFFSTORE
亳仍仂亢亠仆亳亠 仆舒 仄仆仂亢亠于舒舒
 弌亰亟舒于舒仆亠 仆舒 于亰从亳 仄亠亢亟 仂弍亠从亳
(仗亳仄亠仆仂 followers, followees)
 SADD, SREM  ID 仆舒 仗仂亠弍亳亠仍
 Tagging
 仂 亠亟仆仂 仄仆仂亢亠于仂 亰舒 于亠从亳 tag: tag:linux,
tag:programming, tag:ruby
 磻仂 仄仆仂亢亠于仂 舒仆磦舒 ID 仆舒 舒亳
 SINTER tag:linux, tag:programming
丐亳仗: 仗仂亟亠亟亠仆亳 仄仆仂亢亠于舒
(sorted sets)
 弌仂 从舒仂 Set, 仆仂 从仄 于亠从亳 亠仍亠仄亠仆 亠
仗亳于仂磦舒 亠亞仍仂, 仗仂 从仂亠仂 亠 仂亳舒
 亳仍仂亢亠仆亳亠: 从仍舒舒亳亳
Stefan1
Peter3
Svilen4
ZADD leaderboard 2 Ivan
ZRANGE leaderboard 0 -1
1) "Stefan"
2) "Ivan"
3) "Peter"
4) "Svilen"
Ivan2
Stefan1
Peter3
Svilen4
丐亳仗: 仗仂亟亠亟亠仆亳 仄仆仂亢亠于舒
(sorted sets)
 ZRANK, ZREVRANK  仆舒 从仂亠 仄仂 亠
仆舒仄亳舒 于 从仍舒舒亳舒
 ZREMRANGEBY* - 仗亠仄舒于舒 亠仍亠仄亠仆 仂
从仍舒舒亳舒
 RANK
 SCORE
 ZINCRBY  于亠仍亳舒于舒 亠亞仍仂仂 仆舒 亟舒亟亠仆
亠仍亠仄亠仆
丐亳仗: 亠 舒弍仍亳舒 (hash)
亟仄亳仆亳亳舒仆亠
亟仄亳仆亳亳舒仆亠 (1)
 舒亟亠亢亟仆仂 舒仆亠仆亳亠 仆舒 亟舒仆仆亳亠
 仗仍仆仂 亳亰仂弍舒亢亠仆亳亠 仆舒 仗舒仄亠舒 (snapshoting)
 于磻舒 仗仂仄礌舒, 于 append-only 舒亶仍
 从仂仄弍亳仆亳舒仆仂
 亠仗仍亳从舒亳 仂 亳仗 master-slave
 亰仗仂仍亰于舒 亠 亰舒 仄舒舒弍亳亠仄仂 (scalability)
 于仂弍仂亢亟舒于舒仆亠 仆舒 master 于舒 仂
亰舒仗亳于舒仆亠 仆舒 亳仆仂仄舒亳舒 仆舒 亟亳从舒
亟仄亳仆亳亳舒仆亠 (2)
 从仂 仂仗亠舒亳于仆舒舒 仗舒仄亠 于亳?
 亟仂弍舒于礌亠 仆舒 仂亠 仗舒仄亠
 sharding
 于仂仄舒亳亠仆 failover
 亳仆仄亠仆: sentinel
 仂亰于仂仍磦舒 upgrade 弍亠亰 downtime
 亠亰 亠仗仍亳从舒亳
 仂仆亳亞亳舒仆亠 弍亠亰 亠舒亳舒仆亠
亠仄仂仆舒亳
仗仂亳?

More Related Content

Redis by Svilen Ivanov

  • 2. 舒从于仂 舒于舒 仍亠仆仂 Redis? 弌于亳仍亠仆 于舒仆仂于 VarnaConf 20 ミ姉 2013
  • 3. 舒 仄亠仆 2 / 178 弍亳舒 亟舒 仗仂亞舒仄亳舒 仆舒亠 丕仂亟, 从亠仍, 仆亠仆亠... ... 仆仂 仗亠亟仗仂亳舒 Java, Perl, JavaScript, Ruby... 仆舒亠 舒亰仍亳从舒舒 仄亠亢亟 Java 亳 JavaScript svilen.ivanov@gmail.com @buhtum
  • 4. 舒从于仂 亠 Redis? 弌于 亰舒 舒仆亠仆亳亠 仆舒 key/value 亟舒仆仆亳 仂仗亠舒亳于仆舒舒 仗舒仄亠 于亰仄仂亢仆仂 亰舒 仗亠亳仂亟亳仆仂 亳仆仂仆亳亰亳舒仆亠 亟亳从舒* 弌仂亶仆仂亳亠, 仂于亠仆 string, 仄仂亞舒 亟舒 弍亟舒: List (仗亳从) Hash (亠 舒弍仍亳舒) Set (仄仆仂亢亠于仂 弍亠亰 仗仂于仂亠仆亳e) Sorted Set (仗仂亟亠亟亠仆仂 仄仆仂亢亠于仂 弍亠亰 仗仂于仂亠仆亳亠) 亰仗仍仆亠仆亳亠 仆舒 从仂仄舒仆亟亳 于 舒仆亰舒从亳 3 / 178
  • 6. 仆仂于仆亳 从仂仄舒仆亟亳 (1) SET/GET 亳仍亳 MSET/MGET INCR/INCRBY, DECR/DECRBY 亳仍仂亢亠仆亳亠: 弍仂亳 GETSET 亰舒仗亳于舒 仆仂于舒 仂亶仆仂, 于舒 舒舒舒 亳仍仂亢亠仆亳亠: 仆仍亳舒仆亠 仆舒 弍仂 SETNX 亰舒仗亳于舒 仂亶仆仂, 舒从仂 仆礆舒 舒从于 从仍ム 亳仍仂亢亠仆亳亠: 亰舒从仍ム于舒仆亠 SETBIT, BITCOUNT 亳仍仂亢亠仆亳亠: 舒亳亳从亳 于 亠舒仍仆仂 于亠仄亠
  • 7. 亳仍仂亢亠仆亳e: 亰舒从仍ム于舒仆亠 弌仆亳仂仆亳亰舒亳 仄亠亢亟 舒亰仗亠亟亠仍亠仆亳 仗仂亠亳 仂仍舒于舒仆亠: SETNX lock.foo <current Unix time + lock timeout + 1> 仂于亠从舒: GET lock.foo + 仗仂于亠从舒 亟舒仍亳 仆亠 亠 亳亰亠从仍舒 于仂弍仂亢亟舒于舒仆亠 GETSET lock.foo <current Unix timestamp + lock timeout + 1> + 仗仂于亠从舒 亟舒仍亳 仆亠 亠 亳亰亠从仍舒 DEL
  • 8. 仍ム仂于亠 亳 仂亶仆仂亳 仍ム仂于亠 仆礆舒 从仂仆从亠仆亳 亳亰亳从于舒仆亳 仗亳仄亠: 亳仗 仆舒 仂弍亠从舒, 亳亟亠仆亳亳从舒仂 亳/亳仍亳 亟亞舒 亳仆仂仄舒亳, 仗仂 从仂仂 亠 仆舒仍舒亞舒 亟舒 亠 亳 user:123, logins:2013-03-31, user:123:followers, etc. 弌仂亶仆仂亳 仂弍亳从仆仂于亠仆亳 仆亳亰仂于亠 (从仂仆于亠亳舒仆亳 亟仂 弍舒亶仂于亠) 亠亳舒仍亳亰亳舒仆 仂弍亠从: JSON, Protocol Buffers...
  • 11. 亳仍仂亢亠仆亳亠 仆舒 仗亳亳亠 仗舒从舒 亰舒 舒亳仆仂仆仆仂 亳亰仗仍仆亠仆亳亠 仆舒 亰舒亟舒亳 (job queue) RPUSH, LPOP BRPUSH, BLPOP: 弍仍仂从亳舒亳 舒仆舒仍仂亰亳 亠舒仍亳亰舒亳 仆舒 timeline LPUSH 亰舒 亟仂弍舒于礌亠 LRANGE 亰舒 亳亰于仍亳舒仆亠 仆舒 仗仂仍亠亟仆亳亠 N 仗仂舒 Recently viewed LPUSH LTRIM n 亰舒 仗亠仄舒于舒仆亠 仆舒 亳亰仍亳仆亳亠 亟仆亳
  • 13. 丐亳仗: set (仄仆仂亢亠于仂) 仗亠舒亳亳 于 仄仆仂亢亠于舒舒 SINTER 亠亠仆亳亠 SINTERSTORE SUNION 仂弍亠亟亳仆亠仆亳亠 SUNIONSTORE SDIFF 舒亰仍亳从舒 SDIFFSTORE
  • 14. 亳仍仂亢亠仆亳亠 仆舒 仄仆仂亢亠于舒舒 弌亰亟舒于舒仆亠 仆舒 于亰从亳 仄亠亢亟 仂弍亠从亳 (仗亳仄亠仆仂 followers, followees) SADD, SREM ID 仆舒 仗仂亠弍亳亠仍 Tagging 仂 亠亟仆仂 仄仆仂亢亠于仂 亰舒 于亠从亳 tag: tag:linux, tag:programming, tag:ruby 磻仂 仄仆仂亢亠于仂 舒仆磦舒 ID 仆舒 舒亳 SINTER tag:linux, tag:programming
  • 15. 丐亳仗: 仗仂亟亠亟亠仆亳 仄仆仂亢亠于舒 (sorted sets) 弌仂 从舒仂 Set, 仆仂 从仄 于亠从亳 亠仍亠仄亠仆 亠 仗亳于仂磦舒 亠亞仍仂, 仗仂 从仂亠仂 亠 仂亳舒 亳仍仂亢亠仆亳亠: 从仍舒舒亳亳 Stefan1 Peter3 Svilen4 ZADD leaderboard 2 Ivan ZRANGE leaderboard 0 -1 1) "Stefan" 2) "Ivan" 3) "Peter" 4) "Svilen" Ivan2 Stefan1 Peter3 Svilen4
  • 16. 丐亳仗: 仗仂亟亠亟亠仆亳 仄仆仂亢亠于舒 (sorted sets) ZRANK, ZREVRANK 仆舒 从仂亠 仄仂 亠 仆舒仄亳舒 于 从仍舒舒亳舒 ZREMRANGEBY* - 仗亠仄舒于舒 亠仍亠仄亠仆 仂 从仍舒舒亳舒 RANK SCORE ZINCRBY 于亠仍亳舒于舒 亠亞仍仂仂 仆舒 亟舒亟亠仆 亠仍亠仄亠仆
  • 19. 亟仄亳仆亳亳舒仆亠 (1) 舒亟亠亢亟仆仂 舒仆亠仆亳亠 仆舒 亟舒仆仆亳亠 仗仍仆仂 亳亰仂弍舒亢亠仆亳亠 仆舒 仗舒仄亠舒 (snapshoting) 于磻舒 仗仂仄礌舒, 于 append-only 舒亶仍 从仂仄弍亳仆亳舒仆仂 亠仗仍亳从舒亳 仂 亳仗 master-slave 亰仗仂仍亰于舒 亠 亰舒 仄舒舒弍亳亠仄仂 (scalability) 于仂弍仂亢亟舒于舒仆亠 仆舒 master 于舒 仂 亰舒仗亳于舒仆亠 仆舒 亳仆仂仄舒亳舒 仆舒 亟亳从舒
  • 20. 亟仄亳仆亳亳舒仆亠 (2) 从仂 仂仗亠舒亳于仆舒舒 仗舒仄亠 于亳? 亟仂弍舒于礌亠 仆舒 仂亠 仗舒仄亠 sharding 于仂仄舒亳亠仆 failover 亳仆仄亠仆: sentinel 仂亰于仂仍磦舒 upgrade 弍亠亰 downtime 亠亰 亠仗仍亳从舒亳 仂仆亳亞亳舒仆亠 弍亠亰 亠舒亳舒仆亠