SlideShare a Scribd company logo
Aniruddha Chakrabarti
AVP and Solution Lead, Digital Practice, Mphasis
ani.c@outlook.com | Linkedin.com/in/aniruddhac | slideshare.net/aniruddha.chakrabarti/ | Twitter - anchakra
redis and it’s data types
http://redis.io/
Agenda
• What is redis (and what it’s not)
• Popularity of redis, who uses redis, history of redis
• Type of NoSQL databases, NoSQL history
• Different type of data structures supported by redis
• String
• List
• Set
• Hash
• Sorted Set / ZSET
• redis CLI commands
• Database
• Expiry
• Transactions
• Mass insert / Bulk upload
What is redis
• Redis is a Key Value NoSQL database
• Open source (BSD licensed)
• In memory data structure store. All data is served from memory
• Redis mantra - data served from memory, disk used for storage
• Offers high performance, replication, and a unique data model
• Supports five different data structures - strings, lists, sets, hashes, sorted sets
(as value) – redis is also called data structure server
• Used as database, cache and message broker.
• Actually stands for REmote DIctionary Server
• Redis is often compared to memcached, which is a very high performance, key-
value cache server.
• Supports built in replication, Lua scripting, on disk persistence, limited transaction
• Written in ANSI C, supports multiple platform
Key1 Val1
Key2 V2,V3
Key3 Val3
What redis is not
• redis is not a RDBMS
• Does not support Schema
• Does not support Joins (Stored Procs, Triggers etc)
• Does not support ACID Transactions, though supports limited transactions
• Does not support SQL
NoSQL History
• 1998 | Carlo Strozzi used the term NoSQL to name his lightweight, open-source
relational database that did not expose the standard SQL interface.
• 2000 | Graph database Neo4j started
• 2004 | Google BigTable is started in 2004. Paper published in 2006.
• 2005 | CouchDB development started.
• 2007 | Research paper on Amazon Dynamo released (not AWS DynamoDB)
• 2007 | MongoDB started as a part of a open source cloud computing stack and
first standalone release in 2009.
• 2008 | Facebook open sources the Cassandra project
• 2008 | Project Voldemort started
• 2009 | The term NoSQL was reintroduced by Eric Evans of rackspace. Redis initial
release
• 2010 | Some NoSQL conferences NoSQL Matters, NoSQL Now!, INOSA
redis History
• Early 2009 - Redis project was started in early 2009 by an Italian developer
named Salvatore Sanfilippo. Redis was initially written to improve the performance
of LLOOGG, a real-time web analytics product out of Salvatore’s startup.
• June 2009 - Redis was stable, and had enough of a base feature set, to serve
production traffic at LLOOGG (retired the MySQL installation)
• Redis rapidly grew in popularity. Salvatore fostered a great community, added
features at a very rapid pace, and dealt with bugs.
• March 2010 - VMWare hired Salvatore to work full-time on Redis. (Redis itself
remains BSD licensed.) VMWare hired Pieter Noordhuis, a key Redis contributor,
to give the project an additional momentum boost.
• December 2012 - VMWare and EMC spins off Pivotal which would focus on Big
Data and Cloud. Redis related effort moves to Pivotal.
• June 2015 - Redis Labs started sponsoring the development of Redis
Popularity of redis
Source: http://db-engines.com/en/ranking
• Redis is 10th among all
Databases products including
RDBMS & NoSQL
• Third popular NoSQL
database (after MongoDB and
Cassandra)
• Most popular Key Value
store
Who uses redis
Source - http://techstacks.io/
React Chat
Redis Admin UI
Flickr
Instagram
Airbnb
Alibaba
Medium
Pintrest
Shopify
Square
Tumblr
Twitter
UserVoice
Vine
Craigslist
Stripe
rdio
Hulu
GitHub
Disqus
BitBucket
Parse
Songtive
StackOverflow
Lanyrd
Grooveshark
MixRadio
Kickstarter
Trello
NewsBlur
Coinbase
Dashlane
DataSift
Imgur
Gluten Freedom
Recommend
AppLovin
N5 Tech
Mozello
Appknox
Natue
Redis Labs
A notable use case for Redis is at Twitter, where it is used to cache the latest few hundred tweets for every active user.
Types of NoSQL databases
• Redis
• MemcacheDB
• Riak
• Amazon DynamoDB
• Oracle NoSQL
database
• Berkley DB
• Azure Table Storage
• Voldermort
• Amazon SimpleDB
Key Value Store /
Tuple Store
• MongoDB
• CouchDB
• CouchBase
• RavenDB
• Azure DocumentDB
• IBM Cloudant
Document Store
• BigTable
• Hadoop, HBase
• Hortonworks,
Cloudera
• Cassandra
• Cloudata
• Accumolo
• Amazon SimleDB
• IBM Informix
• Google Cloud
Platform BigTable
Wide Column Store /
Column Family
• Neo4J
• OrientDB
• Titan
• HyperGraph DB
• FlockDB (Twitter)
Graph Database
Source - http://nosql-database.org/
Types of NoSQL databases
Key Value Store /
Tuple Store Document Store
Wide Column Store /
Column Family Graph Database
Key1 Val1
Key2 Val2
Key3 Val3
Key n Val n
{
id:1234,
item:"Tshirt",
size: 36,
price: 567.50
}
id:1234
item:"Tshirt",
size: 36,
price: 567.50
redis – Server and CLI/Client
redis-server.exe redis.windows.conf
• Runs on default port, 6379
D:RedisRedis-x64-2.8.2103>redis-cli
127.0.0.1:6379>
• Connects to default port, 6379 and awaits for commands
First set of commands
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> echo "hello from redis"
"hello from redis"
127.0.0.1:6379> quit
D:RedisRedis-x64-2.8.2103>
redis Data Structures
• Redis supports five different data structures
1. String
2. List
3. Set
4. Hash
5. Sorted Set / ZSET
• Best way to think about Redis is it’s a data structure server.
String
Operations Details
SET Sets the value stored at the given key
GET Fetches the data stored at the given key
DEL Deletes the value stored at the given key (works for all types)
SETNX Sets the value of a key, only if the key does not exist
MSET Sets the value of multiple keys
MGET Fetches the data of multiple keys
INCR Increase the integer value of a key by one
INCRBY Increase the integer value of a key by the given amount
DECR Decrease the integer value of a key by one
DECRBY Decrease the integer value of a key by the given amount
STRLEN Gets the length of the value stored in a key
• STRINGs are similar to strings that we see in other languages or other
key-value stores.
• String values could be added, read, deleted and updated for a key.
Key1 Val1
Key2 Val2
city bangalore
String
127.0.0.1:6379> set city bangalore
OK
127.0.0.1:6379> get city
"bangalore"
127.0.0.1:6379> append city ", India"
(integer) 16
127.0.0.1:6379> get city
"bangalore, India"
127.0.0.1:6379> del city
(integer) 1
127.0.0.1:6379> get city
(nil)
127.0.0.1:6379> mset key1 val1 key2 val2 key3 val3
OK
127.0.0.1:6379> mget key1 key2 key3
1) "val1"
2) "val2"
3) "val3“
127.0.0.1:6379> setnx city "den haag"
(integer) 1
127.0.0.1:6379> setnx city "blore"
(integer) 0
127.0.0.1:6379> strlen city
(integer) 8
String (Integer)
127.0.0.1:6379> set counter 100
OK
127.0.0.1:6379> get counter
"100"
127.0.0.1:6379> incr counter
(integer) 101
127.0.0.1:6379> get counter
"101"
127.0.0.1:6379> incrby counter 5
(integer) 106
127.0.0.1:6379> decr counter
(integer) 105
127.0.0.1:6379> decrby counter 10
(integer) 95
List
Operations Details
LPUSH Pushes the value onto the left end (head) of the list
RPUSH Pushes the value onto the right end (tail) of the list
LPOP Pops the value from the left end (head) of the list and returns it
RPOP Pops the value from the right end (tail) of the list and returns it
LRANGE Fetches a range of values from the list
LINDEX Fetches an item at a given position in the list
• Redis Lists are simply sequence of strings
• Ordered by insertion order
• Allows duplicates
• It is possible to add elements to a Redis List
pushing new elements on the head (on the
left) or on the tail (on the right) of the list.
• Implemented via Linked Lists
key1 val1 val2 val1
cities blore chennai mumbai mumbai
Key Value
cities blore, chennai, mumbai
roles admin, general_user, guest
weekends Saturday, Sunday
promoted_emps Bill, Satya, Satish, Steve
prime_nos 1,3,5,7,11,13,17,19
List – command examples
127.0.0.1:6379> rpush cities blore
(integer) 1
127.0.0.1:6379> rpush cities chennai
(integer) 2
127.0.0.1:6379> rpush cities mumbai
(integer) 3
127.0.0.1:6379> lrange cities 0 3
1) "blore"
2) "chennai"
3) "mumbai
127.0.0.1:6379> lrange cities 0 -1
1) "blore"
2) "chennai"
3) "mumbai"
127.0.0.1:6379> lindex cities 1
"chennai"
127.0.0.1:6379> rpush cities mumbai
(integer) 4
127.0.0.1:6379> lrange cities 0 4
1) "blore"
2) "chennai"
3) "mumbai"
4) "mumbai“
127.0.0.1:6379> lpush cities chennai
(integer) 5
127.0.0.1:6379> lrange cities 0 5
1) "chennai"
2) "blore"
3) "chennai"
4) "mumbai"
5) "mumbai"
127.0.0.1:6379>
List – command examples
127.0.0.1:6379> lrange cities 0 5
1) "chennai"
2) "blore"
3) "chennai"
4) "mumbai"
5) "mumbai"
127.0.0.1:6379> rpop cities
"mumbai"
127.0.0.1:6379> lrange cities 0 5
1) "chennai"
2) "blore"
3) "chennai"
4) "mumbai"
127.0.0.1:6379> rpop cities
"mumbai"
127.0.0.1:6379> lrange cities 0 5
1) "chennai"
2) "blore"
3) "chennai"
Set
Operations Details
SADD Adds the item to the set
SMEMBERS Returns the entire set of items
SISMEMBER Checks if an item is in the set
SREM Removes the item from the set, if it exists
• Similar to Lists, Redis Sets are simply
sequence of strings
• Does not allow duplicates.
• Redis SETs use a hash table to keep all
strings unique (though there are no associated
values).
• Redis SETs are unordered, so we can’t push
and pop items from the ends like LISTs.
key1 val1 val2 val3
unq_cities blore chennai mumbai mumbai
Set – command examples
127.0.0.1:6379> sadd ucities chennai kolkata blore
(integer) 3
127.0.0.1:6379> sadd ucities delhi
(integer) 1
127.0.0.1:6379> smembers ucities
1) "delhi"
2) "blore"
3) "kolkata"
4) "Chennai"
127.0.0.1:6379> sismember ucities blore
(integer) 1
127.0.0.1:6379> sismember ucities hyderabad
(integer) 0
127.0.0.1:6379> sadd ucities blore
(integer) 0
127.0.0.1:6379> srem ucities blore
(integer) 1
127.0.0.1:6379> smembers ucities
1) "kolkata"
2) "chennai"
3) "delhi"
127.0.0.1:6379> srem ucities blore
(integer) 0
Hash
Operations Details
HSET Stores the value at the key in the hash (single key and value)
HGET Fetches the value at the given hash key
HGETALL Fetches the entire hash
HDEL Removes a key from the hash, if it exists
HINCRBY Increases the value of the sub-key by the specified value
HKEYS Returns all sub-keys for a specific key
HVALS Returns all values for a specific key
HMSET Stores multiple key value pairs in the hash
HLEN Returns the number of fields in a hash
HEXISTS Determine if the hash field exists
• Whereas LISTs and SETs in Redis hold
sequences of items, Redis HASHes store a
mapping of keys to values
• Suitable for storing an object
key1
sub key1:
val1
sub key2:
val2
sub key2:
val3
users:Satya
name:
“Satya Nadela”
age:
45
email:
Satya.n@ms.com
Hash – command examples
127.0.0.1:6379> hset users:Sata name "Satya Nadela"
(integer) 1
127.0.0.1:6379> hset users:Sata age 45
(integer) 1
127.0.0.1:6379> hset users:Sata email satya.m@ms.com
(integer) 1
127.0.0.1:6379> hget users:Sata email
"satya.m@ms.com"
127.0.0.1:6379> hgetall users:Sata
1) "name"
2) "Satya Nadela"
3) "age"
4) "45"
5) "email"
6) "satya.m@ms.com"
127.0.0.1:6379> hdel users:Sata email
(integer) 1
127.0.0.1:6379> hgetall users:Sata
1) "name"
2) "Satya Nadela"
3) "age"
4) "45"
127.0.0.1:6379> hkeys users:Sata
1) "name"
2) "age"
127.0.0.1:6379> hvals users:Sata
1) "Satya Nadela"
2) "50"
Hash – command examples
127.0.0.1:6379> hincrby users:Sata age 5
(integer) 50
127.0.0.1:6379> hget users:Sata age
"50“
127.0.0.1:6379> hgetall users:Sata
1) "name"
2) "Satya Nadela"
3) "age"
4) "50"
127.0.0.1:6379> hlen users:Sata
(integer) 2
127.0.0.1:6379> hexists users:Sata name
(integer) 1
127.0.0.1:6379> hexists users:Sata email
(integer) 0
Hash – command examples
127.0.0.1:6379> hmset users:Sunder name "Sunder P" age 43 designation CEO
OK
127.0.0.1:6379> hmset users:XYZ name "XYZ PQR" age 21 designation Engineer email xyz@gmail.com mobile
"+(91) 12345 67890"
OK
127.0.0.1:6379> keys users*
1) "users:Sata"
2) "users:XYZ"
3) "users:Sunder"
Sorted Set (ZSET)
Operations Details
ZADD Adds member with the given score to the ZSET
ZRANGE Fetches the items in the ZSET from their positions in sorted order
ZRANGEBYSCORE Fetches items in the ZSET based on a range of scores
ZREM Removes the item from the ZSET, if it exists
ZCARD Returns the number of members in the ZSET
• Whereas LISTs and SETs in Redis hold sequences of items, Redis HASHes store a mapping of
keys to values
• ZSETs offer the ability to store a mapping of members to scores (similar to the keys and values of
HASHes). These mappings allow us to manipulate the numeric scores, 2 and fetch and scan over
both members and scores based on the sorted order of the scores.
Sorted Set (ZSET) – command examples
127.0.0.1:6379> zadd city_ranking 1 'New York'
(integer) 1
127.0.0.1:6379> zadd city_ranking 2 'Minneapolis'
(integer) 1
127.0.0.1:6379> zadd city_ranking 3 'Bangalore'
(integer) 1
127.0.0.1:6379> zadd city_ranking 4 'Amsterdam'
(integer) 1
127.0.0.1:6379> zadd city_ranking 5 'Warshaw'
(integer) 1
127.0.0.1:6379> zrange city_ranking 0 -1
1) "New York"
2) "Minneapolis"
3) "Bangalore"
4) "Amsterdam"
5) "Warshaw"
127.0.0.1:6379> zrangebyscore city_ranking 2 4
1) "Minneapolis"
2) "Bangalore"
3) "Amsterdam"
127.0.0.1:6379> zrem city_ranking bangalore
(integer) 0
127.0.0.1:6379> zrem city_ranking Bangalore
(integer) 1
127.0.0.1:6379> zrange city_ranking 0 -1
1) "New York"
2) "Minneapolis"
3) "Amsterdam"
4) "Warshaw“
127.0.0.1:6379> zcard city_ranking
(integer) 4
Other Commands
127.0.0.1:6379> keys *
1) "user:ani"
2) "users:Sata"
3) "ctr"
4) "ucities"
5) "friend"
6) "counter"
7) "cities“
127.0.0.1:6379> keys user*
1) "user:ani"
2) "users:Sata"
Server related commands
Operations Details
INFO Gets detailed information and statistics about the redis server like redis version, OS name,
Process Id, port, how long the server has been up, config file name, information about
connected clients, memory used by redis, persistence information, replication data, etc.
DBSIZE Returns the no of keys in the database
CLIENT LIST Gets the list of client connections
CLIENT SETNAME Sets the current connection name
CLIENT GETNAME Gets the current connection name
CLIENT KILL Kills the connection of a client
CONFIG GET Gets the value of a configuration parameter
CONFIG SET Sets the value of a configuration parameter
FLUSHDB Removes all keys from the current database
FLUSHALL Removes all keys from all databases
Server related commands
127.0.0.1:6379> info
# Server
redis_version:2.8.2103
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:2a4e86cac6830caf
redis_mode:standalone
os:Windows
arch_bits:64
multiplexing_api:winsock_IOCP
process_id:8724
run_id:5bbf82e6f0382362bfa0bf378cb87fa7109e6269
tcp_port:6379
uptime_in_seconds:153348
uptime_in_days:1
hz:10
lru_clock:16491038
config_file:D:RedisRedis-x64-2.8.2103redis.windows.conf
127.0.0.1:6379> dbsize
(integer) 9
Database in redis
127.0.0.1:6379> set city bangalore
OK
127.0.0.1:6379> use 1
(error) ERR unknown command 'use'
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> set city kolkata
OK
127.0.0.1:6379[1]> get city
"kolkata"
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> get city
"bangalore"
127.0.0.1:6379> dbsize
(integer) 14
• Different types of data, typically for different
application or even modules could be stored in
different redis Database.
• In Redis, databases are identified by an
integer index, not by a database name.
• By default, a client is connected to database 0
• To connect and switch to a different database
use SELECT (such as SELECT 3) – all
subsequent commands will then use database
3, until you issue another SELECT.
• Each Redis database has its own keyspace.
• The number of databases which is available
can be configured in redis.conf — by default, it
is set to 16. Simply set it to a higher number if
you need more
Expiry
127.0.0.1:6379> set emsg Hello
OK
127.0.0.1:6379> expire emsg 30
(integer) 1
127.0.0.1:6379> ttl emsg
(integer) 26
127.0.0.1:6379> get emsg
"Hello"
127.0.0.1:6379> ttl emsg
(integer) 16
127.0.0.1:6379> get emsg
"Hello"
127.0.0.1:6379> ttl emsg
(integer) 8
127.0.0.1:6379> get emsg
"Hello"
127.0.0.1:6379> ttl emsg
(integer) -2
127.0.0.1:6379> get emsg
(nil)
• Set a timeout on key. After the timeout has
expired, the key will automatically be
deleted.
• A key with an associated timeout is often
said to be volatile in Redis terminology.
Operations Details
EXPIRE Sets a key’s time to live or timeout. After
the timeout expires, the key gets deleted
TTL Returns the time to live (TTL) for a key
Transaction
• Set a timeout on key. After the timeout has expired, the key will automatically be deleted.
• A key with an associated timeout is often said to be volatile in Redis terminology.
Operations Details
MULTI Marks the start of a transaction block. Similar to Begin Transaction in SQL. All commands
issued after MULTI are not executed and are queued up.
EXEC Executes all commands issued after MULTI
DISCARD Discards all commands issued after MULTI
WATCH Watch the given keys to determine the execution of MULTI/EXEC block.
Transaction
127.0.0.1:6379> set debit_account 120000
OK
127.0.0.1:6379> set credit_account 40000
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> decrby debit_account 30000
QUEUED
127.0.0.1:6379> incrby credit_account 30000
QUEUED
127.0.0.1:6379> exec
1) (integer) 90000
2) (integer) 70000
127.0.0.1:6379>
127.0.0.1:6379> set debit_account 120000
OK
127.0.0.1:6379> set credit_account 40000
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> decrby debit_account 30000
QUEUED
127.0.0.1:6379> incrby credit_account 30000
QUEUED
127.0.0.1:6379> discard
OK
127.0.0.1:6379> get debit_account
"120000"
127.0.0.1:6379> get credit_account
"40000"
Mass insertion of data (bulk upload)
• Sometimes Redis instances needs to be loaded with big amount of preexisting or user generated
data in short amount of time, so that millions of keys will be created quickly - called mass insertion
• In 2.6 or later versions of Redis the redis-cli utility supports a new mode called pipe mode that was
designed in order to perform mass insertion.
Using the pipe mode the command to run looks like the following:
D:RedisRedis-x64-2.8.2103>type initial_data.txt | redis-cli --pipe
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 5
D:RedisRedis-x64-2.8.2103>redis-cli
127.0.0.1:6379> keys Key*
1) "Key0"
2) "Key4"
3) "Key3"
4) "Key2"
5) "Key1"
Install redis-py through pip
D:Pythonpython 3.4.3Scripts>pip install redis
You are using pip version 7.1.0, however version 7.1.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
Collecting redis
Downloading redis-2.10.3.tar.gz (86kB)
100% |################################| 90kB 375kB/s
Building wheels for collected packages: redis
Running setup.py bdist_wheel for redis
Stored in directory:
C:Usersaniruddha.cAppDataLocalpipCachewheelsb482097713ff9bc6f6a58e41b0a89433aed5d2293fd5868f76a
def94
Successfully built redis
Installing collected packages: redis
Successfully installed redis-2.10.3
Install redis client for node
• Redis Client for Node – NodeRedis (https://github.com/NodeRedis/node_redis)
http://redis.js.org/
npm install redis
Redis node – working with string
var redis = require('redis');
var client = redis.createClient();
var key = 'city', value = 'New York';
client.set(key, value, redis.print); // prints Reply: OK
client.get(key, function(error, data){
console.log(data); // prints New York
});
Redis node – incrementing
var key = 'age';
client.set(key, 35, function(error){
client.incr(key, function (error){
client.get(key, function(error, data){
console.log(data); // 36
client.incrby(key, 5, function (error){
client.get(key, function(error, data){
console.log(data); // 41
});
});
});
});
});
Redis node – decrementing
var key = 'age';
client.set(key, 35, function(error){
client.decr(key, function (error){
client.get(key, function(error, data){
console.log(data); // 34
client.decrby(key, 5, function (error){
client.get(key, function(error, data){
console.log(data); // 29
});
});
});
});
});
Redis node – working with hash (objects)
var redis = require('redis');
var client = redis.createClient();
var key = 'employee';
client.hmset(key, {
name:'Bill',
age:55,
designation:"Board of director"
});
client.hgetall(key, function (error, data){
console.log(data.name); // Bill
console.log(data.age); // 55
console.log(data.designation); // Board of director
});
Redis node – working with lists
var key = "framework";
client.lpush([key,'Angular','Backbone','Amber','Amber'], function (error){
if(! error){
client.lrange(key, 0, -1, function (error, data){
console.log(data); // ['Amber', 'Amber', 'Backbone', 'Angular']
});
}
});
var key = "framework";
client.rpush([key,'Angular','Backbone','Amber',‘Amber'], function (error){
if(! error){
client.lrange(key, 0, -1, function (error, data){
console.log(data); // ['Angular', 'Backbone', 'Amber', 'Amber' ]
});
}
});
Redis node – working with sets
var key = "frameworks";
client.sadd([key,'Angular','Backbone','Amber','Amber'], function (error){
if(! error){
client.smembers(key, function (error, data){
console.log(data); // [ 'Angular', 'Backbone', 'Amber' ]
});
}
});
Redis node – removing the key
var key = "frameworks";
client.exists(key, function(error, reply){
if(reply === 1){
console.log('exists');
client.del(key, function(error, reply){
if(!error){
console.log("key removed successfully");
}
});
}
else {
console.log('does not exist');
}
});

More Related Content

What's hot (20)

PPT
Introduction to redis
Tanu Siwag
 
PPTX
Introduction to Redis
Maarten Smeets
 
PPTX
Redis Introduction
Alex Su
 
PPTX
Redis database
Ñáwrás Ñzár
 
PDF
Redis
imalik8088
 
PPTX
mongodb와 mysql의 CRUD 연산의 성능 비교
Woo Yeong Choi
 
PDF
Redis
DaeMyung Kang
 
PDF
Redis Overview
Hoang Long
 
PPTX
Introduction to Redis
Arnab Mitra
 
PDF
MongodB Internals
Norberto Leite
 
PDF
Redis cluster
iammutex
 
PDF
Paris Redis Meetup Introduction
Gregory Boissinot
 
PDF
MongoDB Performance Tuning
Puneet Behl
 
PDF
Alphorm.com Formation le langage SQL
Alphorm
 
ODP
Redis overview
Ahmad El-khuja
 
PPTX
GitLab과 Kubernetes를 통한 CI/CD 구축
철구 김
 
PDF
An Introduction to Redis for Developers.pdf
Stephen Lorello
 
PPTX
Introduction to Apache ZooKeeper
Saurav Haloi
 
PDF
PostgreSQL WAL for DBAs
PGConf APAC
 
PPTX
Mongo DB 성능최적화 전략
Jin wook
 
Introduction to redis
Tanu Siwag
 
Introduction to Redis
Maarten Smeets
 
Redis Introduction
Alex Su
 
Redis database
Ñáwrás Ñzár
 
Redis
imalik8088
 
mongodb와 mysql의 CRUD 연산의 성능 비교
Woo Yeong Choi
 
Redis Overview
Hoang Long
 
Introduction to Redis
Arnab Mitra
 
MongodB Internals
Norberto Leite
 
Redis cluster
iammutex
 
Paris Redis Meetup Introduction
Gregory Boissinot
 
MongoDB Performance Tuning
Puneet Behl
 
Alphorm.com Formation le langage SQL
Alphorm
 
Redis overview
Ahmad El-khuja
 
GitLab과 Kubernetes를 통한 CI/CD 구축
철구 김
 
An Introduction to Redis for Developers.pdf
Stephen Lorello
 
Introduction to Apache ZooKeeper
Saurav Haloi
 
PostgreSQL WAL for DBAs
PGConf APAC
 
Mongo DB 성능최적화 전략
Jin wook
 

Viewers also liked (16)

PDF
Advanced Redis data structures
amix3k
 
PPTX
Twitter case study
divya_binu
 
PDF
المكتبة الزيتونية سمير باني
Minyar Sassi Hidri
 
PPTX
Using MongoDB As a Tick Database
MongoDB
 
PPT
MongoDB Tick Data Presentation
MongoDB
 
KEY
NoSQL at Twitter (NoSQL EU 2010)
Kevin Weil
 
PDF
Introduction aux bases de données NoSQL
Antoine Augusti
 
PDF
HTML5, Spring, NoSQL et mobilité
Julien Dubois
 
PDF
BigData_Chp4: NOSQL
Lilia Sfaxi
 
PDF
Les BD NoSQL
Minyar Sassi Hidri
 
PPTX
Découverte de Redis
JEMLI Fathi
 
PPTX
Les modèles NoSQL
ebiznext
 
PPTX
introduction à MongoDB
Abdoulaye Dieng
 
PDF
REX Storm Redis
Benjamin Houdu
 
PPTX
Les dessous du framework spring
Antoine Rey
 
PDF
Technologies pour le Big Data
Minyar Sassi Hidri
 
Advanced Redis data structures
amix3k
 
Twitter case study
divya_binu
 
المكتبة الزيتونية سمير باني
Minyar Sassi Hidri
 
Using MongoDB As a Tick Database
MongoDB
 
MongoDB Tick Data Presentation
MongoDB
 
NoSQL at Twitter (NoSQL EU 2010)
Kevin Weil
 
Introduction aux bases de données NoSQL
Antoine Augusti
 
HTML5, Spring, NoSQL et mobilité
Julien Dubois
 
BigData_Chp4: NOSQL
Lilia Sfaxi
 
Les BD NoSQL
Minyar Sassi Hidri
 
Découverte de Redis
JEMLI Fathi
 
Les modèles NoSQL
ebiznext
 
introduction à MongoDB
Abdoulaye Dieng
 
REX Storm Redis
Benjamin Houdu
 
Les dessous du framework spring
Antoine Rey
 
Technologies pour le Big Data
Minyar Sassi Hidri
 
Ad

Similar to Redis and it's data types (20)

PPT
Redis
ssuserbad56d
 
PPTX
REDIS327
Rajan Bhatt
 
PDF
quickguide-einnovator-9-redis
jorgesimao71
 
PDF
key value dbs , redis , cassandra , their architecture
VrajGaglani
 
PPTX
Introduction to Redis
Ofer Zelig
 
PDF
Redis - The Universal NoSQL Tool
Eberhard Wolff
 
PDF
Mini-Training: Redis
Betclic Everest Group Tech Team
 
PDF
#SydPHP - The Magic of Redis
Aaron Weatherall
 
PDF
Redis SoCraTes 2014
steffenbauer
 
PPTX
Redis
Rajesh Kumar
 
PPTX
Intro to Redis
Jeff Alstadt
 
KEY
Blazing Data With Redis (and LEGOS!)
Justin Carmony
 
PDF
Introduction to Redis
Dvir Volk
 
PDF
Redispresentation apac2012
Ankur Gupta
 
PPTX
Redis_Presentation.pptx ppt on redis and
tanishksaini51
 
PDF
Introduction to Redis
Saeid Zebardast
 
PPTX
Redis in 20 minutes
András Fehér
 
PPTX
Get more than a cache back! - ConFoo Montreal
Maarten Balliauw
 
PPTX
Redis Labcamp
Angelo Simone Scotto
 
PDF
Introduction to redis - version 2
Dvir Volk
 
REDIS327
Rajan Bhatt
 
quickguide-einnovator-9-redis
jorgesimao71
 
key value dbs , redis , cassandra , their architecture
VrajGaglani
 
Introduction to Redis
Ofer Zelig
 
Redis - The Universal NoSQL Tool
Eberhard Wolff
 
Mini-Training: Redis
Betclic Everest Group Tech Team
 
#SydPHP - The Magic of Redis
Aaron Weatherall
 
Redis SoCraTes 2014
steffenbauer
 
Intro to Redis
Jeff Alstadt
 
Blazing Data With Redis (and LEGOS!)
Justin Carmony
 
Introduction to Redis
Dvir Volk
 
Redispresentation apac2012
Ankur Gupta
 
Redis_Presentation.pptx ppt on redis and
tanishksaini51
 
Introduction to Redis
Saeid Zebardast
 
Redis in 20 minutes
András Fehér
 
Get more than a cache back! - ConFoo Montreal
Maarten Balliauw
 
Redis Labcamp
Angelo Simone Scotto
 
Introduction to redis - version 2
Dvir Volk
 
Ad

More from Aniruddha Chakrabarti (20)

PDF
Pinecone Vector Database.pdf
Aniruddha Chakrabarti
 
PDF
Mphasis-Annual-Report-2018.pdf
Aniruddha Chakrabarti
 
PDF
Thomas Cook and Accenture expand relationship with 10 year technology consult...
Aniruddha Chakrabarti
 
PDF
NLP using JavaScript Natural Library
Aniruddha Chakrabarti
 
PPTX
Dart programming language
Aniruddha Chakrabarti
 
PPTX
Third era of computing
Aniruddha Chakrabarti
 
PPTX
Golang - Overview of Go (golang) Language
Aniruddha Chakrabarti
 
PPTX
Amazon alexa - building custom skills
Aniruddha Chakrabarti
 
PDF
Using Node-RED for building IoT workflows
Aniruddha Chakrabarti
 
PDF
Mphasis Digital - Use Go (gloang) for system programming, distributed systems...
Aniruddha Chakrabarti
 
PDF
Using Swift for all Apple platforms (iOS, watchOS, tvOS and OS X)
Aniruddha Chakrabarti
 
PDF
Future of .NET - .NET on Non Windows Platforms
Aniruddha Chakrabarti
 
PPTX
CoAP - Web Protocol for IoT
Aniruddha Chakrabarti
 
PPTX
Groovy Programming Language
Aniruddha Chakrabarti
 
PDF
Mphasis Digital POV - Emerging Open Standard Protocol stack for IoT
Aniruddha Chakrabarti
 
PPTX
Level DB - Quick Cheat Sheet
Aniruddha Chakrabarti
 
PPTX
Overview of CoffeeScript
Aniruddha Chakrabarti
 
PPTX
memcached Distributed Cache
Aniruddha Chakrabarti
 
PPTX
pebble - Building apps on pebble
Aniruddha Chakrabarti
 
Pinecone Vector Database.pdf
Aniruddha Chakrabarti
 
Mphasis-Annual-Report-2018.pdf
Aniruddha Chakrabarti
 
Thomas Cook and Accenture expand relationship with 10 year technology consult...
Aniruddha Chakrabarti
 
NLP using JavaScript Natural Library
Aniruddha Chakrabarti
 
Dart programming language
Aniruddha Chakrabarti
 
Third era of computing
Aniruddha Chakrabarti
 
Golang - Overview of Go (golang) Language
Aniruddha Chakrabarti
 
Amazon alexa - building custom skills
Aniruddha Chakrabarti
 
Using Node-RED for building IoT workflows
Aniruddha Chakrabarti
 
Mphasis Digital - Use Go (gloang) for system programming, distributed systems...
Aniruddha Chakrabarti
 
Using Swift for all Apple platforms (iOS, watchOS, tvOS and OS X)
Aniruddha Chakrabarti
 
Future of .NET - .NET on Non Windows Platforms
Aniruddha Chakrabarti
 
CoAP - Web Protocol for IoT
Aniruddha Chakrabarti
 
Groovy Programming Language
Aniruddha Chakrabarti
 
Mphasis Digital POV - Emerging Open Standard Protocol stack for IoT
Aniruddha Chakrabarti
 
Level DB - Quick Cheat Sheet
Aniruddha Chakrabarti
 
Overview of CoffeeScript
Aniruddha Chakrabarti
 
memcached Distributed Cache
Aniruddha Chakrabarti
 
pebble - Building apps on pebble
Aniruddha Chakrabarti
 

Recently uploaded (20)

PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 

Redis and it's data types

  • 1. Aniruddha Chakrabarti AVP and Solution Lead, Digital Practice, Mphasis [email protected] | Linkedin.com/in/aniruddhac | slideshare.net/aniruddha.chakrabarti/ | Twitter - anchakra redis and it’s data types http://redis.io/
  • 2. Agenda • What is redis (and what it’s not) • Popularity of redis, who uses redis, history of redis • Type of NoSQL databases, NoSQL history • Different type of data structures supported by redis • String • List • Set • Hash • Sorted Set / ZSET • redis CLI commands • Database • Expiry • Transactions • Mass insert / Bulk upload
  • 3. What is redis • Redis is a Key Value NoSQL database • Open source (BSD licensed) • In memory data structure store. All data is served from memory • Redis mantra - data served from memory, disk used for storage • Offers high performance, replication, and a unique data model • Supports five different data structures - strings, lists, sets, hashes, sorted sets (as value) – redis is also called data structure server • Used as database, cache and message broker. • Actually stands for REmote DIctionary Server • Redis is often compared to memcached, which is a very high performance, key- value cache server. • Supports built in replication, Lua scripting, on disk persistence, limited transaction • Written in ANSI C, supports multiple platform Key1 Val1 Key2 V2,V3 Key3 Val3
  • 4. What redis is not • redis is not a RDBMS • Does not support Schema • Does not support Joins (Stored Procs, Triggers etc) • Does not support ACID Transactions, though supports limited transactions • Does not support SQL
  • 5. NoSQL History • 1998 | Carlo Strozzi used the term NoSQL to name his lightweight, open-source relational database that did not expose the standard SQL interface. • 2000 | Graph database Neo4j started • 2004 | Google BigTable is started in 2004. Paper published in 2006. • 2005 | CouchDB development started. • 2007 | Research paper on Amazon Dynamo released (not AWS DynamoDB) • 2007 | MongoDB started as a part of a open source cloud computing stack and first standalone release in 2009. • 2008 | Facebook open sources the Cassandra project • 2008 | Project Voldemort started • 2009 | The term NoSQL was reintroduced by Eric Evans of rackspace. Redis initial release • 2010 | Some NoSQL conferences NoSQL Matters, NoSQL Now!, INOSA
  • 6. redis History • Early 2009 - Redis project was started in early 2009 by an Italian developer named Salvatore Sanfilippo. Redis was initially written to improve the performance of LLOOGG, a real-time web analytics product out of Salvatore’s startup. • June 2009 - Redis was stable, and had enough of a base feature set, to serve production traffic at LLOOGG (retired the MySQL installation) • Redis rapidly grew in popularity. Salvatore fostered a great community, added features at a very rapid pace, and dealt with bugs. • March 2010 - VMWare hired Salvatore to work full-time on Redis. (Redis itself remains BSD licensed.) VMWare hired Pieter Noordhuis, a key Redis contributor, to give the project an additional momentum boost. • December 2012 - VMWare and EMC spins off Pivotal which would focus on Big Data and Cloud. Redis related effort moves to Pivotal. • June 2015 - Redis Labs started sponsoring the development of Redis
  • 7. Popularity of redis Source: http://db-engines.com/en/ranking • Redis is 10th among all Databases products including RDBMS & NoSQL • Third popular NoSQL database (after MongoDB and Cassandra) • Most popular Key Value store
  • 8. Who uses redis Source - http://techstacks.io/ React Chat Redis Admin UI Flickr Instagram Airbnb Alibaba Medium Pintrest Shopify Square Tumblr Twitter UserVoice Vine Craigslist Stripe rdio Hulu GitHub Disqus BitBucket Parse Songtive StackOverflow Lanyrd Grooveshark MixRadio Kickstarter Trello NewsBlur Coinbase Dashlane DataSift Imgur Gluten Freedom Recommend AppLovin N5 Tech Mozello Appknox Natue Redis Labs A notable use case for Redis is at Twitter, where it is used to cache the latest few hundred tweets for every active user.
  • 9. Types of NoSQL databases • Redis • MemcacheDB • Riak • Amazon DynamoDB • Oracle NoSQL database • Berkley DB • Azure Table Storage • Voldermort • Amazon SimpleDB Key Value Store / Tuple Store • MongoDB • CouchDB • CouchBase • RavenDB • Azure DocumentDB • IBM Cloudant Document Store • BigTable • Hadoop, HBase • Hortonworks, Cloudera • Cassandra • Cloudata • Accumolo • Amazon SimleDB • IBM Informix • Google Cloud Platform BigTable Wide Column Store / Column Family • Neo4J • OrientDB • Titan • HyperGraph DB • FlockDB (Twitter) Graph Database Source - http://nosql-database.org/
  • 10. Types of NoSQL databases Key Value Store / Tuple Store Document Store Wide Column Store / Column Family Graph Database Key1 Val1 Key2 Val2 Key3 Val3 Key n Val n { id:1234, item:"Tshirt", size: 36, price: 567.50 } id:1234 item:"Tshirt", size: 36, price: 567.50
  • 11. redis – Server and CLI/Client redis-server.exe redis.windows.conf • Runs on default port, 6379 D:RedisRedis-x64-2.8.2103>redis-cli 127.0.0.1:6379> • Connects to default port, 6379 and awaits for commands
  • 12. First set of commands 127.0.0.1:6379> ping PONG 127.0.0.1:6379> echo "hello from redis" "hello from redis" 127.0.0.1:6379> quit D:RedisRedis-x64-2.8.2103>
  • 13. redis Data Structures • Redis supports five different data structures 1. String 2. List 3. Set 4. Hash 5. Sorted Set / ZSET • Best way to think about Redis is it’s a data structure server.
  • 14. String Operations Details SET Sets the value stored at the given key GET Fetches the data stored at the given key DEL Deletes the value stored at the given key (works for all types) SETNX Sets the value of a key, only if the key does not exist MSET Sets the value of multiple keys MGET Fetches the data of multiple keys INCR Increase the integer value of a key by one INCRBY Increase the integer value of a key by the given amount DECR Decrease the integer value of a key by one DECRBY Decrease the integer value of a key by the given amount STRLEN Gets the length of the value stored in a key • STRINGs are similar to strings that we see in other languages or other key-value stores. • String values could be added, read, deleted and updated for a key. Key1 Val1 Key2 Val2 city bangalore
  • 15. String 127.0.0.1:6379> set city bangalore OK 127.0.0.1:6379> get city "bangalore" 127.0.0.1:6379> append city ", India" (integer) 16 127.0.0.1:6379> get city "bangalore, India" 127.0.0.1:6379> del city (integer) 1 127.0.0.1:6379> get city (nil) 127.0.0.1:6379> mset key1 val1 key2 val2 key3 val3 OK 127.0.0.1:6379> mget key1 key2 key3 1) "val1" 2) "val2" 3) "val3“ 127.0.0.1:6379> setnx city "den haag" (integer) 1 127.0.0.1:6379> setnx city "blore" (integer) 0 127.0.0.1:6379> strlen city (integer) 8
  • 16. String (Integer) 127.0.0.1:6379> set counter 100 OK 127.0.0.1:6379> get counter "100" 127.0.0.1:6379> incr counter (integer) 101 127.0.0.1:6379> get counter "101" 127.0.0.1:6379> incrby counter 5 (integer) 106 127.0.0.1:6379> decr counter (integer) 105 127.0.0.1:6379> decrby counter 10 (integer) 95
  • 17. List Operations Details LPUSH Pushes the value onto the left end (head) of the list RPUSH Pushes the value onto the right end (tail) of the list LPOP Pops the value from the left end (head) of the list and returns it RPOP Pops the value from the right end (tail) of the list and returns it LRANGE Fetches a range of values from the list LINDEX Fetches an item at a given position in the list • Redis Lists are simply sequence of strings • Ordered by insertion order • Allows duplicates • It is possible to add elements to a Redis List pushing new elements on the head (on the left) or on the tail (on the right) of the list. • Implemented via Linked Lists key1 val1 val2 val1 cities blore chennai mumbai mumbai Key Value cities blore, chennai, mumbai roles admin, general_user, guest weekends Saturday, Sunday promoted_emps Bill, Satya, Satish, Steve prime_nos 1,3,5,7,11,13,17,19
  • 18. List – command examples 127.0.0.1:6379> rpush cities blore (integer) 1 127.0.0.1:6379> rpush cities chennai (integer) 2 127.0.0.1:6379> rpush cities mumbai (integer) 3 127.0.0.1:6379> lrange cities 0 3 1) "blore" 2) "chennai" 3) "mumbai 127.0.0.1:6379> lrange cities 0 -1 1) "blore" 2) "chennai" 3) "mumbai" 127.0.0.1:6379> lindex cities 1 "chennai" 127.0.0.1:6379> rpush cities mumbai (integer) 4 127.0.0.1:6379> lrange cities 0 4 1) "blore" 2) "chennai" 3) "mumbai" 4) "mumbai“ 127.0.0.1:6379> lpush cities chennai (integer) 5 127.0.0.1:6379> lrange cities 0 5 1) "chennai" 2) "blore" 3) "chennai" 4) "mumbai" 5) "mumbai" 127.0.0.1:6379>
  • 19. List – command examples 127.0.0.1:6379> lrange cities 0 5 1) "chennai" 2) "blore" 3) "chennai" 4) "mumbai" 5) "mumbai" 127.0.0.1:6379> rpop cities "mumbai" 127.0.0.1:6379> lrange cities 0 5 1) "chennai" 2) "blore" 3) "chennai" 4) "mumbai" 127.0.0.1:6379> rpop cities "mumbai" 127.0.0.1:6379> lrange cities 0 5 1) "chennai" 2) "blore" 3) "chennai"
  • 20. Set Operations Details SADD Adds the item to the set SMEMBERS Returns the entire set of items SISMEMBER Checks if an item is in the set SREM Removes the item from the set, if it exists • Similar to Lists, Redis Sets are simply sequence of strings • Does not allow duplicates. • Redis SETs use a hash table to keep all strings unique (though there are no associated values). • Redis SETs are unordered, so we can’t push and pop items from the ends like LISTs. key1 val1 val2 val3 unq_cities blore chennai mumbai mumbai
  • 21. Set – command examples 127.0.0.1:6379> sadd ucities chennai kolkata blore (integer) 3 127.0.0.1:6379> sadd ucities delhi (integer) 1 127.0.0.1:6379> smembers ucities 1) "delhi" 2) "blore" 3) "kolkata" 4) "Chennai" 127.0.0.1:6379> sismember ucities blore (integer) 1 127.0.0.1:6379> sismember ucities hyderabad (integer) 0 127.0.0.1:6379> sadd ucities blore (integer) 0 127.0.0.1:6379> srem ucities blore (integer) 1 127.0.0.1:6379> smembers ucities 1) "kolkata" 2) "chennai" 3) "delhi" 127.0.0.1:6379> srem ucities blore (integer) 0
  • 22. Hash Operations Details HSET Stores the value at the key in the hash (single key and value) HGET Fetches the value at the given hash key HGETALL Fetches the entire hash HDEL Removes a key from the hash, if it exists HINCRBY Increases the value of the sub-key by the specified value HKEYS Returns all sub-keys for a specific key HVALS Returns all values for a specific key HMSET Stores multiple key value pairs in the hash HLEN Returns the number of fields in a hash HEXISTS Determine if the hash field exists • Whereas LISTs and SETs in Redis hold sequences of items, Redis HASHes store a mapping of keys to values • Suitable for storing an object key1 sub key1: val1 sub key2: val2 sub key2: val3 users:Satya name: “Satya Nadela” age: 45 email: [email protected]
  • 23. Hash – command examples 127.0.0.1:6379> hset users:Sata name "Satya Nadela" (integer) 1 127.0.0.1:6379> hset users:Sata age 45 (integer) 1 127.0.0.1:6379> hset users:Sata email [email protected] (integer) 1 127.0.0.1:6379> hget users:Sata email "[email protected]" 127.0.0.1:6379> hgetall users:Sata 1) "name" 2) "Satya Nadela" 3) "age" 4) "45" 5) "email" 6) "[email protected]" 127.0.0.1:6379> hdel users:Sata email (integer) 1 127.0.0.1:6379> hgetall users:Sata 1) "name" 2) "Satya Nadela" 3) "age" 4) "45" 127.0.0.1:6379> hkeys users:Sata 1) "name" 2) "age" 127.0.0.1:6379> hvals users:Sata 1) "Satya Nadela" 2) "50"
  • 24. Hash – command examples 127.0.0.1:6379> hincrby users:Sata age 5 (integer) 50 127.0.0.1:6379> hget users:Sata age "50“ 127.0.0.1:6379> hgetall users:Sata 1) "name" 2) "Satya Nadela" 3) "age" 4) "50" 127.0.0.1:6379> hlen users:Sata (integer) 2 127.0.0.1:6379> hexists users:Sata name (integer) 1 127.0.0.1:6379> hexists users:Sata email (integer) 0
  • 25. Hash – command examples 127.0.0.1:6379> hmset users:Sunder name "Sunder P" age 43 designation CEO OK 127.0.0.1:6379> hmset users:XYZ name "XYZ PQR" age 21 designation Engineer email [email protected] mobile "+(91) 12345 67890" OK 127.0.0.1:6379> keys users* 1) "users:Sata" 2) "users:XYZ" 3) "users:Sunder"
  • 26. Sorted Set (ZSET) Operations Details ZADD Adds member with the given score to the ZSET ZRANGE Fetches the items in the ZSET from their positions in sorted order ZRANGEBYSCORE Fetches items in the ZSET based on a range of scores ZREM Removes the item from the ZSET, if it exists ZCARD Returns the number of members in the ZSET • Whereas LISTs and SETs in Redis hold sequences of items, Redis HASHes store a mapping of keys to values • ZSETs offer the ability to store a mapping of members to scores (similar to the keys and values of HASHes). These mappings allow us to manipulate the numeric scores, 2 and fetch and scan over both members and scores based on the sorted order of the scores.
  • 27. Sorted Set (ZSET) – command examples 127.0.0.1:6379> zadd city_ranking 1 'New York' (integer) 1 127.0.0.1:6379> zadd city_ranking 2 'Minneapolis' (integer) 1 127.0.0.1:6379> zadd city_ranking 3 'Bangalore' (integer) 1 127.0.0.1:6379> zadd city_ranking 4 'Amsterdam' (integer) 1 127.0.0.1:6379> zadd city_ranking 5 'Warshaw' (integer) 1 127.0.0.1:6379> zrange city_ranking 0 -1 1) "New York" 2) "Minneapolis" 3) "Bangalore" 4) "Amsterdam" 5) "Warshaw" 127.0.0.1:6379> zrangebyscore city_ranking 2 4 1) "Minneapolis" 2) "Bangalore" 3) "Amsterdam" 127.0.0.1:6379> zrem city_ranking bangalore (integer) 0 127.0.0.1:6379> zrem city_ranking Bangalore (integer) 1 127.0.0.1:6379> zrange city_ranking 0 -1 1) "New York" 2) "Minneapolis" 3) "Amsterdam" 4) "Warshaw“ 127.0.0.1:6379> zcard city_ranking (integer) 4
  • 28. Other Commands 127.0.0.1:6379> keys * 1) "user:ani" 2) "users:Sata" 3) "ctr" 4) "ucities" 5) "friend" 6) "counter" 7) "cities“ 127.0.0.1:6379> keys user* 1) "user:ani" 2) "users:Sata"
  • 29. Server related commands Operations Details INFO Gets detailed information and statistics about the redis server like redis version, OS name, Process Id, port, how long the server has been up, config file name, information about connected clients, memory used by redis, persistence information, replication data, etc. DBSIZE Returns the no of keys in the database CLIENT LIST Gets the list of client connections CLIENT SETNAME Sets the current connection name CLIENT GETNAME Gets the current connection name CLIENT KILL Kills the connection of a client CONFIG GET Gets the value of a configuration parameter CONFIG SET Sets the value of a configuration parameter FLUSHDB Removes all keys from the current database FLUSHALL Removes all keys from all databases
  • 30. Server related commands 127.0.0.1:6379> info # Server redis_version:2.8.2103 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:2a4e86cac6830caf redis_mode:standalone os:Windows arch_bits:64 multiplexing_api:winsock_IOCP process_id:8724 run_id:5bbf82e6f0382362bfa0bf378cb87fa7109e6269 tcp_port:6379 uptime_in_seconds:153348 uptime_in_days:1 hz:10 lru_clock:16491038 config_file:D:RedisRedis-x64-2.8.2103redis.windows.conf 127.0.0.1:6379> dbsize (integer) 9
  • 31. Database in redis 127.0.0.1:6379> set city bangalore OK 127.0.0.1:6379> use 1 (error) ERR unknown command 'use' 127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> keys * (empty list or set) 127.0.0.1:6379[1]> set city kolkata OK 127.0.0.1:6379[1]> get city "kolkata" 127.0.0.1:6379[1]> select 0 OK 127.0.0.1:6379> get city "bangalore" 127.0.0.1:6379> dbsize (integer) 14 • Different types of data, typically for different application or even modules could be stored in different redis Database. • In Redis, databases are identified by an integer index, not by a database name. • By default, a client is connected to database 0 • To connect and switch to a different database use SELECT (such as SELECT 3) – all subsequent commands will then use database 3, until you issue another SELECT. • Each Redis database has its own keyspace. • The number of databases which is available can be configured in redis.conf — by default, it is set to 16. Simply set it to a higher number if you need more
  • 32. Expiry 127.0.0.1:6379> set emsg Hello OK 127.0.0.1:6379> expire emsg 30 (integer) 1 127.0.0.1:6379> ttl emsg (integer) 26 127.0.0.1:6379> get emsg "Hello" 127.0.0.1:6379> ttl emsg (integer) 16 127.0.0.1:6379> get emsg "Hello" 127.0.0.1:6379> ttl emsg (integer) 8 127.0.0.1:6379> get emsg "Hello" 127.0.0.1:6379> ttl emsg (integer) -2 127.0.0.1:6379> get emsg (nil) • Set a timeout on key. After the timeout has expired, the key will automatically be deleted. • A key with an associated timeout is often said to be volatile in Redis terminology. Operations Details EXPIRE Sets a key’s time to live or timeout. After the timeout expires, the key gets deleted TTL Returns the time to live (TTL) for a key
  • 33. Transaction • Set a timeout on key. After the timeout has expired, the key will automatically be deleted. • A key with an associated timeout is often said to be volatile in Redis terminology. Operations Details MULTI Marks the start of a transaction block. Similar to Begin Transaction in SQL. All commands issued after MULTI are not executed and are queued up. EXEC Executes all commands issued after MULTI DISCARD Discards all commands issued after MULTI WATCH Watch the given keys to determine the execution of MULTI/EXEC block.
  • 34. Transaction 127.0.0.1:6379> set debit_account 120000 OK 127.0.0.1:6379> set credit_account 40000 OK 127.0.0.1:6379> multi OK 127.0.0.1:6379> decrby debit_account 30000 QUEUED 127.0.0.1:6379> incrby credit_account 30000 QUEUED 127.0.0.1:6379> exec 1) (integer) 90000 2) (integer) 70000 127.0.0.1:6379> 127.0.0.1:6379> set debit_account 120000 OK 127.0.0.1:6379> set credit_account 40000 OK 127.0.0.1:6379> multi OK 127.0.0.1:6379> decrby debit_account 30000 QUEUED 127.0.0.1:6379> incrby credit_account 30000 QUEUED 127.0.0.1:6379> discard OK 127.0.0.1:6379> get debit_account "120000" 127.0.0.1:6379> get credit_account "40000"
  • 35. Mass insertion of data (bulk upload) • Sometimes Redis instances needs to be loaded with big amount of preexisting or user generated data in short amount of time, so that millions of keys will be created quickly - called mass insertion • In 2.6 or later versions of Redis the redis-cli utility supports a new mode called pipe mode that was designed in order to perform mass insertion. Using the pipe mode the command to run looks like the following: D:RedisRedis-x64-2.8.2103>type initial_data.txt | redis-cli --pipe All data transferred. Waiting for the last reply... Last reply received from server. errors: 0, replies: 5 D:RedisRedis-x64-2.8.2103>redis-cli 127.0.0.1:6379> keys Key* 1) "Key0" 2) "Key4" 3) "Key3" 4) "Key2" 5) "Key1"
  • 36. Install redis-py through pip D:Pythonpython 3.4.3Scripts>pip install redis You are using pip version 7.1.0, however version 7.1.2 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. Collecting redis Downloading redis-2.10.3.tar.gz (86kB) 100% |################################| 90kB 375kB/s Building wheels for collected packages: redis Running setup.py bdist_wheel for redis Stored in directory: C:Usersaniruddha.cAppDataLocalpipCachewheelsb482097713ff9bc6f6a58e41b0a89433aed5d2293fd5868f76a def94 Successfully built redis Installing collected packages: redis Successfully installed redis-2.10.3
  • 37. Install redis client for node • Redis Client for Node – NodeRedis (https://github.com/NodeRedis/node_redis) http://redis.js.org/ npm install redis
  • 38. Redis node – working with string var redis = require('redis'); var client = redis.createClient(); var key = 'city', value = 'New York'; client.set(key, value, redis.print); // prints Reply: OK client.get(key, function(error, data){ console.log(data); // prints New York });
  • 39. Redis node – incrementing var key = 'age'; client.set(key, 35, function(error){ client.incr(key, function (error){ client.get(key, function(error, data){ console.log(data); // 36 client.incrby(key, 5, function (error){ client.get(key, function(error, data){ console.log(data); // 41 }); }); }); }); });
  • 40. Redis node – decrementing var key = 'age'; client.set(key, 35, function(error){ client.decr(key, function (error){ client.get(key, function(error, data){ console.log(data); // 34 client.decrby(key, 5, function (error){ client.get(key, function(error, data){ console.log(data); // 29 }); }); }); }); });
  • 41. Redis node – working with hash (objects) var redis = require('redis'); var client = redis.createClient(); var key = 'employee'; client.hmset(key, { name:'Bill', age:55, designation:"Board of director" }); client.hgetall(key, function (error, data){ console.log(data.name); // Bill console.log(data.age); // 55 console.log(data.designation); // Board of director });
  • 42. Redis node – working with lists var key = "framework"; client.lpush([key,'Angular','Backbone','Amber','Amber'], function (error){ if(! error){ client.lrange(key, 0, -1, function (error, data){ console.log(data); // ['Amber', 'Amber', 'Backbone', 'Angular'] }); } }); var key = "framework"; client.rpush([key,'Angular','Backbone','Amber',‘Amber'], function (error){ if(! error){ client.lrange(key, 0, -1, function (error, data){ console.log(data); // ['Angular', 'Backbone', 'Amber', 'Amber' ] }); } });
  • 43. Redis node – working with sets var key = "frameworks"; client.sadd([key,'Angular','Backbone','Amber','Amber'], function (error){ if(! error){ client.smembers(key, function (error, data){ console.log(data); // [ 'Angular', 'Backbone', 'Amber' ] }); } });
  • 44. Redis node – removing the key var key = "frameworks"; client.exists(key, function(error, reply){ if(reply === 1){ console.log('exists'); client.del(key, function(error, reply){ if(!error){ console.log("key removed successfully"); } }); } else { console.log('does not exist'); } });