SlideShare a Scribd company logo
1 of 29
Download to read offline
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
●

More Related Content

What's hot

Cloudian HyperStore Operating Environment
Cloudian HyperStore Operating EnvironmentCloudian HyperStore Operating Environment
Cloudian HyperStore Operating EnvironmentCloudian
 
Webinar: Fighting Fraud with Graph Databases
Webinar: Fighting Fraud with Graph DatabasesWebinar: Fighting Fraud with Graph Databases
Webinar: Fighting Fraud with Graph DatabasesDataStax
 
Analytics over Terabytes of Data at Twitter
Analytics over Terabytes of Data at TwitterAnalytics over Terabytes of Data at Twitter
Analytics over Terabytes of Data at TwitterImply
 
Azure Big Data Story
Azure Big Data StoryAzure Big Data Story
Azure Big Data StoryLynn Langit
 
Building a Microservices-based ERP System
Building a Microservices-based ERP SystemBuilding a Microservices-based ERP System
Building a Microservices-based ERP SystemMongoDB
 
MongoDB .local Munich 2019: Mastering MongoDB on Kubernetes – MongoDB Enterpr...
MongoDB .local Munich 2019: Mastering MongoDB on Kubernetes – MongoDB Enterpr...MongoDB .local Munich 2019: Mastering MongoDB on Kubernetes – MongoDB Enterpr...
MongoDB .local Munich 2019: Mastering MongoDB on Kubernetes – MongoDB Enterpr...MongoDB
 
I’ve outgrown my basic stack. Now what?
I’ve outgrown my basic stack. Now what?I’ve outgrown my basic stack. Now what?
I’ve outgrown my basic stack. Now what?Francis David Cleary
 

What's hot (9)

Nosql Introduction, Basics
Nosql Introduction, BasicsNosql Introduction, Basics
Nosql Introduction, Basics
 
Mongo db 3.4 Overview
Mongo db 3.4 OverviewMongo db 3.4 Overview
Mongo db 3.4 Overview
 
Cloudian HyperStore Operating Environment
Cloudian HyperStore Operating EnvironmentCloudian HyperStore Operating Environment
Cloudian HyperStore Operating Environment
 
Webinar: Fighting Fraud with Graph Databases
Webinar: Fighting Fraud with Graph DatabasesWebinar: Fighting Fraud with Graph Databases
Webinar: Fighting Fraud with Graph Databases
 
Analytics over Terabytes of Data at Twitter
Analytics over Terabytes of Data at TwitterAnalytics over Terabytes of Data at Twitter
Analytics over Terabytes of Data at Twitter
 
Azure Big Data Story
Azure Big Data StoryAzure Big Data Story
Azure Big Data Story
 
Building a Microservices-based ERP System
Building a Microservices-based ERP SystemBuilding a Microservices-based ERP System
Building a Microservices-based ERP System
 
MongoDB .local Munich 2019: Mastering MongoDB on Kubernetes – MongoDB Enterpr...
MongoDB .local Munich 2019: Mastering MongoDB on Kubernetes – MongoDB Enterpr...MongoDB .local Munich 2019: Mastering MongoDB on Kubernetes – MongoDB Enterpr...
MongoDB .local Munich 2019: Mastering MongoDB on Kubernetes – MongoDB Enterpr...
 
I’ve outgrown my basic stack. Now what?
I’ve outgrown my basic stack. Now what?I’ve outgrown my basic stack. Now what?
I’ve outgrown my basic stack. Now what?
 

Similar to Redis Workshop on Data Structures, Commands, Administration

Redis memory optimization sripathi, CTO hashedin
Redis memory optimization   sripathi, CTO hashedinRedis memory optimization   sripathi, CTO hashedin
Redis memory optimization sripathi, CTO hashedinHashedIn Technologies
 
Building High Performance Apps with In-memory Data
Building High Performance Apps with In-memory DataBuilding High Performance Apps with In-memory Data
Building High Performance Apps with In-memory DataAmazon Web Services
 
Build an ETL Pipeline to Analyze Customer Data (AIM416) - AWS re:Invent 2018
Build an ETL Pipeline to Analyze Customer Data (AIM416) - AWS re:Invent 2018Build an ETL Pipeline to Analyze Customer Data (AIM416) - AWS re:Invent 2018
Build an ETL Pipeline to Analyze Customer Data (AIM416) - AWS re:Invent 2018Amazon Web Services
 
