8. What is redis ?
REmote DIctionary Server
Open-source, in-memory, persistable
key-value advanced datastore
8
9. It’s an Open source project
✘Created in 2009
✘ by @antirez
(Salvatore Sanfilippo)
✘https://github.com/antirez
/redis
✘http://redis.io/
9
10. The redis manifesto
We’re against complexity
the best way to fight complexity
Is, by not creating it at all.
10
11. is used in
Computer
Science to
describe the
performance of
an algorithm
Big O notation O(n)
describes the
worst case
scenario, used to
describe the
execution time
required 11
15. What is a key-value store
CREATE TABLE redis (
k VARCHAR(512MB) NOT NULL,
v VARCHAR(512MB),
PRIMARY KEY (k)
);
15
16. Data Structure: strings
used to store 3 types of values:
■ Byte string values
■ Integer values
■ Floating-point values
16
17. SET & GET
SET tag ’#jab16’
GET tag
String operations
INSERT & SELECT
INSERT INTO redis
(k,v) VALUES
(‘tag’,’#jab16’)
SELECT v FROM redis
WHERE k=‘tag’
17
29. Data structure:Lists
✘Is a sequence of ordered strings
✘Up to 2^32 (4 billion) of elements
✘Duplicate possible
✘O(N) - more fast (head/tail)
✘LPUSH mylist “A”
✘RPUSH mylist “Z”
✘RPOP mylist
✘LPOP mylist
✘LINDEX mylist 3
✘LRANGE mylist 4 10
✘LTRIM mylist 0 4
29
30. LISTS: USE CASE
✘Social network Timeline
✘last user activities, Top n viewed
✘Queques / Stack (LIFO-FIFO)
✘Producer – Consumer
✘Asynchronous job
✘Capped list
30
31. Data structure:Sets
✘Sets are an unordered collection of
String, similar to Lists but no duplicates
allowed
✘Up to 2^32 (4 billion) of elements
✘SADD myset “G” “A” “N”
✘SCARD myset
✘SISMEMBER myset “C”
✘SREM myset “Z”
✘SUNION
✘SINTER (Algebra of sets)
✘SDIFF
myset
A
B
Z
D
G
N
J
B
Z
31
32. Sets use case:Relationships
SADD user:1:friends 3 4 5 /* add some friends of user:1 */
SCARD user:1:friends /* How many friends does user:1 have? */
SINTER user:1:friends user:2:friends /* Common friends */
SDIFF user:1:friends user:2:friends /* Exclusive friends */
SUNION user:1:friends user:2:friends /* All the friends */
32
33. Data structure: Sorted Sets
✘Like sets, sorted sets are composed of unique, string elements
✘every element in a ZSET have a floating point value called the score
✘elements in a sorted sets are taken in order
✘Intersection with ZINTERSTORE union ZUNIONSTORE
✘Uselful l to manage Leaderboard / scoreboard
33
35. ABOUT REDIS KEYS
✘ Very long keys are not a good idea.
✘ Very short keys are often not a good idea.
✘ The maximum allowed key size is 512 MB.
✘ As a convention colon is used (user:1234:followers)
✘ Schema is your keys names so keep them in order.
35
36. Key-point
✘ Limitation physical memory
✘ Survive crash disk persitence
✘ 160 commands
✘ Single threaded
✘ Replication master-slave
✘ Multilanguage support
36