Dave Nielsen, Developer Advocate
dave@redislabs.com @davenielsen
Redis Labs @redislabs
“Redis Functions and
Data Structures”
2
Redis = A Unique Database
Redis is an open source (BSD licensed),
in-memory, data structure store,
used as database, cache AND message broker
3
About Redis
# 1
NoSQL in User
Satisfaction and
Market Presence
[@G2CROWD]
Redis has the largest open source community among the NoSQL databases
Created by Salvatore Sanfilippo (@antirez)
In growth among
NoSQL databases
[@DB-Engines]
NoSQL database
on containers
[@DevOps.com &
ClusterHQ]
Out of 50 tools
developers love to
use
[@Stackshare]
# 1 # 1 # 12
4
Redis Helps the Web Scale!
5
Data Structure Store
“MY GOD, IT’S FULL OF STRUCTURES.”
6
Data structures are used like “Lego” building blocks,
saving developers coding effort and time
Redis : A Data Structure Store
Strings Hashes Lists Sets
Sorted Sets Bit Arrays
Hyper-
LogLogs
Geospatial
indexes
7
What Can You Do With Redis?
Common Uses
• User Sessions
• Message Brokers/Queues
• Real-time Recommendation Engine
• Leaderboads
• …More
Use as in-memory database, cache or message broker
8
User Sessions
The Problem
• Maintain session state across
multiple servers
• Multiple session variables
• High speed/low latency required
Why Redis Rocks
• Hashes are perfect for this!
• HSET lets you save session
variables as key/value pairs
• HGET to retrieve values
• HINCRBY to increment any
field within the hash structure
9
Redis Hashes for User Sessions
userid 8754
name dave
ip 10:20:104:31
hits 1
lastpage home
hash key: usersession:1
HMSET usersession:1 userid 8754 name dave ip 10:20:104:31 hits 1
HMGET usersession:1 userid name ip hits
HINCRBY usersession:1 hits 1
HSET usersession:1 lastpage “home”
HGET usersession:1 lastpage
HDEL usersession:1 lastpage
Hashes store a mapping of keys to values – like a dictionary or associative array – but faster
DEL usersession:1
10
Managing Queues of Work
The Problem
• Tasks need to be worked on asynch
to reduce block/wait times
• Lots of items to be worked on
• Assign items to worker process and
remove from queue at the same time
• Similar to buffering high speed data-
ingestion
Why Redis Rocks
• Lists are perfect for this!
• LPUSH, RPUSH add values at
beginning or end of queue
• RPOPLPUSH – pops an item
from one queue and pushes it
to another queue
11
Redis Lists for Managing Queues
LPUSH queue1 orange
LPUSH queue1 green
LPUSH queue1 blue
RPUSH queue1 red
LPUSH adds values to head of list
RPUSH adds value to tail of list
blue green orange .. .. red
12
Redis Lists for Managing Queues
blue green orange .. ..
RPOPLPUSH queue1 queue2
red
LPUSH queue1 orange
LPUSH queue1 green
LPUSH queue1 blue
RPUSH queue1 red
RPOPLPUSH pops a value from one list and pushes it to another list
13
Real-time Recommendation Engine
The Problem
• People who read this article also
read these other articles
• Want real time not data mining
Also used for:
• Recommending Similar Purchases
• Identifying Fraud
Why Redis Rocks
• SETS are unique collections of strings
• SADD to add tags to each article
• SISMEMBER to check if an article has
a given tag
• SMEMBERS to get all the tags for an
article
• use SINTER to find similar articles
tagged with the same tags
14
Redis Sets for Recommendations
SADD tag:1 article:3 article:1
SADD tag:2 article:22 article:14 article:3
SADD tag:3 article:9 article:3 article:2
SMEMBERS tag:3 (also tag:1 & tag:2)
article 1 article 3 ….
Set: tag:1
article 3 article 14 Article 22 ..
Set: tag:2
SINTER tag:1 tag:2 tag:3
article 2 article 3 article 9 ..
Set: tag:3
Add values (articles) to Sets (tags)
Confirm the values have been added
Find values that exist in all three Sets
15
Sorted Sets for Leaderboards
The Problem
• MANY users playing a game or
collecting points
• Display real-time leaderboard.
• Who is your nearest competition
• Disk-based DB is too slow
Why Redis Rocks
• Sorted Sets are perfect!
• Automatically keeps list of
users sorted by score
• ZADD to add/update
• ZRANGE, ZREVRANGE to get
user
• ZRANK will get any users
rank instantaneously
16
Redis Sorted Sets
ZADD game:1 10000 id:1
ZADD game:1 21000 id:2
ZADD game:1 34000 id:3
ZADD game:1 35000 id:4
ZADD game:1 44000 id:3
or
ZINCRBY game:1 10000 id:3
34000 id:3
35000 id:4
21000 id:2
10000 id:1
ZREVRANGE game:1 0 0
ZREVRANGE game:1 0 1 WITHSCORES
44000 id:3
+ 10000
17
So What?
• Redis Data Structures are entirely in memory … and blazingly fast!
• Simplicity and speed make each Data Structure easy to use
• Combine Data Structures with Functions like Lego building blocks
• Clustering, Persistence, High Availability are now standard
18
Learn More ...
Open Source Redis  redis.io
Free “Redis in Action” eBook  redislabs.com/ebook
Free 30mb Redis Cloud  redislabs.com
Download RLEC Trial  redislabs.com/redis-enterprise
Home of Redis
Thank You!
Dave Nielsen, Developer Advocate
dave@redislabs.com @davenielsen
Redis Labs @redislabs

