Introduction to Redis

50 %
50 %
Information about Introduction to Redis

Published on February 26, 2014

Author: oferzelig



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.

Ofer Zelig @oferzelig

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.

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

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

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

Who uses Redis? I’ve almost forgot… Source:


DATA TYPES STRING users:1 {name:’John’,email:’’} 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 - • Even binary file content (hence binary file manipulation for example)

DATA TYPES STRING users:1 {name:’John’,email:’’} images:5 #h•ChhµLT5•OJ • Examples: • • • • SET GETSET SETBIT INCR, INCRBY z‹ H

DATA TYPES STRING users:1 {name:’John’,email:’’} 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

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

DATA TYPES bus:messages 1 LIST 4 {age:3} • Examples: • • • • LPUSH / RPUSH / LPOP / RPOP RPOPLPUSH LRANGE BLPOP / BRPOP 1

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

DATA TYPES page:home:uniquevi sitors:20140225 • • • • SET 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.

DATA TYPES page:home:uniquevi sitors:20140225 SET • 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

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.

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

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

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

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 S3 etc. • Multiple save-points available, set in redis.conf

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

PERSISTENCE OK, so what should I use? • I don’t have clear answer, as always “it depends” • Visit 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

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.

KEY EXPIRATION • Updating a value doesn’t clear and doesn’t reset the timeout. • Very interesting read on how Redis actually deletes expired keys - • 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

MULTI / EXEC • • • • • Transactions Very safe, no other conflicting clients in the middle Persistency safe Optimistic locking using WATCH EVAL and Lua Scripts

PUB / SUB • SUBSCRIBE / PUBLISH • Very easy (DEMO) • Example usages: • Chat • Bus


BENCHMARKS • • DEMO: redis-benchmark -q

MAKING REDIS EVEN FASTER • Pipelining • EVAL and Lua Scripts

SERVER • Windows setup • • 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 • • You can skip most manual steps and just run a server installation script: •

.NET CLIENTS • ServiceStack.Redis • • BookSleeve • • • • Written by Marc Gravell from Stack Exchange Pipelined, Task API, async/await etc. Maintain 1 connection throughout the app • • DEMO

MANAGERS / VIEWERS • Redsmin - • Robust • Priced (except for a very basic plan) • SaaS • Redis Desktop Manager - • Open Source Desktop GUI • Pretty basic • redis-commander - • Node.js, Open Source • Pretty basic

Clustering • A long task in process, current Alpha-grade code • • • In the mean time: • Replication (master / slaves) – works great • Redis 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).

Resources • – Main Redis site • Clients, servers, full documentation • Interactive tutorial - • The Little Redis Book - • Various Stack Exchange articles about their infrastructure including Redis: • Canonical answer about Stack Exchange cache layers: • Great answer from Antirez himself about usage of the different data types: •

SO WHAT ARE YOU WAITING FOR?  Super fast  Easy to install & maintain  Stable & robust  Easy to consume with .Net  High quality documentation

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Introduction to Redis – Redis

*Introduction to Redis. Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker. It supports ...
Read more

Azure Redis Cache 101 - Introduction to Redis

Scott talks with Saurabh Pant about the new Azure Managed Redis Cache. We'll cover a basic introduction to Redis and Redis on Windows. What kinds of things ...
Read more

Introduction to Redis - Redis To Go

Introduction to Redis. Redis is much more than a key-value store; it offers several data structures that can be used. This introduction will cover the ...
Read more

An introduction to Redis data types and abstractions – Redis

*An introduction to Redis data types and abstractions. Redis is not a plain key-value store, actually it is a data structures server, supporting different ...
Read more

GitHub - jermnelson/introduction-to-redis: Course site for ...

introduction-to-redis - Course site for Introduction to Redis ... Use SSH Clone with HTTPS Use Git or checkout with SVN using the web URL.
Read more

Redis – Wikipedia

Redis bietet Persistenz durch automatisiertes regelmäßiges Abspeichern oder per Protokolldatei, wodurch bei entsprechender Konfiguration auch eine ACID- ...
Read more

Azure Redis Cache 101 - Introduction to Redis (Channel 9)

Scott talks with Saurabh Pant about the new Azure Managed Redis Cache. We'll cover a basic introduction to Redis and Redis on Windows. What ...
Read more

Introduction to Redis - In Memory Key Value Datastore ...

Learn how test data on demand can lead to faster application development, read the IDC white paper, brought to you in partnership with Delphix.
Read more

Introduction to Redis database and using it with Java ...

Introduction: More speed is a dominant need in our life and IT field has no exception from that. Redis comes to meet the need of speed in the database domain.
Read more