the wonders of redis key value store series - day 2 - distributed... · the wonders of redis key...
TRANSCRIPT
The Wonders of Redis key-value store
Shashank Tiwariblog: shanky.org | twitter: @tshanky
Tuesday, January 25, 2011
List cmd: LINDEX
redis> lindex myList 1
"fourth_element"
redis> lrange myList 0 4
1) "seventh_element"
2) "fourth_element"
Tuesday, January 25, 2011
List cmd: LINSERT(BEFORE or AFTER)
redis> linsert myList before "fourth_element" eighth_element
(integer) 3
redis> lrange myList 0 4
1) "seventh_element"
2) "eighth_element"
3) "fourth_element"
Tuesday, January 25, 2011
List cmd: LSET(set element at index to value)
redis> lset myList 2 ninth_element
OK
redis> lrange myList 0 4
1) "seventh_element"
2) "eighth_element"
3) "ninth_element
Tuesday, January 25, 2011
List cmd: BLPOP, BRPOP
• Blocking counterparts of LPOP and RPOP
• BLPOP list1 list2 list2 <timeout>
• Blocked until lpush, rpush or timeout.
Tuesday, January 25, 2011
List cmd: BRPOPLPUSH
• Blocking version of RPOPLPUSH
• Blocked until lpush, rpush or timeout
Tuesday, January 25, 2011
Set Commands
Tuesday, January 25, 2011
Set cmd: SADD(element added only once)
• redis> sadd mySet first_element
• (integer) 1
• redis> sadd mySet first_element
• (integer) 0
Tuesday, January 25, 2011
Set cmd: SCARD(set cardinality)
redis> scard mySet
(integer) 1
Tuesday, January 25, 2011
Set cmd: SDIFF
redis> sadd set_1 a
(integer) 1
redis> sadd set_1 b
(integer) 1
redis> sadd set_2 a
(integer) 1
Tuesday, January 25, 2011
Set cmd: SDIFFSTORE
• SDIFF and store result to destination
Tuesday, January 25, 2011
Set cmd: SINTER
redis> sinter set_1 set_2
1) "a"
Tuesday, January 25, 2011
Set cmd: SINTERSTORE
• SINTER and store result to destination
Tuesday, January 25, 2011
Set cmd: SMEMBERS(list all)
redis> smembers set_3
1) "a"
Tuesday, January 25, 2011
Set cmd: SISMEMBER
redis> sismember set_3 "a"
(integer) 1
Tuesday, January 25, 2011
Set cmd: SPOP, SRANDMEMBER
redis> spop set_1
"a"
pops a random member
Tuesday, January 25, 2011
Set cmd: SREM
redis> srem set_1 "a"
(integer) 1
Tuesday, January 25, 2011
Set cmd: SMOVE(from source to destination)
redis> smembers set_1
1) "a"
2) "b"
redis> smove set_1 set_2 "a"
(integer) 1
Tuesday, January 25, 2011
Set cmd: SUNION
redis> smembers set_1
1) "b"
redis> smembers set_2
1) "a"
redis> sunion set_1 set_2
1) "a"
Tuesday, January 25, 2011
Set cmd: SUNIONSTORE
• SUNION and store result to destination
Tuesday, January 25, 2011
Sorted Set Commands
Tuesday, January 25, 2011
Sorted Set cmd:
• A set similar to that of the Set commands
• ZADD, ZCARD, ZRANGE, ZUNIONSTORE and more
• (not covered in this presentation)
Tuesday, January 25, 2011
Authentication
• Password protection
• redis.conf -- requirepass <password>
• AUTH <password>
• then commands
Tuesday, January 25, 2011
SELECT a db
redis> select 0
OK
redis> select 1
OK
new connection uses db 0
Tuesday, January 25, 2011
Redis Internal Storage
• Everything is stored as strings
• Even lists, sets, sorted sets and maps are composed of strings
Tuesday, January 25, 2011
Simple dynamic string (SDS)
• Special structure
• buff – a character array that stores the string
• len – a long type that stores the length of the buff array
• free – number of additional bytes available for use
Tuesday, January 25, 2011
Write to disk
• Everything resides in primary memory till its written to disk
• does not use mmap files
Tuesday, January 25, 2011
Why not memory mapped files?
• http://code.google.com/p/redis/wiki/VirtualMemorySpecification
• http://antirez.com/post/redis-virtual-memory-story.html
• swap pages and redis object don’t map one to one
• format in memory and disk are not same. Disk formats are compressed.
Tuesday, January 25, 2011
Master-slave
• Slave connects and sends SYNC command
• All database and commands relayed from master to slave
• For replication slave config -- slaveof <master ip> 6379
Tuesday, January 25, 2011