SlideShare a Scribd company logo
1 of 11
Download to read offline
16 11 22 12:32Try Redis
1 11http: try.redis.io
Welcome to Try Redis, a demonstration of the Redis database!
Please type TUTORIAL to begin a brief tutorial, HELP to see a list of supported commands, or any
valid Redis command to play with the database.
> HELP
Please type HELP for one of these commands: DECR, DECRBY, DEL, EXISTS, EXPIRE, GET, GETSET,
HDEL, HEXISTS, HGET, HGETALL, HINCRBY, HKEYS, HLEN, HMGET, HMSET, HSET, HVALS, INCR, INCRBY, KEYS,
LINDEX, LLEN, LPOP, LPUSH, LRANGE, LREM, LSET, LTRIM, MGET, MSET, MSETNX, MULTI, PEXPIRE, RENAME,
RENAMENX, RPOP, RPOPLPUSH, RPUSH, SADD, SCARD, SDIFF, SDIFFSTORE, SET, SETEX, SETNX, SINTER,
SINTERSTORE, SISMEMBER, SMEMBERS, SMOVE, SORT, SPOP, SRANDMEMBER, SREM, SUNION, SUNIONSTORE, TTL,
TYPE, ZADD, ZCARD, ZCOUNT, ZINCRBY, ZRANGE, ZRANGEBYSCORE, ZRANK, ZREM, ZREMRANGEBYSCORE,
ZREVRANGE, ZSCORE
> TUTORIAL
Redis is what is called a key-value store, often referred to as a NoSQL database. The essence
of a key-value store is the ability to store some data, called a value, inside a key. This data can
later be retrieved only if we know the exact key used to store it. We can use the command SET
to store the value "fido" at key "server:name":
SET server:name "fido"
Redis will store our data permanently, so we can later ask "What is the value stored at key
server:name?" and Redis will reply with "fido":
GET server:name => "fido"
Tip: You can click the commands above to automatically execute them. The text after the arrow (=>) shows the
expected output.
Type NEXT to continue the tutorial.
> SET server:name "fido"
OK
> GET server:name
"fido"
<key, value>
MGET
16 11 22 12:32Try Redis
2 11http: try.redis.io
> NEXT
Other common operations provided by key-value stores are DEL to delete a given key and
associated value, SET-if-not-exists (called SETNX on Redis) that sets a key only if it does not
already exist, and INCR to atomically increment a number stored at a given key:
SET connections 10
INCR connections => 11
INCR connections => 12
DEL connections
INCR connections => 1
Type NEXT to continue the tutorial.
> SET connections 10
OK
> INCR connections
(integer) 11
> INCR connections
(integer) 12
> DEL connections
(integer) 1
> INCR connections
(integer) 1
> SETNX connections 10
(integer) 0
> GET connections
"1"
> DEL connections
(integer) 1
> SETNX connections 10
(integer) 1
> GET connections
SETNX+EXPIRE/SET
INCRBY INCRBYFLOAT INCR DECRBY DECR
INCRBY / /
16 11 22 12:32Try Redis
3 11http: try.redis.io
"10"
> NEXT
There is something special about INCR. Why do we provide such an operation if we can do it
ourself with a bit of code? After all it is as simple as:
x = GET count
x = x + 1
SET count x
The problem is that doing the increment in this way will only work as long as there is a single
client using the key. See what happens if two clients are accessing this key at the same time:
1. Client A reads count as 10.
2. Client B reads count as 10.
3. Client A increments 10 and sets count to 11.
4. Client B increments 10 and sets count to 11.
We wanted the value to be 12, but instead it is 11! This is because incrementing the value in this
way is not an atomic operation. Calling the INCR command in Redis will prevent this from
happening, because it is an atomic operation. Redis provides many of these atomic operations
on different types of data.
Type NEXT to continue the tutorial.
> NEXT
Redis can be told that a key should only exist for a certain length of time. This is accomplished
with the EXPIRE and TTL commands.
SET resource:lock "Redis Demo"
EXPIRE resource:lock 120
This causes the key resource:lock to be deleted in 120 seconds. You can test how long a key
will exist with the TTL command. It returns the number of seconds until it will be deleted.
TTL resource:lock => 113
// after 113s
TTL resource:lock => -2
The -2 for the TTL of the key means that the key does not exist (anymore). A -1 for the TTL of the
key means that it will never expire. Note that if you SET a key, its TTL will be reset.
SET resource:lock "Redis Demo 1"
EXPIRE resource:lock 120
TTL resource:lock => 119
Key /
/
Key (persistent) vs (volatile)
time to live
/value /key
Key /
/
Key (persistent) vs (volatile)
time to live
/value /key
16 11 22 12:32Try Redis
4 11http: try.redis.io
SET resource:lock "Redis Demo 2"
TTL resource:lock => -1
Type NEXT to continue the tutorial.
> SET resource:lock "Redis Demo"
OK
> EXPIRE resource:lock 20
(integer) 1
> TTL resource:lock
(integer) 4
> TTL resource:lock
(integer) -2
> SET resource:lock "Redis Demo 1"
OK
> EXPIRE resource:lock 120
(integer) 1
> TTL resource:lock
(integer) 117
> SET resource:lock "Redis Demo 2"
OK
> TTL resource:lock
(integer) -1
> NEXT
Redis also supports several more complex data structures. The first one we'll look at is a list. A
list is a series of ordered values. Some of the important commands for interacting with lists are
RPUSH, LPUSH, LLEN, LRANGE, LPOP, and RPOP. You can immediately begin working with a key as a
list, as long as it doesn't already exist as a different type.
RPUSH puts the new value at the end of the list.
RPUSH friends "Alice"
RPUSH friends "Bob"
<key, [value]>
DEL friends
/
key
MSET ( )
<key, [value]>
DEL friends
/
key
MSET ( )
16 11 22 12:32Try Redis
5 11http: try.redis.io
LPUSH puts the new value at the start of the list.
LPUSH friends "Sam"
LRANGE gives a subset of the list. It takes the index of the first element you want to retrieve as its
first parameter and the index of the last element you want to retrieve as its second parameter. A
value of -1 for the second parameter means to retrieve elements until the end of the list.
LRANGE friends 0 -1 => 1) "Sam", 2) "Alice", 3) "Bob"
LRANGE friends 0 1 => 1) "Sam", 2) "Alice"
LRANGE friends 1 2 => 1) "Alice", 2) "Bob"
Type NEXT to continue the tutorial.
> DEL friends
(integer) 1
> RPUSH friends "Alice"
(integer) 1
> RPUSH friends "Bob"
(integer) 2
> LPUSH friends "Sam"
(integer) 3
> LRANGE friends 0 -1
1) "Sam"
2) "Alice"
3) "Bob"
> LRANGE friends 0 1
1) "Sam"
2) "Alice"
> LRANGE friends 1 2
1) "Alice"
2) "Bob"
> NEXT
LLEN returns the current length of the list.
/
[start, stop]
/
[start, stop]
/
[start, stop]
/
[start, stop]
16 11 22 12:32Try Redis
6 11http: try.redis.io
LLEN friends => 3
LPOP removes the first element from the list and returns it.
LPOP friends => "Sam"
RPOP removes the last element from the list and returns it.
RPOP friends => "Bob"
Note that the list now only has one element:
LLEN friends => 1
LRANGE friends 0 -1 => 1) "Alice"
Type NEXT to continue the tutorial.
> LLEN friends
(integer) 3
> LPOP friends
"Sam"
> RPOP friends
"Bob"
> LLEN friends
(integer) 1
> LRANGE friends 0 -1
1) "Alice"
> NEXT
The next data structure that we'll look at is a set. A set is similar to a list, except it does not have
a specific order and each element may only appear once. Some of the important commands in
working with sets are SADD, SREM, SISMEMBER, SMEMBERS and SUNION.
SADD adds the given value to the set.
SADD superpowers "flight"
SADD superpowers "x-ray vision"
SADD superpowers "reflexes"
SREM removes the given value from the set.
<key, {member}>
( ) SINTER( ) SDIFF( )
/ ( )
/
BRPOP/BLPOP key timeout
RPOPLPUSH/BRPOPLPUSH
<key, {member}>
( ) SINTER( ) SDIFF( )
/ ( )
/
BRPOP/BLPOP key timeout
RPOPLPUSH/BRPOPLPUSH
<key, {member}>
( ) SINTER( ) SDIFF( )
/ ( )
/
BRPOP/BLPOP key timeout
RPOPLPUSH/BRPOPLPUSH
<key, {member}>
( ) SINTER( ) SDIFF( )
/ ( )
/
BRPOP/BLPOP key timeout
RPOPLPUSH/BRPOPLPUSH
16 11 22 12:32Try Redis
7 11http: try.redis.io
SREM superpowers "reflexes"
Type NEXT to continue the tutorial.
> SADD superpowers "flight"
(integer) 1
> SADD superpowers "x-ray vision"
(integer) 1
> SADD superpowers "reflexes"
(integer) 1
> SREM superpowers "reflexes"
1
> NEXT
SISMEMBER tests if the given value is in the set. It returns 1 if the value is there and 0 if it is not.
SISMEMBER superpowers "flight" => 1
SISMEMBER superpowers "reflexes" => 0
SMEMBERS returns a list of all the members of this set.
SMEMBERS superpowers => 1) "flight", 2) "x-ray vision"
SUNION combines two or more sets and returns the list of all elements.
SADD birdpowers "pecking"
SADD birdpowers "flight"
SUNION superpowers birdpowers => 1) "pecking", 2) "x-ray vision", 3) "flight"
Type NEXT to continue the tutorial.
> SISMEMBER superpowers "flight"
(integer) 1
> SISMEMBER superpowers "reflexes"
(integer) 0
> SMEMBERS superpowers
1) "flight"
2) "x-ray vision"
> SADD birdpowers "pecking"
true/false
/member
true/false
/member
true/false
/member
true/false
/member
16 11 22 12:32Try Redis
8 11http: try.redis.io
(integer) 1
> SADD birdpowers "flight"
(integer) 1
> SUNION superpowers birdpowers
1) "flight"
2) "pecking"
3) "x-ray vision"
> NEXT
Sets are a very handy data type, but as they are unsorted they don't work well for a number of
problems. This is why Redis 1.2 introduced Sorted Sets.
A sorted set is similar to a regular set, but now each value has an associated score. This score
is used to sort the elements in the set.
ZADD hackers 1940 "Alan Kay"
ZADD hackers 1906 "Grace Hopper"
ZADD hackers 1953 "Richard Stallman"
ZADD hackers 1965 "Yukihiro Matsumoto"
ZADD hackers 1916 "Claude Shannon"
ZADD hackers 1969 "Linus Torvalds"
ZADD hackers 1957 "Sophie Wilson"
ZADD hackers 1912 "Alan Turing"
In these examples, the scores are years of birth and the values are the names of famous
hackers.
ZRANGE hackers 2 4 => 1) "Claude Shannon", 2) "Alan Kay", 3) "Richard Stallman"
Type NEXT to continue the tutorial.
> ZADD hackers 1940 "Alan Kay"
(integer) 1
> ZADD hackers 1906 "Grace Hopper"
(integer) 1
> ZADD hackers 1953 "Richard Stallman"
(integer) 1
> ZADD hackers 1965 "Yukihiro Matsumoto"
(integer) 1
> ZADD hackers 1916 "Claude Shannon"
<key, {<member, score>}>
/score
Top N
<key, {<member, score>}><key, {<member, score>}>
/score
Top N
<key, {<member, score>}>
16 11 22 12:32Try Redis
9 11http: try.redis.io
> ZADD hackers 1916 "Claude Shannon"
(integer) 1
> ZADD hackers 1969 "Linus Torvalds"
(integer) 1
> ZADD hackers 1957 "Sophie Wilson"
(integer) 1
> ZADD hackers 1912 "Alan Turing"
(integer) 1
> ZRANGE hackers 2 4
1) "Claude Shannon"
2) "Alan Kay"
3) "Richard Stallman"
> NEXT
Simple strings, sets and sorted sets already get a lot done but there is one more data type
Redis can handle: Hashes.
Hashes are maps between string fields and string values, so they are the perfect data type to
represent objects (eg: A User with a number of fields like name, surname, age, and so forth):
HSET user:1000 name "John Smith"
HSET user:1000 email "john.smith@example.com"
HSET user:1000 password "s3cret"
To get back the saved data use HGETALL:
HGETALL user:1000
You can also set multiple fields at once:
HMSET user:1001 name "Mary Jones" password "hidden" email "mjones@example.com"
If you only need a single field value that is possible as well:
HGET user:1001 name => "Mary Jones"
Type NEXT to continue the tutorial.
> HSET user:1000 name "John Smith"
> HSET user:1000 name "John Smith"
(integer) 0
> HSET user:1000 email "john.smith@example.com"
<key, {<field, value>}>
HMSET/HMGET
ZRANGE key start stop
ZRANGEBYSCORE key min max
<key, {<field, value>}>
HMSET/HMGET
ZRANGE key start stop
ZRANGEBYSCORE key min max
<key, {<field, value>}>
HMSET/HMGET
ZRANGE key start stop
ZRANGEBYSCORE key min max
<key, {<field, value>}>
HMSET/HMGET
ZRANGE key start stop
ZRANGEBYSCORE key min max
16 11 22 12:32Try Redis
10 11http: try.redis.io
> HSET user:1000 email "john.smith@example.com"
(integer) 1
> HSET user:1000 password "s3cret"
(integer) 1
> HGETALL user:1000
1) "name"
2) "John Smith"
3) "email"
4) "john.smith@example.com"
5) "password"
6) "s3cret"
> HMSET user:1001 name "Mary Jones" password "hidden" email
"mjones@example.com"
OK
> HGET user:1001 name
"Mary Jones"
> NEXT
Numerical values in hash fields are handled exactly the same as in simple strings and there are
operations to increment this value in an atomic way.
HSET user:1000 visits 10
HINCRBY user:1000 visits 1 => 11
HINCRBY user:1000 visits 10 => 21
HDEL user:1000 visits
HINCRBY user:1000 visits 1 => 1
Check the full list of Hash commands for more information.
Type NEXT to continue the tutorial.
> HSET user:1000 visits 10
(integer) 1
> HINCRBY user:1000 visits 1
(integer) 11
> HINCRBY user:1000 visits 10
(integer) 21
/field/field
16 11 22 12:32Try Redis
11 11http: try.redis.io
> HDEL user:1000 visits
(integer) 1
> HINCRBY user:1000 visits 1
(integer) 1
> NEXT
That wraps up the Try Redis tutorial. Please feel free to goof around with this console as much
as you'd like.
Check out the following links to continue learning about Redis.
Redis Documentation
Command Reference
Implement a Twitter Clone in Redis
Introduction to Redis Data Types
This site was originally written by Alex McHale (github twitter) and inspired by Try Mongo. It's now maintained and hosted
by Jan-Erik Rediger (github twitter)
The source code to Try Redis is available on GitHub.

