SlideShare a Scribd company logo
1 of 30
Download to read offline
The Basics 
Dominik Gruber, @the_dom 
ViennaDB – Sep. 22, 2014
Agenda 
• What Is Redis? 
• Operations / Data Types 
• Transactions 
• Scripting 
• Use Cases 
Dominik Redis Gruber • @the_dom
Dominik Gruber 
• Work at KURIER.at online network 
• Many services, among them events.at–which 
makes use of Redis 
• Studied “Software Engineering & Internet 
Computing” at Vienna University of Technology 
Dominik Redis Gruber • @the_dom
Fun Facts 
• Redis = REmote DIctionary Server 
• Initial release in 2009 
• Most recent version: 2.8.17 
• Written in C 
• Libraries for 30+ programming languages 
• Good documentation 
Dominik Redis Gruber • @the_dom
Redis 
“Redis is an open source, BSD licensed, 
advanced key-value cache and store. It is often 
referred to as a data structure server since 
keys can contain strings, hashes, lists, sets, 
sorted sets, bitmaps and hyperloglogs.” 
Dominik Redis Gruber • @the_dom
Redis 
• Redis is an in-memory but persistent on disk database 
• 1 Million small Key -> String value pairs use ~ 100 MB of memory 
• Single threaded – but CPU should not be the bottleneck 
• Average Linux system can deliver even 500k requests per 
second 
• Limit is likely the available memory in your system 
• max. 232 keys 
Dominik Redis Gruber • @the_dom
Differences to Memcached 
• Memcached is a “distributed memory object caching system” 
• Redis persists data to disk eventually 
• Memcached is an LRU cache 
• Redis has different data types and more features 
• Memcached is multithreaded 
• Similar speed 
Dominik Redis Gruber • @the_dom
Prominent Adopters 
• Twitter 
• Pinterest 
• Tumblr 
• GitHub 
• Stack Overflow 
Dominik Redis Gruber • @the_dom
Operations / Data Types 
Dominik Redis Gruber • @the_dom
Basics 
• SET key value [EX seconds] [PX milliseconds] [NX|XX] 
• GET key 
• DEL key 
! 
• Possible Use Case: Caching 
Dominik Redis Gruber • @the_dom
Basics 
• EXISTS key 
• KEYS pattern 
• EXPIRE key seconds 
• MGET key [key …] 
• MSET key value [key value …] 
Dominik Redis Gruber • @the_dom
Strings 
• STRLEN KEY 
• APPEND key value 
Dominik Redis Gruber • @the_dom
Integer 
• INCR key / INCRBY key increment 
• DECR key / DECRBY key increment 
! 
• Possible Use Case: Track Ad- or Page-Impressions 
Dominik Redis Gruber • @the_dom
Hashes 
• HSET key field value 
• HGET key field 
• HGETALL key 
• HDEL key field [field …] 
! 
• Possible Use Case: Session Storage 
Dominik Redis Gruber • @the_dom
Lists 
• LSET key index value 
• LPUSH key value [value …] / RPUSH key value [value …] 
• LPOP key / RPOP key 
• LRANGE key start stopl 
• LREM key count value 
• Possible Use Cases: Task queue, Last modified items (with paging) 
Dominik Redis Gruber • @the_dom
Sets 
• SADD key member [member …] 
• SMEMBERS key / SRANDMEMBER key [count] 
• SSCAN key cursor [MATCH pattern] [COUNT count] 
• SISMEMBER key member 
• SPOP key 
• SREM key member [member …] 
• Possible Use Case: Index items by category 
Dominik Redis Gruber • @the_dom
Sets: Operations 
• SINTER key [key …] / SINTERSTORE destination key [key …] 
• SDIFF key [key …] / SDIFFSTORE destination key [key …] 
• SUNION key [key …] / SUNIONSTORE destination key [key …] 
Dominik Redis Gruber • @the_dom
Sorted Sets 
• ZADD key score member [score member …] 
• ZSCORE key member 
• ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset 
count] 
• ZREM key member [member ...] 
• ZREMRANGEBYLEX key min max 
• ZINCRBY key increment member 
• Possible Use Case: Scoreboards, Track time-based events 
Dominik Redis Gruber • @the_dom
HyperLogLog 
“Hyperloglog is an approximate technique for 
computing the number of distinct entries in a set 
(cardinality). It does this while using a small amount of 
memory. For instance, to achieve 99% accuracy, it 
needs only 16 KB. “ 
– Wikipedia 
Dominik Redis Gruber • @the_dom
HyperLogLog 
• PFADD key element [element …] 
• PFCOUNT key [key …] 
• PFMERGE destkey sourcekey [sourcekey ...] 
! 
• Possible Use Case: Track Unique Visitors 
Dominik Redis Gruber • @the_dom
Pub/Sub 
• SUBSCRIBE channel [channel …] 
• UNSUBSCRIBE [channel [channel …]] 
! 
• PUBLISH channel message 
Dominik Redis Gruber • @the_dom
Transactions 
• MULTI 
• EXEC 
• DISCARD 
Dominik Redis Gruber • @the_dom
Transactions 
WATCH mykey 
val = GET mykey 
val = val + 1 
MULTI 
SET mykey $val 
EXEC 
! 
Dominik Redis Gruber • @the_dom
Scripting 
• Execute Lua scripts on the server side 
• EVAL script numkeys key [key ...] arg [arg …] 
! 
• SCRIPT LOAD script 
• EVALSHA sha1 numkeys key [key ...] arg [arg ...] 
Dominik Redis Gruber • @the_dom
Scripting: Example 
RandomPushScript = <<EOF 
local i = tonumber(ARGV[1]) 
local res 
math.randomseed(tonumber(ARGV[2])) 
while (i > 0) do 
res = redis.call('lpush',KEYS[1],math.random()) 
i = i-1 
end 
return res 
EOF 
! 
r.del(:mylist) 
puts r.eval(RandomPushScript,1,:mylist,10,rand(2**32)) 
Dominik Redis Gruber • @the_dom
Use Cases 
Dominik Redis Gruber • @the_dom
Resque 
• “Resque is a Redis-backed Ruby library for creating background 
jobs, placing them on multiple queues, and processing them 
later.” 
• Comes with a web interface 
• Developed by GitHub 
• https://github.com/resque/resque 
• https://github.com/blog/542-introducing-resque 
Dominik Redis Gruber • @the_dom
Typeahead Completion 
• ZRANGEBYLEX key min max [LIMIT offset count] 
• ZRANGEBYLEX uses plain binary comparison 
! 
• ZADD autocomplete 0 apricot 0 apple 0 banana 0 brocolli 0 
brinjal 
• ZRANGEBYLEX autocomplete [br [br(0xff) 
! 
• ZADD 0 mango:{"name":"Mango Smoothie Recipe"} 
0 smoothie:{"name":"Mango Smoothie Recipe"} 
0 recipe:{"name":"Mango Smoothie Recipe"} 
Dominik Redis Gruber • @the_dom
Q & A 
Dominik Redis Gruber • @the_dom
Source 
• http://redis.io 
• http://highscalability.com 
• http://www.cucumbertown.com/craft/ 
autocomplete-using-redis-nginx-lua/ 
Dominik Redis Gruber • @the_dom

More Related Content

Viewers also liked

Using Redis as Distributed Cache for ASP.NET apps - Peter Kellner, 73rd Stre...
 Using Redis as Distributed Cache for ASP.NET apps - Peter Kellner, 73rd Stre... Using Redis as Distributed Cache for ASP.NET apps - Peter Kellner, 73rd Stre...
Using Redis as Distributed Cache for ASP.NET apps - Peter Kellner, 73rd Stre...Redis Labs
 
RedisConf 2016 talk - The Redis API: Simple, Composable, Powerful
RedisConf 2016 talk - The Redis API: Simple, Composable, PowerfulRedisConf 2016 talk - The Redis API: Simple, Composable, Powerful
RedisConf 2016 talk - The Redis API: Simple, Composable, PowerfulDynomiteDB
 
Troubleshooting Redis- DaeMyung Kang, Kakao
Troubleshooting Redis- DaeMyung Kang, KakaoTroubleshooting Redis- DaeMyung Kang, Kakao
Troubleshooting Redis- DaeMyung Kang, KakaoRedis Labs
 
Scalable Streaming Data Pipelines with Redis
Scalable Streaming Data Pipelines with RedisScalable Streaming Data Pipelines with Redis
Scalable Streaming Data Pipelines with RedisAvram Lyon
 
Cloud Foundry for Data Science
Cloud Foundry for Data ScienceCloud Foundry for Data Science
Cloud Foundry for Data ScienceIan Huston
 
Back your App with MySQL & Redis, the Cloud Foundry Way- Kenny Bastani, Pivotal
Back your App with MySQL & Redis, the Cloud Foundry Way- Kenny Bastani, PivotalBack your App with MySQL & Redis, the Cloud Foundry Way- Kenny Bastani, Pivotal
Back your App with MySQL & Redis, the Cloud Foundry Way- Kenny Bastani, PivotalRedis Labs
 
Redis & MongoDB: Stop Big Data Indigestion Before It Starts
Redis & MongoDB: Stop Big Data Indigestion Before It StartsRedis & MongoDB: Stop Big Data Indigestion Before It Starts
Redis & MongoDB: Stop Big Data Indigestion Before It StartsItamar Haber
 
Redis High availability and fault tolerance in a multitenant environment
Redis High availability and fault tolerance in a multitenant environmentRedis High availability and fault tolerance in a multitenant environment
Redis High availability and fault tolerance in a multitenant environmentIccha Sethi
 
March 29, 2016 Dr. Josiah Carlson talks about using Redis as a Time Series DB
March 29, 2016 Dr. Josiah Carlson talks about using Redis as a Time Series DBMarch 29, 2016 Dr. Josiah Carlson talks about using Redis as a Time Series DB
March 29, 2016 Dr. Josiah Carlson talks about using Redis as a Time Series DBJosiah Carlson
 
Benchmarking Redis by itself and versus other NoSQL databases
Benchmarking Redis by itself and versus other NoSQL databasesBenchmarking Redis by itself and versus other NoSQL databases
Benchmarking Redis by itself and versus other NoSQL databasesItamar Haber
 
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, HerokuPostgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, HerokuRedis Labs
 
Leveraging Probabilistic Data Structures for Real Time Analytics with Redis M...
Leveraging Probabilistic Data Structures for Real Time Analytics with Redis M...Leveraging Probabilistic Data Structures for Real Time Analytics with Redis M...
Leveraging Probabilistic Data Structures for Real Time Analytics with Redis M...Itamar Haber
 

Viewers also liked (13)

Using Redis as Distributed Cache for ASP.NET apps - Peter Kellner, 73rd Stre...
 Using Redis as Distributed Cache for ASP.NET apps - Peter Kellner, 73rd Stre... Using Redis as Distributed Cache for ASP.NET apps - Peter Kellner, 73rd Stre...
Using Redis as Distributed Cache for ASP.NET apps - Peter Kellner, 73rd Stre...
 
RedisConf 2016 talk - The Redis API: Simple, Composable, Powerful
RedisConf 2016 talk - The Redis API: Simple, Composable, PowerfulRedisConf 2016 talk - The Redis API: Simple, Composable, Powerful
RedisConf 2016 talk - The Redis API: Simple, Composable, Powerful
 
Troubleshooting Redis- DaeMyung Kang, Kakao
Troubleshooting Redis- DaeMyung Kang, KakaoTroubleshooting Redis- DaeMyung Kang, Kakao
Troubleshooting Redis- DaeMyung Kang, Kakao
 
Scalable Streaming Data Pipelines with Redis
Scalable Streaming Data Pipelines with RedisScalable Streaming Data Pipelines with Redis
Scalable Streaming Data Pipelines with Redis
 
Cloud Foundry for Data Science
Cloud Foundry for Data ScienceCloud Foundry for Data Science
Cloud Foundry for Data Science
 
Back your App with MySQL & Redis, the Cloud Foundry Way- Kenny Bastani, Pivotal
Back your App with MySQL & Redis, the Cloud Foundry Way- Kenny Bastani, PivotalBack your App with MySQL & Redis, the Cloud Foundry Way- Kenny Bastani, Pivotal
Back your App with MySQL & Redis, the Cloud Foundry Way- Kenny Bastani, Pivotal
 
Redis & MongoDB: Stop Big Data Indigestion Before It Starts
Redis & MongoDB: Stop Big Data Indigestion Before It StartsRedis & MongoDB: Stop Big Data Indigestion Before It Starts
Redis & MongoDB: Stop Big Data Indigestion Before It Starts
 
Redis High availability and fault tolerance in a multitenant environment
Redis High availability and fault tolerance in a multitenant environmentRedis High availability and fault tolerance in a multitenant environment
Redis High availability and fault tolerance in a multitenant environment
 
March 29, 2016 Dr. Josiah Carlson talks about using Redis as a Time Series DB
March 29, 2016 Dr. Josiah Carlson talks about using Redis as a Time Series DBMarch 29, 2016 Dr. Josiah Carlson talks about using Redis as a Time Series DB
March 29, 2016 Dr. Josiah Carlson talks about using Redis as a Time Series DB
 
Benchmarking Redis by itself and versus other NoSQL databases
Benchmarking Redis by itself and versus other NoSQL databasesBenchmarking Redis by itself and versus other NoSQL databases
Benchmarking Redis by itself and versus other NoSQL databases
 
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, HerokuPostgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
 
Leveraging Probabilistic Data Structures for Real Time Analytics with Redis M...
Leveraging Probabilistic Data Structures for Real Time Analytics with Redis M...Leveraging Probabilistic Data Structures for Real Time Analytics with Redis M...
Leveraging Probabilistic Data Structures for Real Time Analytics with Redis M...
 
Redis acc 2015_eng
Redis acc 2015_engRedis acc 2015_eng
Redis acc 2015_eng
 

Recently uploaded

Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 

Recently uploaded (20)

Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 

2014-09-22 | Redis - The Basics (ViennaDB)

  • 1. The Basics Dominik Gruber, @the_dom ViennaDB – Sep. 22, 2014
  • 2. Agenda • What Is Redis? • Operations / Data Types • Transactions • Scripting • Use Cases Dominik Redis Gruber • @the_dom
  • 3. Dominik Gruber • Work at KURIER.at online network • Many services, among them events.at–which makes use of Redis • Studied “Software Engineering & Internet Computing” at Vienna University of Technology Dominik Redis Gruber • @the_dom
  • 4. Fun Facts • Redis = REmote DIctionary Server • Initial release in 2009 • Most recent version: 2.8.17 • Written in C • Libraries for 30+ programming languages • Good documentation Dominik Redis Gruber • @the_dom
  • 5. Redis “Redis is an open source, BSD licensed, advanced key-value cache and store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, sorted sets, bitmaps and hyperloglogs.” Dominik Redis Gruber • @the_dom
  • 6. Redis • Redis is an in-memory but persistent on disk database • 1 Million small Key -> String value pairs use ~ 100 MB of memory • Single threaded – but CPU should not be the bottleneck • Average Linux system can deliver even 500k requests per second • Limit is likely the available memory in your system • max. 232 keys Dominik Redis Gruber • @the_dom
  • 7. Differences to Memcached • Memcached is a “distributed memory object caching system” • Redis persists data to disk eventually • Memcached is an LRU cache • Redis has different data types and more features • Memcached is multithreaded • Similar speed Dominik Redis Gruber • @the_dom
  • 8. Prominent Adopters • Twitter • Pinterest • Tumblr • GitHub • Stack Overflow Dominik Redis Gruber • @the_dom
  • 9. Operations / Data Types Dominik Redis Gruber • @the_dom
  • 10. Basics • SET key value [EX seconds] [PX milliseconds] [NX|XX] • GET key • DEL key ! • Possible Use Case: Caching Dominik Redis Gruber • @the_dom
  • 11. Basics • EXISTS key • KEYS pattern • EXPIRE key seconds • MGET key [key …] • MSET key value [key value …] Dominik Redis Gruber • @the_dom
  • 12. Strings • STRLEN KEY • APPEND key value Dominik Redis Gruber • @the_dom
  • 13. Integer • INCR key / INCRBY key increment • DECR key / DECRBY key increment ! • Possible Use Case: Track Ad- or Page-Impressions Dominik Redis Gruber • @the_dom
  • 14. Hashes • HSET key field value • HGET key field • HGETALL key • HDEL key field [field …] ! • Possible Use Case: Session Storage Dominik Redis Gruber • @the_dom
  • 15. Lists • LSET key index value • LPUSH key value [value …] / RPUSH key value [value …] • LPOP key / RPOP key • LRANGE key start stopl • LREM key count value • Possible Use Cases: Task queue, Last modified items (with paging) Dominik Redis Gruber • @the_dom
  • 16. Sets • SADD key member [member …] • SMEMBERS key / SRANDMEMBER key [count] • SSCAN key cursor [MATCH pattern] [COUNT count] • SISMEMBER key member • SPOP key • SREM key member [member …] • Possible Use Case: Index items by category Dominik Redis Gruber • @the_dom
  • 17. Sets: Operations • SINTER key [key …] / SINTERSTORE destination key [key …] • SDIFF key [key …] / SDIFFSTORE destination key [key …] • SUNION key [key …] / SUNIONSTORE destination key [key …] Dominik Redis Gruber • @the_dom
  • 18. Sorted Sets • ZADD key score member [score member …] • ZSCORE key member • ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] • ZREM key member [member ...] • ZREMRANGEBYLEX key min max • ZINCRBY key increment member • Possible Use Case: Scoreboards, Track time-based events Dominik Redis Gruber • @the_dom
  • 19. HyperLogLog “Hyperloglog is an approximate technique for computing the number of distinct entries in a set (cardinality). It does this while using a small amount of memory. For instance, to achieve 99% accuracy, it needs only 16 KB. “ – Wikipedia Dominik Redis Gruber • @the_dom
  • 20. HyperLogLog • PFADD key element [element …] • PFCOUNT key [key …] • PFMERGE destkey sourcekey [sourcekey ...] ! • Possible Use Case: Track Unique Visitors Dominik Redis Gruber • @the_dom
  • 21. Pub/Sub • SUBSCRIBE channel [channel …] • UNSUBSCRIBE [channel [channel …]] ! • PUBLISH channel message Dominik Redis Gruber • @the_dom
  • 22. Transactions • MULTI • EXEC • DISCARD Dominik Redis Gruber • @the_dom
  • 23. Transactions WATCH mykey val = GET mykey val = val + 1 MULTI SET mykey $val EXEC ! Dominik Redis Gruber • @the_dom
  • 24. Scripting • Execute Lua scripts on the server side • EVAL script numkeys key [key ...] arg [arg …] ! • SCRIPT LOAD script • EVALSHA sha1 numkeys key [key ...] arg [arg ...] Dominik Redis Gruber • @the_dom
  • 25. Scripting: Example RandomPushScript = <<EOF local i = tonumber(ARGV[1]) local res math.randomseed(tonumber(ARGV[2])) while (i > 0) do res = redis.call('lpush',KEYS[1],math.random()) i = i-1 end return res EOF ! r.del(:mylist) puts r.eval(RandomPushScript,1,:mylist,10,rand(2**32)) Dominik Redis Gruber • @the_dom
  • 26. Use Cases Dominik Redis Gruber • @the_dom
  • 27. Resque • “Resque is a Redis-backed Ruby library for creating background jobs, placing them on multiple queues, and processing them later.” • Comes with a web interface • Developed by GitHub • https://github.com/resque/resque • https://github.com/blog/542-introducing-resque Dominik Redis Gruber • @the_dom
  • 28. Typeahead Completion • ZRANGEBYLEX key min max [LIMIT offset count] • ZRANGEBYLEX uses plain binary comparison ! • ZADD autocomplete 0 apricot 0 apple 0 banana 0 brocolli 0 brinjal • ZRANGEBYLEX autocomplete [br [br(0xff) ! • ZADD 0 mango:{"name":"Mango Smoothie Recipe"} 0 smoothie:{"name":"Mango Smoothie Recipe"} 0 recipe:{"name":"Mango Smoothie Recipe"} Dominik Redis Gruber • @the_dom
  • 29. Q & A Dominik Redis Gruber • @the_dom
  • 30. Source • http://redis.io • http://highscalability.com • http://www.cucumbertown.com/craft/ autocomplete-using-redis-nginx-lua/ Dominik Redis Gruber • @the_dom