Introduction to Redis
Upcoming SlideShare
Loading in...5
×
 

Introduction to Redis

on

  • 2,288 views

Introduction to Redis, include example use-case used in Mindtalk

Introduction to Redis, include example use-case used in Mindtalk

Statistics

Views

Total Views
2,288
Views on SlideShare
2,270
Embed Views
18

Actions

Likes
5
Downloads
18
Comments
0

7 Embeds 18

http://apps.mindtalk.com 5
https://twitter.com 4
http://www.mindtalk.com 3
http://light.mindtalk.com 3
https://si0.twimg.com 1
http://www.docshut.com 1
https://www.mindtalk.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Introduction to Redis Introduction to Redis Presentation Transcript

  • Introduction toRizky Abdilah, Mindtalk, @rizkyabdilah
  • Background● First release in March 2009● Created n maintained by Salvatore Sanfilippo● Mostly like Memcache, but with superset feature● In-memory database, key-value store● Open Source, BSD license● Sponsored by VMWare
  • What to Expect● Introduction to Redis: – Data Structure Server, Key Feature – Scale it up, Persistence, Whos using Redis● Redis and Memcache: – Difference and Similarity● Tips with Redis● Redis on Mindtalk
  • Data Structure Server● String => “This is String”● List => [“dog”, “fox”, “dog”, “cat”]● Hash Table, Dictionary => {“name”: “Rizky”}● Set => set([“dog”, “fox”, “cat”])● Sorted Set: – set([(“dog”, 1), (“fox”, 4), (“cat”, 6)])
  • Key Features and Cool Stuff● All data is in memory (fast)● Support for persistence (rdb, aof)● Time To Live key (set expired)● Atomic Operation (INCR)● Lua Scripting (v2.6)● Pub/Sub● Awesome Community (Friendly)
  • Persistence● RDB, saved periodically or immediately, snashotting: – save 900 1 - (15 min, 1 key changed) – LZF compression● AOF (Append Only File, work like journal) – appendonly yes – Data safety, sacrificing speed● Background and Foreground save
  • Scale it Up● Master-slave, replication for HA● Single-threaded, multiple instance in one machine● Cluster? (to be released)
  • Whos Using Redis● Mindtalk● Twitter● Instagram● Github (Resque)● YouPorn● Etc (http://redis.io/topics/whos-using-redis)
  • BO (Basic Operation): “String”● SET => SET foo “bar”● GET => GET foo● INCR => INCR user_id – Increment integer value by 1● INCRBY => INCRBY user_id 2 – Increment integer value by given amount● SETEX => SETEX foo 5 “bar” – Set the value and the expired time
  • BO List: [“list”]● [L|R]PUSH => LPUSH jobs:android “bla” – Prepend or append element in a list● [L|R]POP => LPOP jobs:android – Remove and get first element in a list● LLEN => LLEN jobs:android – Return length of a list● LRANGE => LRANGE jobs:android 0 -1 – Return all elements in a list● LINDEX => LINDEX jobs:android 0 – Get element in a list by its index
  • BO Hash Table: {“key”: “val”}● HSET => HSET user:$id name “Rizky” – Set value in hash table● HGET => HGET user:$id name – Return value inside hash table● HGETALL => HGETALL user:$id – Return all data in hash table
  • BO Set: set([“1”])● SADD => SADD uniq_ip_visitor “192.168.1.1” – Add element in Set● SREM => SREM uniq_ip_visitor “192.168.1.1” – Remove element in Set● SMEMBERS => SMEMBERS uniq_ip_visitor – Return all elements in Set
  • BO Sorted Set: set([(“cat”, 56)])● ZADD => ZADD post_view 1 $post_id – Add element in sorted set● ZINCRBY => ZINCRBY post_view 1 $post_id – increment● ZRANGE => ZRANGE post_view 0 -1 – Return all data with ascending sort● ZREVRANGE => ZREVRANGE post_view 0 -1 – Return all data with descending sort
  • Advanced Operation● Intersect, Diff, Union Set (join?)● Intersect, Diff, Union Sorted Set
  • Advanced Operation [2]● Blocking List: B[L|R]POP● Multi SET/GET● Bit Operation: SETBIT● Bitwise Operation: BITOP● Append String: APPEND● Scription: EVAL● Etc (http://redis.io/commands)
  • Redis and Memcache: Similarity● RAM only● TTL Support● Communicate via TCP
  • Redis and Memcache: “Difference”● Single vs Multithreaded● Redis Persistence concern● Redis native support for list, hash, set, etc● Redis native support for Pub/Sub● Memcache support for UDP while Redis concern with durability and data safety (TCP only)
  • Tips with Redis● Use hash table (HSET) to compress memory usage, instead of using plain String (SET) - http://mndt.lk/Vr7L - 70MB (SET) vs 16MB (HSET)● Hash and list ziplist max-size, around 1024 – Redis will encode the value inside hash table if the biggest entry didnt exceed the threshold (config) – hash-max-ziplist-entries and list-max- ziplist-entries
  • Redis Way● “But the Redis Way is that the user must understand how things work so that he is able to pick the best compromise, and to understand how the system will behave exactly.” - Salvatore Sanfilippo● So, Redis or Memcache?
  • Redis on Mindtalk● Cache● REST API Rate Limiting● Message Queue (digaku.rpush)● Most Viewed Article (last 2 hour)
  • REST API Rate Limiting● Goal: To limit API Call from Client
  • Message Queue● Implemented using BLPOP and RPUSH command
  • Most Viewed Article (last 2 Hour)
  • Most Viewed Article (last 2 Hour)● Batch processing log-file, result saved in Redis using Sorted Set● ZADD article_view:$hour $count $post_id● ZREVRANGE article_view:$hour 0 9
  • Pub/Sub: digaku.notifier
  • Pub/Sub: digaku.notifier● Long-pooling in client and server● Implemented using Redis PUBLISH and SUBSCRIBE command