More Related Content

What's hot

Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
Anton Arhipov
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
Attila Balazs
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎
anzhong70
 

What's hot (20)

Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
 
JavaFX, because you're worth it
JavaFX, because you're worth itJavaFX, because you're worth it
JavaFX, because you're worth it
 
Introduction to JavaFX 2
Introduction to JavaFX 2Introduction to JavaFX 2
Introduction to JavaFX 2
 
Wx::Perl::Smart
Wx::Perl::SmartWx::Perl::Smart
Wx::Perl::Smart
 
The Ring programming language version 1.10 book - Part 92 of 212
The Ring programming language version 1.10 book - Part 92 of 212The Ring programming language version 1.10 book - Part 92 of 212
The Ring programming language version 1.10 book - Part 92 of 212
 
SecureSocial - Authentication for Play Framework
SecureSocial - Authentication for Play FrameworkSecureSocial - Authentication for Play Framework
SecureSocial - Authentication for Play Framework
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
Google Guava
Google GuavaGoogle Guava
Google Guava
 
Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6
 
C99
C99C99
C99
 
P3 2018 python_regexes
P3 2018 python_regexesP3 2018 python_regexes
P3 2018 python_regexes
 
Stop Guessing and Start Measuring - Benchmarking in Practice (Lambdadays)
Stop Guessing and Start Measuring - Benchmarking in Practice (Lambdadays)Stop Guessing and Start Measuring - Benchmarking in Practice (Lambdadays)
Stop Guessing and Start Measuring - Benchmarking in Practice (Lambdadays)
 
