Ofer Zelig
@oferzelig
What is Redis?
• Key-Value Store / “Data Structure Server”

• Value data types: string, list, set, sorted set, hash

• Mem...
What is Redis?
• Extremely easy to use; clearly documented
• Mainly used as Cache layer but some use it as their
first-cla...
Redis vs Memcached
• Rich set of data types, not just key-value pairs
• Supports persistence, you don’t lose what’s in the...
What is Redis?
• Open Source (BSD 3-Clause)
• Written by Salvatore Sanfilippo (@antirez)
and other very smart contributors...
Who uses Redis?

I’ve almost forgot…

Source: http://blog.togo.io/redisphere/redis-roundup-what-companies-use-redis/
DATA TYPES
DATA TYPES

STRING

users:1

{name:’John’,email:’a@b.com’}

images:5

#h•ChhµLT5•OJ

z‹ H

• Up to 2^32 bits (512MB) in ea...
DATA TYPES

STRING

users:1

{name:’John’,email:’a@b.com’}

images:5

#h•ChhµLT5•OJ

• Examples:
•
•
•
•

SET
GETSET
SETBI...
DATA TYPES

STRING

users:1

{name:’John’,email:’a@b.com’}

images:5

#h•ChhµLT5•OJ

z‹ H

• Sample usages:
• Lock:
SET re...
DATA TYPES
bus:messages

1

LIST
4

{age:3}

1

• Up to 2^32 elements in each key (about 4 billion!)
• Internally maintain...
DATA TYPES
bus:messages

1

LIST
4

{age:3}

• Examples:
•
•
•
•

LPUSH / RPUSH / LPOP / RPOP
RPOPLPUSH
LRANGE
BLPOP / BRP...
DATA TYPES
bus:messages

1

LIST
4

{age:3}

1

• Sample usages:
• Timeline of a social network – LPUSH to add new
items, ...
DATA TYPES
page:home:uniquevi
sitors:20140225

•
•
•
•

80.55.203.40

SET
201.3.14.185

55.67.100.120

40.77.232.204

Main...
DATA TYPES
page:home:uniquevi
sitors:20140225

80.55.203.40

SET
201.3.14.185

55.67.100.120

40.77.232.204

• Examples:
•...
DATA TYPES

SORTED SET

sam
Leaders

neville

devon

matt

4236

5870

7615

7615

• Same as set, but ordered
• The order ...
DATA TYPES

SORTED SET

sam
Leaders

neville

devon

matt

4236

5870

7615

7615

• Sample usages:
• Maintain a leader bo...
DATA TYPES

HASH

name
age

29

gender

m

active

user:13

Bob

1

• If Redis keys/values are a big hash, then a HASH key...
DATA TYPES

HASH

name
age

29

gender

m

active

user:13

Bob

1

• Sample usages:
• Saving properties of a business obj...
PERSISTENCE

• Two options: RDB and AOF
• Each has advantages & disadvantages (of course…)
PERSISTENCE

RDB

• Point-in-time snapshot of all dataset
• Compact, ideal for regular backup/archive, can be
delivered to...
PERSISTENCE
•
•
•
•

AOF == Append Only File
Writes every command, like a tape
Gets re-written when it gets too big
Can be...
PERSISTENCE
OK, so what should I use?

• I don’t have clear answer, as always “it depends”
• Visit http://redis.io/topics/...
KEY EXPIRATION
• Each key can be set to expire:

• EXPIRE / EXPIREAT
• There is at least one command that allows setting
e...
KEY EXPIRATION
• Updating a value doesn’t clear and doesn’t reset the timeout.
• Very interesting read on how Redis actual...
MULTI / EXEC
•
•
•
•
•

Transactions
Very safe, no other conflicting clients in the middle
Persistency safe
Optimistic loc...
PUB / SUB
• SUBSCRIBE / PUBLISH

• Very easy (DEMO)
• Example usages:
• Chat
• Bus
GLOBAL COMMANDS
•
•
•
•
•
•
•