Managing Data with Amazon ElastiCache for Redis - August 2016 Monthly Webinar...
Managing Data with Amazon ElastiCache for Redis - August 2016 Monthly Webinar...Managing Data with Amazon ElastiCache for Redis - August 2016 Monthly Webinar...
Managing Data with Amazon ElastiCache for Redis - August 2016 Monthly Webinar...Amazon Web Services
 
Redispresentation apac2012
Redispresentation apac2012Redispresentation apac2012
Redispresentation apac2012Ankur Gupta
 
A Look Under the Hood – How Amazon.com Uses AWS Services for Analytics at Mas...
A Look Under the Hood – How Amazon.com Uses AWS Services for Analytics at Mas...A Look Under the Hood – How Amazon.com Uses AWS Services for Analytics at Mas...
A Look Under the Hood – How Amazon.com Uses AWS Services for Analytics at Mas...Amazon Web Services
 
How Amazon.com Uses AWS Analytics: Data Analytics Week SF
How Amazon.com Uses AWS Analytics: Data Analytics Week SFHow Amazon.com Uses AWS Analytics: Data Analytics Week SF
How Amazon.com Uses AWS Analytics: Data Analytics Week SFAmazon Web Services
 
Cranking It Up - SuiteWorld 2017
Cranking It Up  - SuiteWorld 2017Cranking It Up  - SuiteWorld 2017
Cranking It Up - SuiteWorld 2017Diego Cardozo
 
Building a game with JavaScript (March 2017, washington dc)
Building a game with JavaScript (March 2017, washington dc)Building a game with JavaScript (March 2017, washington dc)
Building a game with JavaScript (March 2017, washington dc)Daniel Friedman
 
How Amazon.com uses AWS Analytics
How Amazon.com uses AWS AnalyticsHow Amazon.com uses AWS Analytics
How Amazon.com uses AWS AnalyticsAmazon Web Services
 
How Amazon.com Uses AWS Analytics
How Amazon.com Uses AWS AnalyticsHow Amazon.com Uses AWS Analytics
How Amazon.com Uses AWS AnalyticsAmazon Web Services
 
Data scientist enablement dse 400 week 6 roadmap
Data scientist enablement   dse 400   week 6 roadmapData scientist enablement   dse 400   week 6 roadmap
Data scientist enablement dse 400 week 6 roadmapDr. Mohan K. Bavirisetty
 
How to build a data lake with aws glue data catalog (ABD213-R) re:Invent 2017
How to build a data lake with aws glue data catalog (ABD213-R)  re:Invent 2017How to build a data lake with aws glue data catalog (ABD213-R)  re:Invent 2017
How to build a data lake with aws glue data catalog (ABD213-R) re:Invent 2017Amazon Web Services
 
How Amazon.com uses AWS Analytics
How Amazon.com uses AWS AnalyticsHow Amazon.com uses AWS Analytics
How Amazon.com uses AWS AnalyticsAmazon Web Services
 
Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...
Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...
Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...Amazon Web Services
 
Best Practices for Distributed Machine Learning and Predictive Analytics Usin...
Best Practices for Distributed Machine Learning and Predictive Analytics Usin...Best Practices for Distributed Machine Learning and Predictive Analytics Usin...
Best Practices for Distributed Machine Learning and Predictive Analytics Usin...Amazon Web Services
 

Similar to Redis Workshop on Data Structures, Commands, Administration (20)

Redis memory optimization sripathi, CTO hashedin
Redis memory optimization   sripathi, CTO hashedinRedis memory optimization   sripathi, CTO hashedin
Redis memory optimization sripathi, CTO hashedin
 
Building High Performance Apps with In-memory Data
Building High Performance Apps with In-memory DataBuilding High Performance Apps with In-memory Data
Building High Performance Apps with In-memory Data
 
Introduction to redis
Introduction to redisIntroduction to redis
Introduction to redis
 
Build an ETL Pipeline to Analyze Customer Data (AIM416) - AWS re:Invent 2018
Build an ETL Pipeline to Analyze Customer Data (AIM416) - AWS re:Invent 2018Build an ETL Pipeline to Analyze Customer Data (AIM416) - AWS re:Invent 2018
Build an ETL Pipeline to Analyze Customer Data (AIM416) - AWS re:Invent 2018
 