Generic lazy class
Generic lazy classGeneric lazy class
Generic lazy class
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
 
第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎
 
Frege is a Haskell for the JVM
Frege is a Haskell for the JVMFrege is a Haskell for the JVM
Frege is a Haskell for the JVM
 
Linq introduction
Linq introductionLinq introduction
Linq introduction
 
Nantes Jug - Java 7
Nantes Jug - Java 7Nantes Jug - Java 7
Nantes Jug - Java 7
 

Viewers also liked

A project report on awareness of mutual funds 1
A project report on awareness of mutual funds 1A project report on awareness of mutual funds 1
A project report on awareness of mutual funds 1
Nirali Nayi
 
Mobile Finance: 2017 Trends and Innovations
Mobile Finance: 2017 Trends and InnovationsMobile Finance: 2017 Trends and Innovations
Mobile Finance: 2017 Trends and Innovations
Corporate Insight
 
Massolino_tesiMCS_VideoSismologiaSocial_04.01.2017
Massolino_tesiMCS_VideoSismologiaSocial_04.01.2017Massolino_tesiMCS_VideoSismologiaSocial_04.01.2017
Massolino_tesiMCS_VideoSismologiaSocial_04.01.2017
Giulia Massolino
 

Viewers also liked (20)

What is A Cloud Stack in 2017
What is A Cloud Stack in 2017What is A Cloud Stack in 2017
What is A Cloud Stack in 2017
 