KEYS / SCAN / SSCAN / HSCAN / ZSCAN
SELECT / DBSIZE
PING / ECHO
INFO / INFO ALL / TIME
MONI...
BENCHMARKS

• http://redis.io/topics/benchmarks
• DEMO: redis-benchmark -q
MAKING REDIS EVEN FASTER

• Pipelining
• EVAL and Lua Scripts
SERVER
• Windows setup
• https://github.com/MSOpenTech/redis
• Currently v2.6.12
• For development only

• Linux setup
•
•...
.NET CLIENTS
• ServiceStack.Redis
• https://github.com/ServiceStack/ServiceStack.Redis

• BookSleeve
•
•
•
•

https://code...
MANAGERS / VIEWERS
• Redsmin - https://redsmin.com/
• Robust
• Priced (except for a very basic plan)
• SaaS

• Redis Deskt...
Clustering
• A long task in process, current Alpha-grade code
•
•

http://redis.io/topics/cluster-tutorial
http://redis.io...
Resources
• http://redis.io – Main Redis site
• Clients, servers, full documentation

• Interactive tutorial - http://try....
SO WHAT ARE YOU
WAITING FOR?
 Super fast
 Easy to install & maintain
 Stable & robust
 Easy to consume with .Net
 Hig...
Upcoming SlideShare
Loading in …5
×

Introduction to Redis

1,785 views

Published on

