Redis – Memory is
the new Disk
1. What isRedis
2. Available clients
3. Data types
4. Operations on data types
5. Performance
6. Persistence
7. Sweet spots
8. Design considerations / Bestpractices
9. Adopters
3
Agenda
● Familiarity with NoSQL?
● Memcache?
● Redis?
4
Poll
Key/value store
like
Memcached onsteroids
5
What is Redis
In-memory
NoSQL database
backed bydisk
6
What is Redis
Collection of data structures exposed
over the network
7
What is Redis
NotjustStrings!
Keys can contain
Strings, Hashes, Lists, Sets and
Sorted Sets
8
What is Redis
What is Redis
9
“ Isee redis definitely more as a flexible tool than as a
solution specialized to solve a specific problem.
- SalvatoreSanfilippo
A Brief History of Redis
1
0
• Started in 2009 by SalvatoreSanfillipo
• VMWare hires Salvatore & Pieter Noordhuis
• Financially supported and promoted by VMWare
Redis Characteristics
10
• Issingle-threaded
• Has queryble keynamespace
• Isextremely fast (~100K operations/second)
• Iseasy to install (no dependencies)
• Issimple and flexible
• Stores everything inmemory
• Written in ANSI C and BSD licensed (free, open)
Language Support
12
Ruby, Python, PHP,Erlang,
Tcl, Perl, Lua, Java, Scala,
Clojure, C#, C/C++,
JavaScript/Node.js, Haskell,
IO,Go
Data Types
13
• Strings (Integers)
• Lists
• Hashes
• Sets
• Sorted Sets
Thinking in Redis
14
• Thisis not an RDBMS
• Itsall about the KEYS, and NOT the
values
• There is no querying onvalues
• There are no indexes
• There are no schemas
Operations on Strings/Integers
15
• SETkey value
• GETkey
• MGET/MSET
• INCR/DECR
• INCRBYDECRBY
• APPENDkey value
• SETNXkey value
• GETSETkey value
Installation
16
• $ wget
http://redis.googlecode.com/files/re
dis-2.4.2.tar.gz
• $ tar xzf redis-2.4.2.tar.gz
• $ cd redis-2.4.2
• $make
• $ ./src/redis-server
Atomicity of Commands
17
• All of the built-in commands of Redis are atomic
• Commands can be chained together (pipelined) to
create complex atomic transactions
• Redis is single threaded and therefore, no locking is
necessary
• In other words, commands like INCR won’t tread on
each other’s toes coming from multiple clients
simultaneously!
Key Expiration
18
• When caching, we don’t want things to live forever
• Any item in Redis can be made to expire after or at a
certain time
• EXPIREmy_key60
• EXPIREATmy_key 1293840000
Operations on Lists
19
• LPUSH/RPUSHkey value [value...]
• LPOP/RPOPkey
• LRANGEkey start stop
• LSETkey index value
• LLENkey
• LTRIMkey startstop
• LINSERTkey before|after pivot
value
• BLPOPkey [key ...] timeout
• LREMkey count value
Operations on Sets
20
SINTERkey [key ...]
SUNIONkey [key ...]
SISMEMBER key member
• SADDkey member
• SPOPkey
• SMEMBERSkey
•
•
•
• SCARD key
Operations on Sorted Sets
20
• ZADDkey score member [score]
[member]
• ZCARDkey
• ZCOUNTkey min max
• ZINCRBYkey increment member
• ZRANKkey member
• ZRANGEkey start stop [WITHSCORES]
• ZSCOREkey member
Operations on Hashes
22
HVALSkey
HLENkey
HEXISTSkey field
• HSETkey field value
• HGETkey field
• HKEYSkey
•
•
•
• HDELkey field [field ...]
Operations on Keys
23
EXPIREkey seconds
EXPIREATkey timestamp
EXISTSkey
• TYPEkey
• TTLkey
• KEYSpattern
•
•
•
• DELkey
Redis Administration Commands
24
DBSIZE
• MONITOR
• SAVE/BGSAVE
• INFO
•
•
•
FLUSHALL
CONFIG SETparameter value
Performance
25
We added two million items into a list in 1.28 seconds, with
a networking layer between us and the server
- SalvatoreSanfilippo,Creator
Performance
26
• Almost same for read and write, handles writes
faster than read
• 100 Kops/second
• Performance depends a lot on
Operation complexity
• Hardware
• Persistence configuration
•
•
•
Payload size
Batch size(pipelining)
Redis-benchmark
27
Demo
28
Key Expiry
• Redis CLI
• Getting Help
• Operations on Strings
•
•
•
Operations on Lists,Set
Groovy program operating on Sorted Set and
Hash
• MONITOR / INFO / DBSIZE/TYPE
• Redis benchmark
Data Durability
29
Dump data to disk after certain conditions are met
OR
Manually
AND/OR
Append Only File (AOF)
Advantages of Persistence
30
• On server restart, Redis re-populates its cache from files stored on
disk
• No issue of “cache warm-up”
• Itis possible to back-up and replicate the files/cache
Memory Requirements?
30
• Depends upon the payload size, and the data-type used
• Instagram reports that they stored 300 million key-value pairs in
about 5 GB of memory =>16 MB/million pairs (they also report that
Memcache requires 52 MB for the same million keys)
Sweet Spots / Use-cases
32
Most read / shared articles
Share state betweenprocesses / Session Tokens
Auto-completion
API rate limiting
Activity Feeds
Job Queue (LPUSH & RPOP)
• Cache Server
• Stats collection
• Tag Cloud
•
•
•
•
•
•
• URL Shortener mapping
Case Studies
33
• Video Marketing Platform
• Real-time analytics
• Stats for usage reports toclients
• Content Publishing application
• Tag Cloud
• Top Read Articles
• URL Shortening information
• Social Media Marketingapplication
• Daily, Monthly and overall stats collection
• List of online users
Design Considerations / Best Practices
34
Use consistent key namespace nomenclature
Have human readable keys
• Avoid excessively longkeys
•
•
• Since there is no querying on values, keys should be thought-out
before hand
• Store data based on use-cases. Ad-hoc queries can-be very
expensive
• Multiple databases are useful for segmenting key namespaces,
especially where key queries are needed
Design Considerations / Best Practices
35
• All datamust fit in memory
•
•
Data duplication/redundancy isOK
Persistence strategy should be chosen based on the kind of data
that you are storing and performance trade-off must be understood
• Good for write-heavy, frequently changing and non-relational data
• Polyglot-persistence isa smartchoice
• Start by using Redis to augment, and then replace
Some Adopters
36
Digg
• Stackoverflow
• Craiglist
• Github
•
•
•
Instagram
Blizzard Entertainment
Learning Resources
37
• http://redis.io/commands
•
•
http://try.redis-db.org
Redis CLI
Advanced Features
38
Pub /Sub
Transactions
Using multiple databases
Commands pipelining
Clustering / Sharding
• Master-slave replication
•
•
•
•
•
• LUA scripting
What is Redis
39
• Simple &Easytouse(Key / Value, data-types map toprogramming
Fast(100 Kops / second)
Powerful(data-types, operations on data-types)
Safe(persistence)
Easytouse(various client libraries)
languages)
• Flexible (Supports commonly used data-types
•
•
•
•
• Widely adopted (Digg, Craiglist, Github …)
Discussion
40
Now that you understand Redis and its capabilities, what use-cases do
come to your mind?
What are your concerns around usage of Redis?
Contact us
Our Office
Client
Location
As the Advanced Consulting
Partners of MongoDB we
have delivered some
amazing MongoDB
development projects,
Know more:
Click Here To Know More!
Have more queries on Grails?
Talk to our GRAILS experts
Now!
Talk To Our Experts

Introduction to Redis

  • 2.
    Redis – Memoryis the new Disk
  • 3.
    1. What isRedis 2.Available clients 3. Data types 4. Operations on data types 5. Performance 6. Persistence 7. Sweet spots 8. Design considerations / Bestpractices 9. Adopters 3 Agenda
  • 4.
    ● Familiarity withNoSQL? ● Memcache? ● Redis? 4 Poll
  • 5.
  • 6.
  • 7.
    Collection of datastructures exposed over the network 7 What is Redis
  • 8.
    NotjustStrings! Keys can contain Strings,Hashes, Lists, Sets and Sorted Sets 8 What is Redis
  • 9.
    What is Redis 9 “Isee redis definitely more as a flexible tool than as a solution specialized to solve a specific problem. - SalvatoreSanfilippo
  • 10.
    A Brief Historyof Redis 1 0 • Started in 2009 by SalvatoreSanfillipo • VMWare hires Salvatore & Pieter Noordhuis • Financially supported and promoted by VMWare
  • 11.
    Redis Characteristics 10 • Issingle-threaded •Has queryble keynamespace • Isextremely fast (~100K operations/second) • Iseasy to install (no dependencies) • Issimple and flexible • Stores everything inmemory • Written in ANSI C and BSD licensed (free, open)
  • 12.
    Language Support 12 Ruby, Python,PHP,Erlang, Tcl, Perl, Lua, Java, Scala, Clojure, C#, C/C++, JavaScript/Node.js, Haskell, IO,Go
  • 13.
    Data Types 13 • Strings(Integers) • Lists • Hashes • Sets • Sorted Sets
  • 14.
    Thinking in Redis 14 •Thisis not an RDBMS • Itsall about the KEYS, and NOT the values • There is no querying onvalues • There are no indexes • There are no schemas
  • 15.
    Operations on Strings/Integers 15 •SETkey value • GETkey • MGET/MSET • INCR/DECR • INCRBYDECRBY • APPENDkey value • SETNXkey value • GETSETkey value
  • 16.
    Installation 16 • $ wget http://redis.googlecode.com/files/re dis-2.4.2.tar.gz •$ tar xzf redis-2.4.2.tar.gz • $ cd redis-2.4.2 • $make • $ ./src/redis-server
  • 17.
    Atomicity of Commands 17 •All of the built-in commands of Redis are atomic • Commands can be chained together (pipelined) to create complex atomic transactions • Redis is single threaded and therefore, no locking is necessary • In other words, commands like INCR won’t tread on each other’s toes coming from multiple clients simultaneously!
  • 18.
    Key Expiration 18 • Whencaching, we don’t want things to live forever • Any item in Redis can be made to expire after or at a certain time • EXPIREmy_key60 • EXPIREATmy_key 1293840000
  • 19.
    Operations on Lists 19 •LPUSH/RPUSHkey value [value...] • LPOP/RPOPkey • LRANGEkey start stop • LSETkey index value • LLENkey • LTRIMkey startstop • LINSERTkey before|after pivot value • BLPOPkey [key ...] timeout • LREMkey count value
  • 20.
    Operations on Sets 20 SINTERkey[key ...] SUNIONkey [key ...] SISMEMBER key member • SADDkey member • SPOPkey • SMEMBERSkey • • • • SCARD key
  • 21.
    Operations on SortedSets 20 • ZADDkey score member [score] [member] • ZCARDkey • ZCOUNTkey min max • ZINCRBYkey increment member • ZRANKkey member • ZRANGEkey start stop [WITHSCORES] • ZSCOREkey member
  • 22.
    Operations on Hashes 22 HVALSkey HLENkey HEXISTSkeyfield • HSETkey field value • HGETkey field • HKEYSkey • • • • HDELkey field [field ...]
  • 23.
    Operations on Keys 23 EXPIREkeyseconds EXPIREATkey timestamp EXISTSkey • TYPEkey • TTLkey • KEYSpattern • • • • DELkey
  • 24.
    Redis Administration Commands 24 DBSIZE •MONITOR • SAVE/BGSAVE • INFO • • • FLUSHALL CONFIG SETparameter value
  • 25.
    Performance 25 We added twomillion items into a list in 1.28 seconds, with a networking layer between us and the server - SalvatoreSanfilippo,Creator
  • 26.
    Performance 26 • Almost samefor read and write, handles writes faster than read • 100 Kops/second • Performance depends a lot on Operation complexity • Hardware • Persistence configuration • • • Payload size Batch size(pipelining)
  • 27.
  • 28.
    Demo 28 Key Expiry • RedisCLI • Getting Help • Operations on Strings • • • Operations on Lists,Set Groovy program operating on Sorted Set and Hash • MONITOR / INFO / DBSIZE/TYPE • Redis benchmark
  • 29.
    Data Durability 29 Dump datato disk after certain conditions are met OR Manually AND/OR Append Only File (AOF)
  • 30.
    Advantages of Persistence 30 •On server restart, Redis re-populates its cache from files stored on disk • No issue of “cache warm-up” • Itis possible to back-up and replicate the files/cache
  • 31.
    Memory Requirements? 30 • Dependsupon the payload size, and the data-type used • Instagram reports that they stored 300 million key-value pairs in about 5 GB of memory =>16 MB/million pairs (they also report that Memcache requires 52 MB for the same million keys)
  • 32.
    Sweet Spots /Use-cases 32 Most read / shared articles Share state betweenprocesses / Session Tokens Auto-completion API rate limiting Activity Feeds Job Queue (LPUSH & RPOP) • Cache Server • Stats collection • Tag Cloud • • • • • • • URL Shortener mapping
  • 33.
    Case Studies 33 • VideoMarketing Platform • Real-time analytics • Stats for usage reports toclients • Content Publishing application • Tag Cloud • Top Read Articles • URL Shortening information • Social Media Marketingapplication • Daily, Monthly and overall stats collection • List of online users
  • 34.
    Design Considerations /Best Practices 34 Use consistent key namespace nomenclature Have human readable keys • Avoid excessively longkeys • • • Since there is no querying on values, keys should be thought-out before hand • Store data based on use-cases. Ad-hoc queries can-be very expensive • Multiple databases are useful for segmenting key namespaces, especially where key queries are needed
  • 35.
    Design Considerations /Best Practices 35 • All datamust fit in memory • • Data duplication/redundancy isOK Persistence strategy should be chosen based on the kind of data that you are storing and performance trade-off must be understood • Good for write-heavy, frequently changing and non-relational data • Polyglot-persistence isa smartchoice • Start by using Redis to augment, and then replace
  • 36.
    Some Adopters 36 Digg • Stackoverflow •Craiglist • Github • • • Instagram Blizzard Entertainment
  • 37.
  • 38.
    Advanced Features 38 Pub /Sub Transactions Usingmultiple databases Commands pipelining Clustering / Sharding • Master-slave replication • • • • • • LUA scripting
  • 39.
    What is Redis 39 •Simple &Easytouse(Key / Value, data-types map toprogramming Fast(100 Kops / second) Powerful(data-types, operations on data-types) Safe(persistence) Easytouse(various client libraries) languages) • Flexible (Supports commonly used data-types • • • • • Widely adopted (Digg, Craiglist, Github …)
  • 40.
    Discussion 40 Now that youunderstand Redis and its capabilities, what use-cases do come to your mind? What are your concerns around usage of Redis?
  • 41.
    Contact us Our Office Client Location Asthe Advanced Consulting Partners of MongoDB we have delivered some amazing MongoDB development projects, Know more: Click Here To Know More! Have more queries on Grails? Talk to our GRAILS experts Now! Talk To Our Experts