Design in Tech Report 2017
Design in Tech Report 2017Design in Tech Report 2017
Design in Tech Report 2017
 
Europe ai scaleups report 2016
Europe ai scaleups report 2016Europe ai scaleups report 2016
Europe ai scaleups report 2016
 
The Benefits of Cloud Computing
The Benefits of Cloud ComputingThe Benefits of Cloud Computing
The Benefits of Cloud Computing
 
Comparing 30 MongoDB operations with Oracle SQL statements
Comparing 30 MongoDB operations with Oracle SQL statementsComparing 30 MongoDB operations with Oracle SQL statements
Comparing 30 MongoDB operations with Oracle SQL statements
 
Sudhir hadoop and Data warehousing resume
Sudhir hadoop and Data warehousing resume Sudhir hadoop and Data warehousing resume
Sudhir hadoop and Data warehousing resume
 
A project report on awareness of mutual funds 1
A project report on awareness of mutual funds 1A project report on awareness of mutual funds 1
A project report on awareness of mutual funds 1
 
Mobile Finance: 2017 Trends and Innovations
Mobile Finance: 2017 Trends and InnovationsMobile Finance: 2017 Trends and Innovations
Mobile Finance: 2017 Trends and Innovations
 
Getting Started with AWS
Getting Started with AWSGetting Started with AWS
Getting Started with AWS
 