This presentation was used in a Redis talk that took place in ALT.NET Melbourne at February 25th, 2014. It is aimed for .Net developers, though almost all of the slides solely discuss Redis as the data store server, without any relation to client libraries in general and .Net in particular.

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,785
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
33
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide
  • WATCH - a command that will make the EXEC conditional: we are asking Redis to perform the transaction only if no other client modified any of the WATCHed keys
  • Introduction to Redis

    1. 1. Ofer Zelig @oferzelig
    2. 2. What is Redis? • Key-Value Store / “Data Structure Server” • Value data types: string, list, set, sorted set, hash • Memory-based which means… very fast reads and writes!! • BUT with disk persistence, so you enjoy all the goods • The only limitation is physical memory on the machine.
    3. 3. What is Redis? • Extremely easy to use; clearly documented • Mainly used as Cache layer but some use it as their first-class database • Hmm… and BTW… it’s also a great Pub/Sub engine! • All of its config is done in one very clear text file • DEMO • Single-threaded
    4. 4. Redis vs Memcached • Rich set of data types, not just key-value pairs • Supports persistence, you don’t lose what’s in the RAM when server reboots • Replication out-of-the-box • You can even create replication trees
    5. 5. What is Redis? • Open Source (BSD 3-Clause) • Written by Salvatore Sanfilippo (@antirez) and other very smart contributors • Full time sponsored by VMware and now by Pivotal
    6. 6. Who uses Redis? I’ve almost forgot… Source: http://blog.togo.io/redisphere/redis-roundup-what-companies-use-redis/
    7. 7. DATA TYPES
    8. 8. DATA TYPES STRING users:1 {name:’John’,email:’a@b.com’} images:5 #h•ChhµLT5•OJ z‹ H • Up to 2^32 bits (512MB) in each key! • Binary safe • Can hold, and ideal for: • Plain strings – page/post cache etc. • Full JSON objects • Raw bits / flags (great usage: realtime metrics – daily active users - http://blog.getspool.com/2011/11/29/fast-easyrealtime-metrics-using-redis-bitmaps/) • Even binary file content (hence binary file manipulation for example)
    9. 9. DATA TYPES STRING users:1 {name:’John’,email:’a@b.com’} images:5 #h•ChhµLT5•OJ • Examples: • • • • SET GETSET SETBIT INCR, INCRBY z‹ H
    10. 10. DATA TYPES STRING users:1 {name:’John’,email:’a@b.com’} images:5 #h•ChhµLT5•OJ z‹ H • Sample usages: • Lock: SET resource-name anystring NX EX max-lock-time • General Cache • Flags • Picture masking (BITOP) • A/B Testing stats
    11. 11. DATA TYPES bus:messages 1 LIST 4 {age:3} 1 • Up to 2^32 elements in each key (about 4 billion!) • Internally maintained as linked-lists • O(N) - Extremely fast near both ends of the list, less in the middle • Ideal for: • Queues (private and shared) • Stacks • Top N, Recent News
    12. 12. DATA TYPES bus:messages 1 LIST 4 {age:3} • Examples: • • • • LPUSH / RPUSH / LPOP / RPOP RPOPLPUSH LRANGE BLPOP / BRPOP 1
    13. 13. DATA TYPES bus:messages 1 LIST 4 {age:3} 1 • Sample usages: • Timeline of a social network – LPUSH to add new items, LRANGE to retrieve most recently added items • LPUSH+LTRIM together to always keep top/latest N • Useful for logging of recent user actions / errors • RPOPLPUSH using same source and destination to create a rotation of “ring” of items • Blocking/waiting
    14. 14. DATA TYPES page:home:uniquevi sitors:20140225 • • • • 80.55.203.40 SET 201.3.14.185 55.67.100.120 40.77.232.204 Maintains a unique set of items (but unordered) Up to 2^32 members in each key (about 4 billion)! Supports intersections, unions etc. Quickly check whether a member exists – SISMEMBER - O(1) • Ideal for: • Storing relations (i.e. followers, friends etc.) – the key represents the User ID, the value (set members) is the users they follow.
    15. 15. DATA TYPES page:home:uniquevi sitors:20140225 80.55.203.40 SET 201.3.14.185 55.67.100.120 40.77.232.204 • Examples: • SRANDMEMBER / SPOP • SINTERSTORE / SUNIONSTORE / SDIFFSTORE etc. • Sample usages: • Tracking unique IPs (just use SADD every view, then use SCARD whenever you want) • Tagging: Use a set for each tag (each member is, say, a picture ID). You can easily get all pictures that have 3 different tags using SINTER • Extract random members (ads, lottery balls, tip of the day…) using SPOP / SRANDMEMBER
    16. 16. DATA TYPES SORTED SET sam Leaders neville devon matt 4236 5870 7615 7615 • Same as set, but ordered • The order is defined by score. Each member has a score assigned to. • Up to 2^32 members in each key (about 4 billion)! • Supports intersections, unions etc. – basically all commands that a SET supports • O(LOG(N)), O(N) and such • But hey! It’s still a millisecond or so even for big sorted sets • Sorting takes place upon insert, so queries for ranges, even in the middle, are still fast • Members are still unique, but scores may be repeated.
    17. 17. DATA TYPES SORTED SET sam Leaders neville devon matt 4236 5870 7615 7615 • Sample usages: • Maintain a leader board of a game, get top X etc. • In general, any top something. Such as top posts by pageviews etc. • Index other data in Redis. For example: a sorted set where each member is a user ID and the score is their age. You can query for all users within a given age range. • Search text statistics
    18. 18. DATA TYPES HASH name age 29 gender m active user:13 Bob 1 • If Redis keys/values are a big hash, then a HASH key holds a value that is itself a HASH • But that can’t be nested further • Up to 2^32 key/value pairs in each hash • Fields in a hash can still be atomically incremented, for example
    19. 19. DATA TYPES HASH name age 29 gender m active user:13 Bob 1 • Sample usages: • Saving properties of a business object (very similar to plain DB table) • Saving sessions & CSRF tokens • Maintaining many-to-many mappings and such
    20. 20. PERSISTENCE • Two options: RDB and AOF • Each has advantages & disadvantages (of course…)
    21. 21. PERSISTENCE RDB • Point-in-time snapshot of all dataset • Compact, ideal for regular backup/archive, can be delivered to S3 etc. • Multiple save-points available, set in redis.conf
    22. 22. PERSISTENCE • • • • AOF == Append Only File Writes every command, like a tape Gets re-written when it gets too big Can be easily parsed & edited AOF
    23. 23. PERSISTENCE OK, so what should I use? • I don’t have clear answer, as always “it depends” • Visit http://redis.io/topics/persistence for much more explanation including “What Should I Use” • Bottom line – defaults are OK, just start and worry about that when your app gets bigger
    24. 24. KEY EXPIRATION • Each key can be set to expire: • EXPIRE / EXPIREAT • There is at least one command that allows setting expire time within the command: SETEX • Cancel expiration - PERSIST • TTL - returns the remaining time to live of a key that has a timeout. • Overwrite of a whole key (i.e. SET) clears the timeout.
    25. 25. KEY EXPIRATION • Updating a value doesn’t clear and doesn’t reset the timeout. • Very interesting read on how Redis actually deletes expired keys - http://redis.io/commands/expire • Global expiration policy can be set in redis.conf. Ideal: when Redis is used as a cache layer: • maxmemory 2mb maxmemory-policy allkeys-lru • There are more options – read the comments inside redis.conf
    26. 26. MULTI / EXEC • • • • • Transactions Very safe, no other conflicting clients in the middle Persistency safe Optimistic locking using WATCH EVAL and Lua Scripts
    27. 27. PUB / SUB • SUBSCRIBE / PUBLISH • Very easy (DEMO) • Example usages: • Chat • Bus
    28. 28. GLOBAL COMMANDS • • • • • • • KEYS / SCAN / SSCAN / HSCAN / ZSCAN SELECT / DBSIZE PING / ECHO INFO / INFO ALL / TIME MONITOR SAVE / BGSAVE CLIENT LIST / CLIENT KILL • Advanced: SORT (http://redis.io/commands/sort)
    29. 29. BENCHMARKS • http://redis.io/topics/benchmarks • DEMO: redis-benchmark -q
    30. 30. MAKING REDIS EVEN FASTER • Pipelining • EVAL and Lua Scripts
    31. 31. SERVER • Windows setup • https://github.com/MSOpenTech/redis • Currently v2.6.12 • For development only • Linux setup • • • • Recommended for production Currently v2.8.6 First of all, don’t be afraid! Clear instructions on the Redis site • http://redis.io/topics/quickstart • You can skip most manual steps and just run a server installation script: • http://download.redis.io/redis-stable/README
    32. 32. .NET CLIENTS • ServiceStack.Redis • https://github.com/ServiceStack/ServiceStack.Redis • BookSleeve • • • • https://code.google.com/p/booksleeve/ Written by Marc Gravell from Stack Exchange Pipelined, Task API, async/await etc. Maintain 1 connection throughout the app • http://stackoverflow.com/a/8777999/290343 • DEMO
    33. 33. MANAGERS / VIEWERS • Redsmin - https://redsmin.com/ • Robust • Priced (except for a very basic plan) • SaaS • Redis Desktop Manager - http://redisdesktop.com/ • Open Source Desktop GUI • Pretty basic • redis-commander - https://github.com/joeferner/redis-commander • Node.js, Open Source • Pretty basic
    34. 34. Clustering • A long task in process, current Alpha-grade code • • http://redis.io/topics/cluster-tutorial http://redis.io/presentation/Redis_Cluster.pdf • In the mean time: • Replication (master / slaves) – works great • Redis Sentinel: http://redis.io/topics/sentinel • A system designed to help managing Redis instances • Monitoring. constantly checks if master and slave instances are working as expected. • Notification. notify sysadmin or other program, that something is wrong with one of the monitored Redis instances. • Automatic failover. if the master failed (a slave is promoted to be master, everyone is updated).
    35. 35. Resources • http://redis.io – Main Redis site • Clients, servers, full documentation • Interactive tutorial - http://try.redis.io/ • The Little Redis Book - http://openmymind.net/redis.pdf • Various Stack Exchange articles about their infrastructure including Redis: • Canonical answer about Stack Exchange cache layers: http://meta.stackoverflow.com/a/69172 • Great answer from Antirez himself about usage of the different data types: • http://stackoverflow.com/a/9626334
    36. 36. SO WHAT ARE YOU WAITING FOR?  Super fast  Easy to install & maintain  Stable & robust  Easy to consume with .Net  High quality documentation

    ×