Redis overview
Redis overviewRedis overview
Redis overview
 
Managing Data with Amazon ElastiCache for Redis - August 2016 Monthly Webinar...
Managing Data with Amazon ElastiCache for Redis - August 2016 Monthly Webinar...Managing Data with Amazon ElastiCache for Redis - August 2016 Monthly Webinar...
Managing Data with Amazon ElastiCache for Redis - August 2016 Monthly Webinar...
 
Redispresentation apac2012
Redispresentation apac2012Redispresentation apac2012
Redispresentation apac2012
 
Guidelines HTML5 & CSS3 - Atlogys (2018)
Guidelines HTML5 & CSS3 - Atlogys (2018)Guidelines HTML5 & CSS3 - Atlogys (2018)
Guidelines HTML5 & CSS3 - Atlogys (2018)
 
A Look Under the Hood – How Amazon.com Uses AWS Services for Analytics at Mas...
A Look Under the Hood – How Amazon.com Uses AWS Services for Analytics at Mas...A Look Under the Hood – How Amazon.com Uses AWS Services for Analytics at Mas...
A Look Under the Hood – How Amazon.com Uses AWS Services for Analytics at Mas...
 
How Amazon.com Uses AWS Analytics: Data Analytics Week SF
How Amazon.com Uses AWS Analytics: Data Analytics Week SFHow Amazon.com Uses AWS Analytics: Data Analytics Week SF
How Amazon.com Uses AWS Analytics: Data Analytics Week SF
 
Cranking It Up - SuiteWorld 2017
Cranking It Up  - SuiteWorld 2017Cranking It Up  - SuiteWorld 2017
Cranking It Up - SuiteWorld 2017
 
Building a game with JavaScript (March 2017, washington dc)
Building a game with JavaScript (March 2017, washington dc)Building a game with JavaScript (March 2017, washington dc)
Building a game with JavaScript (March 2017, washington dc)
 
How Amazon.com uses AWS Analytics
How Amazon.com uses AWS AnalyticsHow Amazon.com uses AWS Analytics
How Amazon.com uses AWS Analytics
 
How Amazon.com Uses AWS Analytics
How Amazon.com Uses AWS AnalyticsHow Amazon.com Uses AWS Analytics
How Amazon.com Uses AWS Analytics
 
Data scientist enablement dse 400 week 6 roadmap
Data scientist enablement   dse 400   week 6 roadmapData scientist enablement   dse 400   week 6 roadmap
Data scientist enablement dse 400 week 6 roadmap
 
How to build a data lake with aws glue data catalog (ABD213-R) re:Invent 2017
How to build a data lake with aws glue data catalog (ABD213-R)  re:Invent 2017How to build a data lake with aws glue data catalog (ABD213-R)  re:Invent 2017
How to build a data lake with aws glue data catalog (ABD213-R) re:Invent 2017
 
How Amazon.com uses AWS Analytics
How Amazon.com uses AWS AnalyticsHow Amazon.com uses AWS Analytics
How Amazon.com uses AWS Analytics
 
Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...
Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...
Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...
 
Best Practices for Distributed Machine Learning and Predictive Analytics Usin...
Best Practices for Distributed Machine Learning and Predictive Analytics Usin...Best Practices for Distributed Machine Learning and Predictive Analytics Usin...
Best Practices for Distributed Machine Learning and Predictive Analytics Usin...
 
ansible_rhel_90.pdf
ansible_rhel_90.pdfansible_rhel_90.pdf
ansible_rhel_90.pdf
 

Recently uploaded

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 

Recently uploaded (20)

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 

Redis Workshop on Data Structures, Commands, Administration

  • 1. Redis Workshop Data Structures, Commands, Administration Sripathi Krishnan, CTO, HashedIn Technologies
  • 2. 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
  • 3. 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
  • 5. 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
  • 6. 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
  • 7. 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.
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. Copyright © 2017 HashedIn Technologies Pvt. Ltd. ASSIGNMENT: BASIC DATA STRUCTURES
  • 15. 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
  • 16. 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?
  • 17. 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
  • 18. 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
  • 19. 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?
  • 20. 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
  • 21. 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?
  • 22. 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
  • 23. 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?
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. 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"
  • 29. Copyright © 2017 HashedIn Technologies Pvt. Ltd. Demo: Persistence Options ● Run the info command ●