IBM Storage for Analytics, Cognitive and Cloud
IBM Storage for Analytics, Cognitive and CloudIBM Storage for Analytics, Cognitive and Cloud
IBM Storage for Analytics, Cognitive and Cloud
 
Massolino_tesiMCS_VideoSismologiaSocial_04.01.2017
Massolino_tesiMCS_VideoSismologiaSocial_04.01.2017Massolino_tesiMCS_VideoSismologiaSocial_04.01.2017
Massolino_tesiMCS_VideoSismologiaSocial_04.01.2017
 
Khalil khan (it engineer resume)
Khalil khan (it engineer resume)Khalil khan (it engineer resume)
Khalil khan (it engineer resume)
 
Comparing approaches: Running database workloads on Dell EMC and Microsoft hy...
Comparing approaches: Running database workloads on Dell EMC and Microsoft hy...Comparing approaches: Running database workloads on Dell EMC and Microsoft hy...
Comparing approaches: Running database workloads on Dell EMC and Microsoft hy...
 
S4 1610 business value l1
S4 1610 business value l1S4 1610 business value l1
S4 1610 business value l1
 
AgensGraph: a Multi-model Graph Database based on PostgreSql
AgensGraph: a Multi-model Graph Database based on PostgreSqlAgensGraph: a Multi-model Graph Database based on PostgreSql
AgensGraph: a Multi-model Graph Database based on PostgreSql
 
Databases
DatabasesDatabases
Databases
 
Getting started with microsoft azure in 30 mins
Getting started with microsoft azure in 30 minsGetting started with microsoft azure in 30 mins
Getting started with microsoft azure in 30 mins
 
Ignorance is bliss, but not for MongoDB
Ignorance is bliss, but not for MongoDBIgnorance is bliss, but not for MongoDB
Ignorance is bliss, but not for MongoDB
 
DATA SCIENCE IS CATALYZING BUSINESS AND INNOVATION
DATA SCIENCE IS CATALYZING BUSINESS AND INNOVATION DATA SCIENCE IS CATALYZING BUSINESS AND INNOVATION
DATA SCIENCE IS CATALYZING BUSINESS AND INNOVATION
 
Europa AI startup scaleups report 2016
Europa AI startup scaleups report 2016 Europa AI startup scaleups report 2016
Europa AI startup scaleups report 2016
 

Similar to Try Redis - interactive Tutorial

python chapter 1
python chapter 1python chapter 1
python chapter 1
Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
Raghu nath
 

Similar to Try Redis - interactive Tutorial (20)

Redis is not just a cache, Andrew Lavers, ConFoo Montreal 2020
Redis is not just a cache, Andrew Lavers, ConFoo Montreal 2020Redis is not just a cache, Andrew Lavers, ConFoo Montreal 2020
Redis is not just a cache, Andrew Lavers, ConFoo Montreal 2020
 
Redis Set Go
Redis Set GoRedis Set Go
Redis Set Go
 
Introduction to redis
Introduction to redisIntroduction to redis
Introduction to redis
 
Redis
RedisRedis
Redis
 
Redis basics
Redis basicsRedis basics
Redis basics
 
Redis Lua Scripts
Redis Lua ScriptsRedis Lua Scripts
Redis Lua Scripts
 
Intro to Redis
Intro to RedisIntro to Redis
Intro to Redis
 
Redis introduction
Redis introductionRedis introduction
Redis introduction
 
Elixir in a nutshell - Fundamental Concepts
Elixir in a nutshell - Fundamental ConceptsElixir in a nutshell - Fundamental Concepts
Elixir in a nutshell - Fundamental Concepts
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
A limited guide to intermediate and advanced Ruby
A limited guide to intermediate and advanced RubyA limited guide to intermediate and advanced Ruby
A limited guide to intermediate and advanced Ruby
 
REDIS intro and how to use redis
REDIS intro and how to use redisREDIS intro and how to use redis
REDIS intro and how to use redis
 
Apache Spark - Basics of RDD & RDD Operations | Big Data Hadoop Spark Tutoria...
Apache Spark - Basics of RDD & RDD Operations | Big Data Hadoop Spark Tutoria...Apache Spark - Basics of RDD & RDD Operations | Big Data Hadoop Spark Tutoria...
Apache Spark - Basics of RDD & RDD Operations | Big Data Hadoop Spark Tutoria...
 
Erlang kickstart
Erlang kickstartErlang kickstart
Erlang kickstart
 
Introducing redis
Introducing redisIntroducing redis
Introducing redis
 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
 
Tuga IT 2017 - Redis
Tuga IT 2017 - RedisTuga IT 2017 - Redis
Tuga IT 2017 - Redis
 
