Redis Workshop
Data Structures, Commands, Administration
Sripathi Krishnan, CTO, HashedIn Technologies
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Think of Redis as extended RAM for your application.
When you create objects in java or python, they are allocated in Heap memory. These objects
are only available within that process and on that specific server.
In contrast, Redis is a separate process. It usually runs on a separate server independent of
your application. As a result, if your application has multiple servers, each of those servers can
access the objects stored in Redis.
Thus, Redis is a shared memory for your application. It is accessed over the network. You can
use any programming language to connect to Redis.
Redis Data Model
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Redis is an in-memory data structure store.
Keys are variable names / reference that are used to retrieve objects. In Redis, keys are always
strings.
Values are the objects stored against a key. Redis has several types of values - strings, hashes,
sets, lists, sortedsets, geo hash, hyperloglog, and bit arrays.
An object in redis is a) A key, b) its corresponding value, and c) any associated expiry
Redis is a data structure store
Image Credit: Redis Labs
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Keys are strings, and must be unique within a redis instance. By convention, redis keys are
separated by colon (:)
● “users:123” is a key representing a user with id = 123
● “users:123:favourites” is a key representing the favourites of user with id = 123
Keys always have an associated value. If the value is empty / null, redis automatically deletes
the key.
Keys
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Redis has 5 basic data types:
1. Strings
2. Hashes
3. Lists
4. Sets
5. Sorted Sets
In addition, it the following advanced data structures:
1. Bitmaps: built on string
2. Bitfield: built on string
3. HyperLogLog: built on string
4. Geospatial: built on sortedset
Data Types in Redis
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Object Lifecycle
A key = value pair is called an object in redis.
Objects are created automatically when you first use them. There is no separate step to
instantiate an object.
Similarly, an object is automatically deleted when the value becomes empty. For example, if a
set or list has zero elements, it is deleted automatically.
Finally, any object can have an expiry. Once an object expires, it is automatically deleted by
redis in a background thread.
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
● Expiry
You can set an expiry on any key, regardless of the type. Once a key expires, Redis
automatically deletes it from memory.
● Existence
You can check if a key exists in memory
● Type
You can check the type of a key - i.e. whether it’s a string, set or list etc.
For a full list of key related commands, see https://redis.io/commands#generic
Redis General Operations on Objects
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Hashes can store key=value pairs. Hashes
cannot be further nested, the value must be a
binary string.
- HashMap in Java becomes a Hash in Redis
- Dict in Python/Javascript becomes a Hash
in Redis
If you have a class in java or python, you would
typically store that as a Hash in Redis
Hashes
Common Commands
hmset blogs:123 id 123 title "intro to
redis" views 904
hget blogs:123 title
hgetall blogs:123
hincrby blogs:123 views 1
hmget blogs:123 views title
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Sets store unique things - no duplicates
allowed. Also, elements within a set have no
particular order.
Checking for existence of a member in a set is
very fast - O(1) time complexity.
You can perform intersection, union and diff of
sets very quickly.
Sets
Common Commands
sadd blogs_tagged:aws 130 120 140
sadd blogs_tagged:redis 140 170 150
smembers blogs_tagged:aws
sinter blogs_tagged:redis
blogs_tagged:aws
sismember blogs_tagged:aws 120
scard blogs_tagged:aws
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Think of an array or list from any programming
language - you can perform those operations in
a redis list - length of a list, add/remove items,
get a sublist etc.
In Redis, a list also acts as a queue and as a
stack. You can insert elements from either end.
You have blocking variants for pop. If a list is
empty, the connection will block until a new
element is available.
Lists
Common Commands
lpush recent_blogs 123 190
rpush recent_blogs 124 130
lrange recent_blogs 0 2
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Sorted Set = Hash + List
● Hash because you can store a “score” with
every element
● List because you can retrieve a subset of
elements in a sorted order
Sorted Sets
Common Commands
ZADD most_liked 1 123
ZADD most_liked 1 234
ZINCRBY most_liked 1 234
ZRANGE most_liked 0 -1
ZRANGE most_liked 0 -1 WITHSCORES
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
They are the simplest and most complicated
data structure.
First, strings are binary. Which means you can
store text like “hello world”, or numbers like 42,
or a floating point number like 3.14
Strings
Common Commands
set pi 3.14
get pi
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
ASSIGNMENT: BASIC DATA STRUCTURES
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Assignments
We will build features of an ecommerce store
using just redis.
You are expected to:
1. Choose data structure - with key names and values
2. Write commands to insert data
3. Write commands to retrieve data
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Q1: Products You Viewed Recently
You want to show users products the last 5 products they
viewed recently. How will you store this data in redis?
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
First, a Product object needs to be stored in redis. The best way to store objects in Redis is
using the hash data structure:
hmset products:123 id 123 title “Casual T-Shirt” price 1849 discounted_price 999
hmset products:234 id 234 title “Formal Shirt” price 2349 discounted_price 1499
hmset products:345 id 345 title “Denim Shirt” price 1789 discounted_price 1299
This creates 3 products in redis.
Solution: Recently Viewed Products - Part I
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Now, every time a user views a product, append product id to a list:
lpush users:984:recently_viewed 123
lpush users:984:recently_viewed 345
We only want to maintain 5, so trim the array every time we insert a product:
ltrim users:984:recently_viewed 0 4
To get the ids of recently viewed products:
lrange users:984:recently_viewed 0 -1
Then use redis pipeline to get details of all the products:
hgetall products:123
hgetall products:345
Solution: Recently Viewed Products - Part II
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Q2: Filtering Products on Listing Page
You want to build a listing page to display products. On the
left, you have a filters widget with the following filters:
1. Brand Filter - Allen Solly, Van Heusen etc.
2. Price Range Filter - less than 1000, 1000-2000,
2000-3000, greater than 3000
You need to display products that match the criteria that
the user selected.
How can you build this using Redis?
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
For each filter, store matching product ids in a set:
sadd filters:brand:allen-solly 123 124 253 345
sadd filters:brand:van-heusen 764 873 982
sadd filters:price:less-than-1000 123 345 764
sadd filters:price:1000-2000 124 873
sadd filters:price:2000-3000 982 253
To find products where brand=”Allen Solly” and price is less than 1000:
sinter filters:brand:allen-solly filters:price:less-than-1000
This will give you matching product ids. You can then fetch all the products using hget in a
pipeline:
hgetall products:123
hgetall products:345
Solution: Filtering Products on Listing Page
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Q3: Avoid sending duplicate emails
Your application needs to send transactional emails and SMS whenever an order is confirmed.
For performance reasons, you are sending these notifications via a background task.
If there are infrastructure issues, background jobs can run twice or even thrice. Naturally, you
don’t want the same email / sms to be sent to the user multiple times.
Using Redis, how can you eliminate duplicate emails / sms?
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Solution: Deduplication
Create a unique key for your email / sms. For example:
orders:123:confirmation-email
Before you send the email, check against redis if the key exists.
1. If the key does not exist - create the key in redis and continue to send the email
2. If the key exists - do nothing, since the email was already sent earlier
Finally, you want to automatically clear the key after some time, so set an expiry on the key.
All this is achieved by:
● SETNX orders:123:confirmation-email sent
● If response is 1, the key doesn’t exist and was created - so send email
● EXPIRE orders:123:confirmation-email 300
● Else If response is 0, the key exists - so don’t send email
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Q4: Real Time Analytics
Management team wants a real time dashboard that shows them the following:
1. For each category of products, they want to see total items sold and total revenue
2. For each department, they want to see the top 5 items with highest sales
This data should be for the current hour and previous hours. The dashboard must always
show latest data. Your SQL database cannot handle the extra load your dashboard will
generate.
How can you build this using redis?
Image Credit: MicroStrategy
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Sales and Revenues keep increasing with every order. This means we simply need a counter
that keeps incrementing every time an order completes.
Counters can be implemented using hashes as well as using strings, but a hash is more
efficient memory wise.
First, let’s build the key. We want data by the hour, so the key must have the date time upto
the hour.
key = category-sales-2017-08-03-1400, value = hash
Now, inside the hash, we can make the category name as the key. The value will be the actual
items sold.
When an order is completed, increment the counter. Redis will automatically create the hash if
it does not exist.
hincrby category-sales-2017-08-03-1400 electronics-a 1
Solution: Total Revenue & Sales By Category - I
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Similarly, for revenue, you can create another hash, and increment it every time an order
completes:
hincrby category-revenue-2017-08-03-1400 electronics-a 1285
When you want to render the dashboard, use hgetall to get the current values:
hgetall category-sales-2017-08-03-1400
hgetall category-revenue-2017-08-03-1400
If you want to see data for the past hour, change the key name to *-1300
And finally, you can set an expiry on the hash, so that the data is deleted after a day.
expires category-sales-2017-08-03-1400 86400
Solution: Total Revenue & Sales By Category - II
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Solution: Top 5 items with highest sales
Whenever you see “top N” based on some criteria, sorted set is a good choice.
We want top 5 items by department, so the department name or id has to be part of the key.
Also, we want to sort by sales, the score would be the number of sales for each item.
zadd category:sales 1 "electronicsa"
ZRANGE category:sales 0 4
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Demo: Pub/Sub
Open 3 terminal windows:
Terminal 1:
subscribe channel:questions:redis
Terminal 2:
subscribe channel:questions:redis channel:questions:aws
Terminal 3:
publish channel:questions:redis “What is the difference between RDB and AOF?”
publish channel:questions:aws "Redis on ElasticCache v/s Redis on EC2"
Copyright © 2017 HashedIn Technologies Pvt. Ltd.
Demo: Persistence Options
● Run the info command
●

