REDIS 101 - DATA STRUCTURE
ISMAIL ANJRINI - REDIS CERTIFIED DEVELOPER
08:00
PM
Wednesday 27 - Jan -
2021
ZOOM
‫نعم‬
58
%
‫ال‬
42
%
‫استخدمت‬ ‫هل‬
Redis
‫مشروع؟‬ ‫في‬
‫نعم‬ ‫ال‬
95 of 296
KEYS
OVERVIEW
 Very long keys are not a good idea
 Use pattern for key names
 user:2435:settings
 The maximum allowed key size is 512 MB
KEYS PATTERN
 O(N) with N being the number of keys in the database
 Returns all keys matching pattern
 Redis running on an entry level laptop can scan a 1 million key database in 40 milliseconds
 Should only be used in production environments with extreme care
SCAN CURSOR [MATCH PATTERN] [COUNT COUNT] [TYPE TYPE]
 O(1) for every call.
 O(N) for a complete iteration
 N is the number of elements inside the collection
 SCAN is a cursor based iterator
 Returns only a small number of elements per call
 An iteration starts when the cursor is set to 0
 Terminates when the cursor returned by the server is 0
DEL - UNLINK
 DEL - O(N)
 UNLINK - O(1)
EXISTS KEY [KEY ...]
 O(1)
 EXISTS k1
 EXISTS k1 k2
TYPE KEY
 O(1)
 SET name ismail
 TYPE name -> string
FLUSHDB [ASYNC]
 O(N)
 Delete all the keys of the currently selected DB
DATABASE
S
Redis Cluster only supports database
zero
0 ->
15
Different databases can have keys with the same
name
SELECT
redis-cli -n
14
DATA TYPES
OVERVIEW
 Binary-safe strings.
 Lists
 Sets
 Sorted sets
 Hashes
 Bit arrays (or simply bitmaps)
 HyperLogLogs
 Streams
STRINGS
The Redis String type is the simplest type of value you can associate with a
Redis key
SET and the GET commands are the way we set and retrieve a string
value
A key value can't be bigger than 512
MB
GET “”
SET “” ismail
DEMO
Encoding is checked before command
execution
LIST
The speed of adding a new element to the head of a list with ten elements is the
same as adding an element to the head of list with 10 million elements
A List is just a sequence of
elements
value1 value2 value3 value4 value5
LPUS
H
RPOP
value1 value2 value3 value4 value5
LPUS
H
BRPOP
value1 value2
LPUS
H
value2
LMOVE
Duplicates are
allowed
Used to implement Stacks and
Queues
E1
E2 E1
E3 E2 E1
E4 E3 E2 E1
LPUS
H
RPOP
E4 E3 E2
E4 E3
E4
E1
E2 E1
E3 E2 E1
E4 E3 E2 E1
LPUS
H
LPOP
E3 E2 E1
E2 E1
E1
HASHES
Redis Hashes are maps between string fields and string
values
It is the perfect data type to represent
objects
A hash with a few fields is stored in a way that takes very little
space
id name age status level city
1981 Ismail 33 active 1 Riyadh
1985 Fadi 30 active 1 Aleppo
HSET user:1981 name Ismail age 33 status active level 1 city
Riyadh
HSET user:1985 name Fadi age 30 status active level 1 city
Aleppo
SETS
Sets are unordered collections of
strings
Intersection, union or difference between multiple
sets
Redis returns the elements in any order at every call from the same
set
Duplicates are NOT
allowed
SADD
SPOP
SMOV
E
SRANDMEMBER
SSCAN
SORTED SETS
Is similar to a mix between a Set and a
Hash
Every element in a sorted set is associated with a floating point value, called
the score
If A and B are two elements with a different score, then A > B if A.score is >
B.score
If A and B have exactly the same score, then A > B if the A string is lexicographically greater than the
B string
Duplicates are NOT
allowed
score name
99 ismail
101 riyadh
67 fadi
80 kasem
0 0 0 0 0 0 0 0 0 0
Ismail Ahma
d
Fadi Riyad
h
Kasem Ave Sami John Emmy So
1)
"Ahmad"
2) "Ave"
3) "Emmy"
4) "Fadi"
5) "Ismail"
6) "John"
7) "Kasem"
8)
"Riyadh"
9) "Sami"
10) "So"
ZRANGEBYSCORE employees 0
+inf
ZRANGEBYLEX employees [A
[John
1) "Ahmad"
2) "Ave"
3) "Emmy"
4) "Fadi"
5) "Ismail"
6) "John"
ZRANGEBYLEX employees [A "[Axff"
1) "Ahmad"
2) "Ave"
BIT DATA
setbit page:about:2021:01 2
1
0010000
0
setbit page:about:2021:01 9
1
00100000
01000000
strlen page:about:2021:01 2
getbit page:about:2021:01 9 1
getbit page:about:2021:01
10
0
bitcount
page:about:2021:01
2
get
page:about:2021:01
" @"
BITOP AND
BITOP OR
BITOP XOR
BITOP NOT
BITFIELD i: Signed u:
Unsigned
0 3
u4
unsigne
d
4
bits
offset bitfield test set u4 0
2
0 0 1 0
bitfield test get u4 0
2
bitfield test get u4 2
8
1 0 0 0
0 0 0 0 0 0 0 0 0 0
setbit visits:2020:12:05 9
1
0 0 0 0 0 0 0 0 0 1
setbit visits:2020:12:06 9
1
0 0 0 0 0 0 1 1 0 0
setbit visits:2020:12:10 9
1
1 0 1 0 0 1 1 0 1 1
BITOP OR visits:2020:12 visits:2020:12:05 visits:2020:12:06
visits:2020:12:10
1 0 1 0 0 1 1 1 0 1
BITOP AND visits:2020:12 visits:2020:12:05 visits:2020:12:06
visits:2020:12:10
0 0 0 0 0 0 0 0 0 0
0 1 2
0
1
2
offset = Y * MAX_WIDTH + X
QUESTIONS
‫في‬ ‫الصور‬ ‫حفظ‬ ‫الجيد‬ ‫من‬ ‫هل‬
Redis
‫؟‬
‫استخدام‬ ‫يمكن‬ ‫أين‬
Redis
‫؟‬
THANKS