Redis Indices (#RedisTLV)
Redis Indices (#RedisTLV)Redis Indices (#RedisTLV)
Redis Indices (#RedisTLV)
 
Java 8 - project lambda
Java 8 - project lambdaJava 8 - project lambda
Java 8 - project lambda
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 

Try Redis - interactive Tutorial

  • 1. 16 11 22 12:32Try Redis 1 11http: try.redis.io Welcome to Try Redis, a demonstration of the Redis database! Please type TUTORIAL to begin a brief tutorial, HELP to see a list of supported commands, or any valid Redis command to play with the database. > HELP Please type HELP for one of these commands: DECR, DECRBY, DEL, EXISTS, EXPIRE, GET, GETSET, HDEL, HEXISTS, HGET, HGETALL, HINCRBY, HKEYS, HLEN, HMGET, HMSET, HSET, HVALS, INCR, INCRBY, KEYS, LINDEX, LLEN, LPOP, LPUSH, LRANGE, LREM, LSET, LTRIM, MGET, MSET, MSETNX, MULTI, PEXPIRE, RENAME, RENAMENX, RPOP, RPOPLPUSH, RPUSH, SADD, SCARD, SDIFF, SDIFFSTORE, SET, SETEX, SETNX, SINTER, SINTERSTORE, SISMEMBER, SMEMBERS, SMOVE, SORT, SPOP, SRANDMEMBER, SREM, SUNION, SUNIONSTORE, TTL, TYPE, ZADD, ZCARD, ZCOUNT, ZINCRBY, ZRANGE, ZRANGEBYSCORE, ZRANK, ZREM, ZREMRANGEBYSCORE, ZREVRANGE, ZSCORE > TUTORIAL Redis is what is called a key-value store, often referred to as a NoSQL database. The essence of a key-value store is the ability to store some data, called a value, inside a key. This data can later be retrieved only if we know the exact key used to store it. We can use the command SET to store the value "fido" at key "server:name": SET server:name "fido" Redis will store our data permanently, so we can later ask "What is the value stored at key server:name?" and Redis will reply with "fido": GET server:name => "fido" Tip: You can click the commands above to automatically execute them. The text after the arrow (=>) shows the expected output. Type NEXT to continue the tutorial. > SET server:name "fido" OK > GET server:name "fido" <key, value> MGET
  • 2. 16 11 22 12:32Try Redis 2 11http: try.redis.io > NEXT Other common operations provided by key-value stores are DEL to delete a given key and associated value, SET-if-not-exists (called SETNX on Redis) that sets a key only if it does not already exist, and INCR to atomically increment a number stored at a given key: SET connections 10 INCR connections => 11 INCR connections => 12 DEL connections INCR connections => 1 Type NEXT to continue the tutorial. > SET connections 10 OK > INCR connections (integer) 11 > INCR connections (integer) 12 > DEL connections (integer) 1 > INCR connections (integer) 1 > SETNX connections 10 (integer) 0 > GET connections "1" > DEL connections (integer) 1 > SETNX connections 10 (integer) 1 > GET connections SETNX+EXPIRE/SET INCRBY INCRBYFLOAT INCR DECRBY DECR INCRBY / /
  • 3. 16 11 22 12:32Try Redis 3 11http: try.redis.io "10" > NEXT There is something special about INCR. Why do we provide such an operation if we can do it ourself with a bit of code? After all it is as simple as: x = GET count x = x + 1 SET count x The problem is that doing the increment in this way will only work as long as there is a single client using the key. See what happens if two clients are accessing this key at the same time: 1. Client A reads count as 10. 2. Client B reads count as 10. 3. Client A increments 10 and sets count to 11. 4. Client B increments 10 and sets count to 11. We wanted the value to be 12, but instead it is 11! This is because incrementing the value in this way is not an atomic operation. Calling the INCR command in Redis will prevent this from happening, because it is an atomic operation. Redis provides many of these atomic operations on different types of data. Type NEXT to continue the tutorial. > NEXT Redis can be told that a key should only exist for a certain length of time. This is accomplished with the EXPIRE and TTL commands. SET resource:lock "Redis Demo" EXPIRE resource:lock 120 This causes the key resource:lock to be deleted in 120 seconds. You can test how long a key will exist with the TTL command. It returns the number of seconds until it will be deleted. TTL resource:lock => 113 // after 113s TTL resource:lock => -2 The -2 for the TTL of the key means that the key does not exist (anymore). A -1 for the TTL of the key means that it will never expire. Note that if you SET a key, its TTL will be reset. SET resource:lock "Redis Demo 1" EXPIRE resource:lock 120 TTL resource:lock => 119 Key / / Key (persistent) vs (volatile) time to live /value /key Key / / Key (persistent) vs (volatile) time to live /value /key
  • 4. 16 11 22 12:32Try Redis 4 11http: try.redis.io SET resource:lock "Redis Demo 2" TTL resource:lock => -1 Type NEXT to continue the tutorial. > SET resource:lock "Redis Demo" OK > EXPIRE resource:lock 20 (integer) 1 > TTL resource:lock (integer) 4 > TTL resource:lock (integer) -2 > SET resource:lock "Redis Demo 1" OK > EXPIRE resource:lock 120 (integer) 1 > TTL resource:lock (integer) 117 > SET resource:lock "Redis Demo 2" OK > TTL resource:lock (integer) -1 > NEXT Redis also supports several more complex data structures. The first one we'll look at is a list. A list is a series of ordered values. Some of the important commands for interacting with lists are RPUSH, LPUSH, LLEN, LRANGE, LPOP, and RPOP. You can immediately begin working with a key as a list, as long as it doesn't already exist as a different type. RPUSH puts the new value at the end of the list. RPUSH friends "Alice" RPUSH friends "Bob" <key, [value]> DEL friends / key MSET ( ) <key, [value]> DEL friends / key MSET ( )
  • 5. 16 11 22 12:32Try Redis 5 11http: try.redis.io LPUSH puts the new value at the start of the list. LPUSH friends "Sam" LRANGE gives a subset of the list. It takes the index of the first element you want to retrieve as its first parameter and the index of the last element you want to retrieve as its second parameter. A value of -1 for the second parameter means to retrieve elements until the end of the list. LRANGE friends 0 -1 => 1) "Sam", 2) "Alice", 3) "Bob" LRANGE friends 0 1 => 1) "Sam", 2) "Alice" LRANGE friends 1 2 => 1) "Alice", 2) "Bob" Type NEXT to continue the tutorial. > DEL friends (integer) 1 > RPUSH friends "Alice" (integer) 1 > RPUSH friends "Bob" (integer) 2 > LPUSH friends "Sam" (integer) 3 > LRANGE friends 0 -1 1) "Sam" 2) "Alice" 3) "Bob" > LRANGE friends 0 1 1) "Sam" 2) "Alice" > LRANGE friends 1 2 1) "Alice" 2) "Bob" > NEXT LLEN returns the current length of the list. / [start, stop] / [start, stop] / [start, stop] / [start, stop]
  • 6. 16 11 22 12:32Try Redis 6 11http: try.redis.io LLEN friends => 3 LPOP removes the first element from the list and returns it. LPOP friends => "Sam" RPOP removes the last element from the list and returns it. RPOP friends => "Bob" Note that the list now only has one element: LLEN friends => 1 LRANGE friends 0 -1 => 1) "Alice" Type NEXT to continue the tutorial. > LLEN friends (integer) 3 > LPOP friends "Sam" > RPOP friends "Bob" > LLEN friends (integer) 1 > LRANGE friends 0 -1 1) "Alice" > NEXT The next data structure that we'll look at is a set. A set is similar to a list, except it does not have a specific order and each element may only appear once. Some of the important commands in working with sets are SADD, SREM, SISMEMBER, SMEMBERS and SUNION. SADD adds the given value to the set. SADD superpowers "flight" SADD superpowers "x-ray vision" SADD superpowers "reflexes" SREM removes the given value from the set. <key, {member}> ( ) SINTER( ) SDIFF( ) / ( ) / BRPOP/BLPOP key timeout RPOPLPUSH/BRPOPLPUSH <key, {member}> ( ) SINTER( ) SDIFF( ) / ( ) / BRPOP/BLPOP key timeout RPOPLPUSH/BRPOPLPUSH <key, {member}> ( ) SINTER( ) SDIFF( ) / ( ) / BRPOP/BLPOP key timeout RPOPLPUSH/BRPOPLPUSH <key, {member}> ( ) SINTER( ) SDIFF( ) / ( ) / BRPOP/BLPOP key timeout RPOPLPUSH/BRPOPLPUSH
  • 7. 16 11 22 12:32Try Redis 7 11http: try.redis.io SREM superpowers "reflexes" Type NEXT to continue the tutorial. > SADD superpowers "flight" (integer) 1 > SADD superpowers "x-ray vision" (integer) 1 > SADD superpowers "reflexes" (integer) 1 > SREM superpowers "reflexes" 1 > NEXT SISMEMBER tests if the given value is in the set. It returns 1 if the value is there and 0 if it is not. SISMEMBER superpowers "flight" => 1 SISMEMBER superpowers "reflexes" => 0 SMEMBERS returns a list of all the members of this set. SMEMBERS superpowers => 1) "flight", 2) "x-ray vision" SUNION combines two or more sets and returns the list of all elements. SADD birdpowers "pecking" SADD birdpowers "flight" SUNION superpowers birdpowers => 1) "pecking", 2) "x-ray vision", 3) "flight" Type NEXT to continue the tutorial. > SISMEMBER superpowers "flight" (integer) 1 > SISMEMBER superpowers "reflexes" (integer) 0 > SMEMBERS superpowers 1) "flight" 2) "x-ray vision" > SADD birdpowers "pecking" true/false /member true/false /member true/false /member true/false /member
  • 8. 16 11 22 12:32Try Redis 8 11http: try.redis.io (integer) 1 > SADD birdpowers "flight" (integer) 1 > SUNION superpowers birdpowers 1) "flight" 2) "pecking" 3) "x-ray vision" > NEXT Sets are a very handy data type, but as they are unsorted they don't work well for a number of problems. This is why Redis 1.2 introduced Sorted Sets. A sorted set is similar to a regular set, but now each value has an associated score. This score is used to sort the elements in the set. ZADD hackers 1940 "Alan Kay" ZADD hackers 1906 "Grace Hopper" ZADD hackers 1953 "Richard Stallman" ZADD hackers 1965 "Yukihiro Matsumoto" ZADD hackers 1916 "Claude Shannon" ZADD hackers 1969 "Linus Torvalds" ZADD hackers 1957 "Sophie Wilson" ZADD hackers 1912 "Alan Turing" In these examples, the scores are years of birth and the values are the names of famous hackers. ZRANGE hackers 2 4 => 1) "Claude Shannon", 2) "Alan Kay", 3) "Richard Stallman" Type NEXT to continue the tutorial. > ZADD hackers 1940 "Alan Kay" (integer) 1 > ZADD hackers 1906 "Grace Hopper" (integer) 1 > ZADD hackers 1953 "Richard Stallman" (integer) 1 > ZADD hackers 1965 "Yukihiro Matsumoto" (integer) 1 > ZADD hackers 1916 "Claude Shannon" <key, {<member, score>}> /score Top N <key, {<member, score>}><key, {<member, score>}> /score Top N <key, {<member, score>}>
  • 9. 16 11 22 12:32Try Redis 9 11http: try.redis.io > ZADD hackers 1916 "Claude Shannon" (integer) 1 > ZADD hackers 1969 "Linus Torvalds" (integer) 1 > ZADD hackers 1957 "Sophie Wilson" (integer) 1 > ZADD hackers 1912 "Alan Turing" (integer) 1 > ZRANGE hackers 2 4 1) "Claude Shannon" 2) "Alan Kay" 3) "Richard Stallman" > NEXT Simple strings, sets and sorted sets already get a lot done but there is one more data type Redis can handle: Hashes. Hashes are maps between string fields and string values, so they are the perfect data type to represent objects (eg: A User with a number of fields like name, surname, age, and so forth): HSET user:1000 name "John Smith" HSET user:1000 email "john.smith@example.com" HSET user:1000 password "s3cret" To get back the saved data use HGETALL: HGETALL user:1000 You can also set multiple fields at once: HMSET user:1001 name "Mary Jones" password "hidden" email "mjones@example.com" If you only need a single field value that is possible as well: HGET user:1001 name => "Mary Jones" Type NEXT to continue the tutorial. > HSET user:1000 name "John Smith" > HSET user:1000 name "John Smith" (integer) 0 > HSET user:1000 email "john.smith@example.com" <key, {<field, value>}> HMSET/HMGET ZRANGE key start stop ZRANGEBYSCORE key min max <key, {<field, value>}> HMSET/HMGET ZRANGE key start stop ZRANGEBYSCORE key min max <key, {<field, value>}> HMSET/HMGET ZRANGE key start stop ZRANGEBYSCORE key min max <key, {<field, value>}> HMSET/HMGET ZRANGE key start stop ZRANGEBYSCORE key min max
  • 10. 16 11 22 12:32Try Redis 10 11http: try.redis.io > HSET user:1000 email "john.smith@example.com" (integer) 1 > HSET user:1000 password "s3cret" (integer) 1 > HGETALL user:1000 1) "name" 2) "John Smith" 3) "email" 4) "john.smith@example.com" 5) "password" 6) "s3cret" > HMSET user:1001 name "Mary Jones" password "hidden" email "mjones@example.com" OK > HGET user:1001 name "Mary Jones" > NEXT Numerical values in hash fields are handled exactly the same as in simple strings and there are operations to increment this value in an atomic way. HSET user:1000 visits 10 HINCRBY user:1000 visits 1 => 11 HINCRBY user:1000 visits 10 => 21 HDEL user:1000 visits HINCRBY user:1000 visits 1 => 1 Check the full list of Hash commands for more information. Type NEXT to continue the tutorial. > HSET user:1000 visits 10 (integer) 1 > HINCRBY user:1000 visits 1 (integer) 11 > HINCRBY user:1000 visits 10 (integer) 21 /field/field
  • 11. 16 11 22 12:32Try Redis 11 11http: try.redis.io > HDEL user:1000 visits (integer) 1 > HINCRBY user:1000 visits 1 (integer) 1 > NEXT That wraps up the Try Redis tutorial. Please feel free to goof around with this console as much as you'd like. Check out the following links to continue learning about Redis. Redis Documentation Command Reference Implement a Twitter Clone in Redis Introduction to Redis Data Types This site was originally written by Alex McHale (github twitter) and inspired by Try Mongo. It's now maintained and hosted by Jan-Erik Rediger (github twitter) The source code to Try Redis is available on GitHub.