Redis Workshop on Data Structures, Commands, Administration

  • 1.
    Redis Workshop Data Structures,Commands, Administration Sripathi Krishnan, CTO, HashedIn Technologies
  • 2.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Think of Redis as extended RAM for your application. When you create objects in java or python, they are allocated in Heap memory. These objects are only available within that process and on that specific server. In contrast, Redis is a separate process. It usually runs on a separate server independent of your application. As a result, if your application has multiple servers, each of those servers can access the objects stored in Redis. Thus, Redis is a shared memory for your application. It is accessed over the network. You can use any programming language to connect to Redis. Redis Data Model
  • 3.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Redis is an in-memory data structure store. Keys are variable names / reference that are used to retrieve objects. In Redis, keys are always strings. Values are the objects stored against a key. Redis has several types of values - strings, hashes, sets, lists, sortedsets, geo hash, hyperloglog, and bit arrays. An object in redis is a) A key, b) its corresponding value, and c) any associated expiry Redis is a data structure store
  • 4.
  • 5.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Keys are strings, and must be unique within a redis instance. By convention, redis keys are separated by colon (:) ● “users:123” is a key representing a user with id = 123 ● “users:123:favourites” is a key representing the favourites of user with id = 123 Keys always have an associated value. If the value is empty / null, redis automatically deletes the key. Keys
  • 6.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Redis has 5 basic data types: 1. Strings 2. Hashes 3. Lists 4. Sets 5. Sorted Sets In addition, it the following advanced data structures: 1. Bitmaps: built on string 2. Bitfield: built on string 3. HyperLogLog: built on string 4. Geospatial: built on sortedset Data Types in Redis
  • 7.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Object Lifecycle A key = value pair is called an object in redis. Objects are created automatically when you first use them. There is no separate step to instantiate an object. Similarly, an object is automatically deleted when the value becomes empty. For example, if a set or list has zero elements, it is deleted automatically. Finally, any object can have an expiry. Once an object expires, it is automatically deleted by redis in a background thread.
  • 8.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. ● Expiry You can set an expiry on any key, regardless of the type. Once a key expires, Redis automatically deletes it from memory. ● Existence You can check if a key exists in memory ● Type You can check the type of a key - i.e. whether it’s a string, set or list etc. For a full list of key related commands, see https://redis.io/commands#generic Redis General Operations on Objects
  • 9.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Hashes can store key=value pairs. Hashes cannot be further nested, the value must be a binary string. - HashMap in Java becomes a Hash in Redis - Dict in Python/Javascript becomes a Hash in Redis If you have a class in java or python, you would typically store that as a Hash in Redis Hashes Common Commands hmset blogs:123 id 123 title "intro to redis" views 904 hget blogs:123 title hgetall blogs:123 hincrby blogs:123 views 1 hmget blogs:123 views title
  • 10.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Sets store unique things - no duplicates allowed. Also, elements within a set have no particular order. Checking for existence of a member in a set is very fast - O(1) time complexity. You can perform intersection, union and diff of sets very quickly. Sets Common Commands sadd blogs_tagged:aws 130 120 140 sadd blogs_tagged:redis 140 170 150 smembers blogs_tagged:aws sinter blogs_tagged:redis blogs_tagged:aws sismember blogs_tagged:aws 120 scard blogs_tagged:aws
  • 11.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Think of an array or list from any programming language - you can perform those operations in a redis list - length of a list, add/remove items, get a sublist etc. In Redis, a list also acts as a queue and as a stack. You can insert elements from either end. You have blocking variants for pop. If a list is empty, the connection will block until a new element is available. Lists Common Commands lpush recent_blogs 123 190 rpush recent_blogs 124 130 lrange recent_blogs 0 2
  • 12.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Sorted Set = Hash + List ● Hash because you can store a “score” with every element ● List because you can retrieve a subset of elements in a sorted order Sorted Sets Common Commands ZADD most_liked 1 123 ZADD most_liked 1 234 ZINCRBY most_liked 1 234 ZRANGE most_liked 0 -1 ZRANGE most_liked 0 -1 WITHSCORES
  • 13.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. They are the simplest and most complicated data structure. First, strings are binary. Which means you can store text like “hello world”, or numbers like 42, or a floating point number like 3.14 Strings Common Commands set pi 3.14 get pi
  • 14.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. ASSIGNMENT: BASIC DATA STRUCTURES
  • 15.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Assignments We will build features of an ecommerce store using just redis. You are expected to: 1. Choose data structure - with key names and values 2. Write commands to insert data 3. Write commands to retrieve data
  • 16.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Q1: Products You Viewed Recently You want to show users products the last 5 products they viewed recently. How will you store this data in redis?
  • 17.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. First, a Product object needs to be stored in redis. The best way to store objects in Redis is using the hash data structure: hmset products:123 id 123 title “Casual T-Shirt” price 1849 discounted_price 999 hmset products:234 id 234 title “Formal Shirt” price 2349 discounted_price 1499 hmset products:345 id 345 title “Denim Shirt” price 1789 discounted_price 1299 This creates 3 products in redis. Solution: Recently Viewed Products - Part I
  • 18.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Now, every time a user views a product, append product id to a list: lpush users:984:recently_viewed 123 lpush users:984:recently_viewed 345 We only want to maintain 5, so trim the array every time we insert a product: ltrim users:984:recently_viewed 0 4 To get the ids of recently viewed products: lrange users:984:recently_viewed 0 -1 Then use redis pipeline to get details of all the products: hgetall products:123 hgetall products:345 Solution: Recently Viewed Products - Part II
  • 19.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Q2: Filtering Products on Listing Page You want to build a listing page to display products. On the left, you have a filters widget with the following filters: 1. Brand Filter - Allen Solly, Van Heusen etc. 2. Price Range Filter - less than 1000, 1000-2000, 2000-3000, greater than 3000 You need to display products that match the criteria that the user selected. How can you build this using Redis?
  • 20.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. For each filter, store matching product ids in a set: sadd filters:brand:allen-solly 123 124 253 345 sadd filters:brand:van-heusen 764 873 982 sadd filters:price:less-than-1000 123 345 764 sadd filters:price:1000-2000 124 873 sadd filters:price:2000-3000 982 253 To find products where brand=”Allen Solly” and price is less than 1000: sinter filters:brand:allen-solly filters:price:less-than-1000 This will give you matching product ids. You can then fetch all the products using hget in a pipeline: hgetall products:123 hgetall products:345 Solution: Filtering Products on Listing Page
  • 21.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Q3: Avoid sending duplicate emails Your application needs to send transactional emails and SMS whenever an order is confirmed. For performance reasons, you are sending these notifications via a background task. If there are infrastructure issues, background jobs can run twice or even thrice. Naturally, you don’t want the same email / sms to be sent to the user multiple times. Using Redis, how can you eliminate duplicate emails / sms?
  • 22.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Solution: Deduplication Create a unique key for your email / sms. For example: orders:123:confirmation-email Before you send the email, check against redis if the key exists. 1. If the key does not exist - create the key in redis and continue to send the email 2. If the key exists - do nothing, since the email was already sent earlier Finally, you want to automatically clear the key after some time, so set an expiry on the key. All this is achieved by: ● SETNX orders:123:confirmation-email sent ● If response is 1, the key doesn’t exist and was created - so send email ● EXPIRE orders:123:confirmation-email 300 ● Else If response is 0, the key exists - so don’t send email
  • 23.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Q4: Real Time Analytics Management team wants a real time dashboard that shows them the following: 1. For each category of products, they want to see total items sold and total revenue 2. For each department, they want to see the top 5 items with highest sales This data should be for the current hour and previous hours. The dashboard must always show latest data. Your SQL database cannot handle the extra load your dashboard will generate. How can you build this using redis?
  • 24.
  • 25.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Sales and Revenues keep increasing with every order. This means we simply need a counter that keeps incrementing every time an order completes. Counters can be implemented using hashes as well as using strings, but a hash is more efficient memory wise. First, let’s build the key. We want data by the hour, so the key must have the date time upto the hour. key = category-sales-2017-08-03-1400, value = hash Now, inside the hash, we can make the category name as the key. The value will be the actual items sold. When an order is completed, increment the counter. Redis will automatically create the hash if it does not exist. hincrby category-sales-2017-08-03-1400 electronics-a 1 Solution: Total Revenue & Sales By Category - I
  • 26.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Similarly, for revenue, you can create another hash, and increment it every time an order completes: hincrby category-revenue-2017-08-03-1400 electronics-a 1285 When you want to render the dashboard, use hgetall to get the current values: hgetall category-sales-2017-08-03-1400 hgetall category-revenue-2017-08-03-1400 If you want to see data for the past hour, change the key name to *-1300 And finally, you can set an expiry on the hash, so that the data is deleted after a day. expires category-sales-2017-08-03-1400 86400 Solution: Total Revenue & Sales By Category - II
  • 27.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Solution: Top 5 items with highest sales Whenever you see “top N” based on some criteria, sorted set is a good choice. We want top 5 items by department, so the department name or id has to be part of the key. Also, we want to sort by sales, the score would be the number of sales for each item. zadd category:sales 1 "electronicsa" ZRANGE category:sales 0 4
  • 28.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Demo: Pub/Sub Open 3 terminal windows: Terminal 1: subscribe channel:questions:redis Terminal 2: subscribe channel:questions:redis channel:questions:aws Terminal 3: publish channel:questions:redis “What is the difference between RDB and AOF?” publish channel:questions:aws "Redis on ElasticCache v/s Redis on EC2"
  • 29.
    Copyright © 2017HashedIn Technologies Pvt. Ltd. Demo: Persistence Options ● Run the info command ●