Redis 101 Data Structure

  • 1.
    REDIS 101 -DATA STRUCTURE ISMAIL ANJRINI - REDIS CERTIFIED DEVELOPER 08:00 PM Wednesday 27 - Jan - 2021 ZOOM
  • 2.
  • 3.
  • 4.
    OVERVIEW  Very longkeys are not a good idea  Use pattern for key names  user:2435:settings  The maximum allowed key size is 512 MB
  • 5.
    KEYS PATTERN  O(N)with N being the number of keys in the database  Returns all keys matching pattern  Redis running on an entry level laptop can scan a 1 million key database in 40 milliseconds  Should only be used in production environments with extreme care
  • 6.
    SCAN CURSOR [MATCHPATTERN] [COUNT COUNT] [TYPE TYPE]  O(1) for every call.  O(N) for a complete iteration  N is the number of elements inside the collection  SCAN is a cursor based iterator  Returns only a small number of elements per call  An iteration starts when the cursor is set to 0  Terminates when the cursor returned by the server is 0
  • 7.
    DEL - UNLINK DEL - O(N)  UNLINK - O(1)
  • 8.
    EXISTS KEY [KEY...]  O(1)  EXISTS k1  EXISTS k1 k2
  • 9.
    TYPE KEY  O(1) SET name ismail  TYPE name -> string
  • 10.
    FLUSHDB [ASYNC]  O(N) Delete all the keys of the currently selected DB
  • 11.
  • 12.
    Redis Cluster onlysupports database zero 0 -> 15 Different databases can have keys with the same name SELECT redis-cli -n 14
  • 13.
  • 14.
    OVERVIEW  Binary-safe strings. Lists  Sets  Sorted sets  Hashes  Bit arrays (or simply bitmaps)  HyperLogLogs  Streams
  • 15.
  • 16.
    The Redis Stringtype is the simplest type of value you can associate with a Redis key SET and the GET commands are the way we set and retrieve a string value A key value can't be bigger than 512 MB GET “” SET “” ismail DEMO Encoding is checked before command execution
  • 17.
  • 18.
    The speed ofadding a new element to the head of a list with ten elements is the same as adding an element to the head of list with 10 million elements A List is just a sequence of elements value1 value2 value3 value4 value5 LPUS H RPOP value1 value2 value3 value4 value5 LPUS H BRPOP value1 value2 LPUS H value2 LMOVE Duplicates are allowed Used to implement Stacks and Queues
  • 19.
    E1 E2 E1 E3 E2E1 E4 E3 E2 E1 LPUS H RPOP E4 E3 E2 E4 E3 E4 E1 E2 E1 E3 E2 E1 E4 E3 E2 E1 LPUS H LPOP E3 E2 E1 E2 E1 E1
  • 20.
  • 21.
    Redis Hashes aremaps between string fields and string values It is the perfect data type to represent objects A hash with a few fields is stored in a way that takes very little space id name age status level city 1981 Ismail 33 active 1 Riyadh 1985 Fadi 30 active 1 Aleppo HSET user:1981 name Ismail age 33 status active level 1 city Riyadh HSET user:1985 name Fadi age 30 status active level 1 city Aleppo
  • 22.
  • 23.
    Sets are unorderedcollections of strings Intersection, union or difference between multiple sets Redis returns the elements in any order at every call from the same set Duplicates are NOT allowed SADD SPOP SMOV E SRANDMEMBER SSCAN
  • 24.
  • 25.
    Is similar toa mix between a Set and a Hash Every element in a sorted set is associated with a floating point value, called the score If A and B are two elements with a different score, then A > B if A.score is > B.score If A and B have exactly the same score, then A > B if the A string is lexicographically greater than the B string Duplicates are NOT allowed score name 99 ismail 101 riyadh 67 fadi 80 kasem
  • 26.
    0 0 00 0 0 0 0 0 0 Ismail Ahma d Fadi Riyad h Kasem Ave Sami John Emmy So 1) "Ahmad" 2) "Ave" 3) "Emmy" 4) "Fadi" 5) "Ismail" 6) "John" 7) "Kasem" 8) "Riyadh" 9) "Sami" 10) "So" ZRANGEBYSCORE employees 0 +inf ZRANGEBYLEX employees [A [John 1) "Ahmad" 2) "Ave" 3) "Emmy" 4) "Fadi" 5) "Ismail" 6) "John" ZRANGEBYLEX employees [A "[Axff" 1) "Ahmad" 2) "Ave"
  • 27.
  • 28.
    setbit page:about:2021:01 2 1 0010000 0 setbitpage:about:2021:01 9 1 00100000 01000000 strlen page:about:2021:01 2 getbit page:about:2021:01 9 1 getbit page:about:2021:01 10 0 bitcount page:about:2021:01 2 get page:about:2021:01 " @" BITOP AND BITOP OR BITOP XOR BITOP NOT
  • 29.
    BITFIELD i: Signedu: Unsigned 0 3 u4 unsigne d 4 bits offset bitfield test set u4 0 2 0 0 1 0 bitfield test get u4 0 2 bitfield test get u4 2 8 1 0 0 0
  • 30.
    0 0 00 0 0 0 0 0 0 setbit visits:2020:12:05 9 1 0 0 0 0 0 0 0 0 0 1 setbit visits:2020:12:06 9 1 0 0 0 0 0 0 1 1 0 0 setbit visits:2020:12:10 9 1 1 0 1 0 0 1 1 0 1 1
  • 31.
    BITOP OR visits:2020:12visits:2020:12:05 visits:2020:12:06 visits:2020:12:10 1 0 1 0 0 1 1 1 0 1 BITOP AND visits:2020:12 visits:2020:12:05 visits:2020:12:06 visits:2020:12:10 0 0 0 0 0 0 0 0 0 0
  • 32.
    0 1 2 0 1 2 offset= Y * MAX_WIDTH + X
  • 33.
  • 34.
    ‫في‬ ‫الصور‬ ‫حفظ‬‫الجيد‬ ‫من‬ ‫هل‬ Redis ‫؟‬ ‫استخدام‬ ‫يمكن‬ ‫أين‬ Redis ‫؟‬
  • 35.

Editor's Notes

  • #6 https://redis.io/commands/keys
  • #7 https://redis.io/commands/scan
  • #8 DEL https://redis.io/commands/del UNLINK https://redis.io/commands/unlink
  • #14 https://redis.io/topics/data-types-intro
  • #19 ListQueue
  • #30 https://en.wikipedia.org/wiki/Bit_field
  • #31 Convert to binary: https://codebeautify.org/string-binary-converter Convert number to binary: https://www.rapidtables.com/convert/number/decimal-to-binary.html 0000000000000000000000000000000
  • #38 https://redis.io/topics/streams-intro