Redis Functions, Data Structures for Web Scale Apps

  • 1.
    Dave Nielsen, DeveloperAdvocate [email protected] @davenielsen Redis Labs @redislabs “Redis Functions and Data Structures”
  • 2.
    2 Redis = AUnique Database Redis is an open source (BSD licensed), in-memory, data structure store, used as database, cache AND message broker
  • 3.
    3 About Redis # 1 NoSQLin User Satisfaction and Market Presence [@G2CROWD] Redis has the largest open source community among the NoSQL databases Created by Salvatore Sanfilippo (@antirez) In growth among NoSQL databases [@DB-Engines] NoSQL database on containers [@DevOps.com & ClusterHQ] Out of 50 tools developers love to use [@Stackshare] # 1 # 1 # 12
  • 4.
  • 5.
    5 Data Structure Store “MYGOD, IT’S FULL OF STRUCTURES.”
  • 6.
    6 Data structures areused like “Lego” building blocks, saving developers coding effort and time Redis : A Data Structure Store Strings Hashes Lists Sets Sorted Sets Bit Arrays Hyper- LogLogs Geospatial indexes
  • 7.
    7 What Can YouDo With Redis? Common Uses • User Sessions • Message Brokers/Queues • Real-time Recommendation Engine • Leaderboads • …More Use as in-memory database, cache or message broker
  • 8.
    8 User Sessions The Problem •Maintain session state across multiple servers • Multiple session variables • High speed/low latency required Why Redis Rocks • Hashes are perfect for this! • HSET lets you save session variables as key/value pairs • HGET to retrieve values • HINCRBY to increment any field within the hash structure
  • 9.
    9 Redis Hashes forUser Sessions userid 8754 name dave ip 10:20:104:31 hits 1 lastpage home hash key: usersession:1 HMSET usersession:1 userid 8754 name dave ip 10:20:104:31 hits 1 HMGET usersession:1 userid name ip hits HINCRBY usersession:1 hits 1 HSET usersession:1 lastpage “home” HGET usersession:1 lastpage HDEL usersession:1 lastpage Hashes store a mapping of keys to values – like a dictionary or associative array – but faster DEL usersession:1
  • 10.
    10 Managing Queues ofWork The Problem • Tasks need to be worked on asynch to reduce block/wait times • Lots of items to be worked on • Assign items to worker process and remove from queue at the same time • Similar to buffering high speed data- ingestion Why Redis Rocks • Lists are perfect for this! • LPUSH, RPUSH add values at beginning or end of queue • RPOPLPUSH – pops an item from one queue and pushes it to another queue
  • 11.
    11 Redis Lists forManaging Queues LPUSH queue1 orange LPUSH queue1 green LPUSH queue1 blue RPUSH queue1 red LPUSH adds values to head of list RPUSH adds value to tail of list blue green orange .. .. red
  • 12.
    12 Redis Lists forManaging Queues blue green orange .. .. RPOPLPUSH queue1 queue2 red LPUSH queue1 orange LPUSH queue1 green LPUSH queue1 blue RPUSH queue1 red RPOPLPUSH pops a value from one list and pushes it to another list
  • 13.
    13 Real-time Recommendation Engine TheProblem • People who read this article also read these other articles • Want real time not data mining Also used for: • Recommending Similar Purchases • Identifying Fraud Why Redis Rocks • SETS are unique collections of strings • SADD to add tags to each article • SISMEMBER to check if an article has a given tag • SMEMBERS to get all the tags for an article • use SINTER to find similar articles tagged with the same tags
  • 14.
    14 Redis Sets forRecommendations SADD tag:1 article:3 article:1 SADD tag:2 article:22 article:14 article:3 SADD tag:3 article:9 article:3 article:2 SMEMBERS tag:3 (also tag:1 & tag:2) article 1 article 3 …. Set: tag:1 article 3 article 14 Article 22 .. Set: tag:2 SINTER tag:1 tag:2 tag:3 article 2 article 3 article 9 .. Set: tag:3 Add values (articles) to Sets (tags) Confirm the values have been added Find values that exist in all three Sets
  • 15.
    15 Sorted Sets forLeaderboards The Problem • MANY users playing a game or collecting points • Display real-time leaderboard. • Who is your nearest competition • Disk-based DB is too slow Why Redis Rocks • Sorted Sets are perfect! • Automatically keeps list of users sorted by score • ZADD to add/update • ZRANGE, ZREVRANGE to get user • ZRANK will get any users rank instantaneously
  • 16.
    16 Redis Sorted Sets ZADDgame:1 10000 id:1 ZADD game:1 21000 id:2 ZADD game:1 34000 id:3 ZADD game:1 35000 id:4 ZADD game:1 44000 id:3 or ZINCRBY game:1 10000 id:3 34000 id:3 35000 id:4 21000 id:2 10000 id:1 ZREVRANGE game:1 0 0 ZREVRANGE game:1 0 1 WITHSCORES 44000 id:3 + 10000
  • 17.
    17 So What? • RedisData Structures are entirely in memory … and blazingly fast! • Simplicity and speed make each Data Structure easy to use • Combine Data Structures with Functions like Lego building blocks • Clustering, Persistence, High Availability are now standard
  • 18.
    18 Learn More ... OpenSource Redis  redis.io Free “Redis in Action” eBook  redislabs.com/ebook Free 30mb Redis Cloud  redislabs.com Download RLEC Trial  redislabs.com/redis-enterprise
  • 19.
    Home of Redis ThankYou! Dave Nielsen, Developer Advocate [email protected] @davenielsen Redis Labs @redislabs