Upgrade to Pro — share decks privately, control downloads, hide ads and more …

redis - More than a cache

redis - More than a cache

André Neubauer

November 20, 2012
Tweet

More Decks by André Neubauer

Other Decks in Technology

Transcript

  1. "Software engineering enthusiast. Moved to the dark side. Now executive

    and strong advocate on lean management. Speaker, author, lateral thinker & happy." – @devpg
  2. redis == Remote Dictionary Server - Key-value store - Single-threaded

    - In-memory but persistent on disk - Implemented in C - Human readable protocol - Well documented - Open source - Sponsored by VMware
  3. Data types - Strings (binary-safe) - Hashes - Lists -

    Sets - Sorted Sets Hello World // String user:23 name @devpg // Hash "b", "b", "a", "c" // List "b", "a", "c" // Set "a", "b", "c" // Sorted Set
  4. Commands - Atomic + specific >> SADD following "@devpg" "@TwitterEng"

    // Multiple member arguments since 2.4 (integer) 1 >> SISMEMBER following "@TwitterEng" (integer) 1 >> SISMEMBER following "@Twitter" (integer) 0 >> SADD followers "@devpg" "@TechBerlin" (integer) 1 >> SINTERSTORE overlap following followers (integer) 1 >> SMEMBERS overlap 1) "@devpg"
  5. Commands Redis Any data store 1. Read 2. Filter /

    modify 3. Update 1. Filter / modify
  6. Performance (on my heavily used MacBook Air Core2Duo) ./redis-benchmark ======

    PING (inline) ====== 22624.43 requests per second ====== MSET (10 keys) ====== 13642.57 requests per second ====== SET ====== 20161.29 requests per second ====== GET ====== 21834.06 requests per second ====== INCR ====== 20449.90 requests per second ====== LPUSH ====== 21097.05 requests per second ====== LPOP ====== 21598.27 requests per second ====== SADD ====== 21276.60 requests per second ====== SPOP ====== 22421.52 requests per second ====== LRANGE (first 100) == 16920.47 requests per second ====== LRANGE (first 300) == 11737.09 requests per second ====== LRANGE (first 450) == 9803.92 requests per second [...]
  7. Transactions aka. Multi/Exec/Discard - Atomic execution >> MULTI QUEUED >>

    SADD following "@devpg" "@TwitterEng" QUEUED >> SADD followers "@devpg" "@TechBerlin" QUEUED >> SINTER following followers QUEUED >> EXEC 1) (integer) 1 2) (integer) 1 3) 1) "@devpg"
  8. Transactions aka. Multi/Exec/Discard >> GET item_42 // stock=1 [... check

    > 0 ...] >> MULTI QUEUED >> DECR item_42 QUEUED >> EXEC 1) (integer) 0 >> GET item_42 // stock=1 [... check > 0 ...] >> MULTI QUEUED >> DECR item_42 QUEUED >> EXEC 1) (integer) -1 << Client A >> << Client B >>
  9. Transactions aka. Multi/Exec/Discard + Watch/Unwatch >> WATCH item_42 OK >>

    GET item_42 // stock=1 [... check > 0 ...] >> MULTI QUEUED >> DECR item_42 QUEUED >> EXEC 1) (integer) 0 >> WATCH item_42 OK >> GET item_42 // stock=1 [... check > 0 ...] >> MULTI QUEUED >> DECR item_42 QUEUED >> EXEC (nil) << Client A >> << Client B >>
  10. Persistence - Asynchronously → Won't slow down operations - Delay

    configurable - Different modes: - RDB - AOF - Disabled save <every X sec> <if at least X keys changed> appendonly yes
  11. Replication slaveof <Host> <Port> - Master-slave(s) approach - Non-blocking on

    master-side - Use cases: - Scalability → slaves for read-only queries - Redundancy
  12. Limitations - Data must fit in memory → Cluster-support (at

    least not before version 3.0) - No user/ roles/ privileges - even authentication optional - No data encryption support
  13. Pub/Sub >> SUBSCRIBE java 1) "subscribe" 2) "java" 3) (integer)

    1 1) "message" 2) "java" 3) "hello world" >> PUBLISH java "hello world" (integer) 1 >> PUBLISH Java "hello world" (integer) 0 << Client A >> << Client B >>
  14. Bindings for ... - ActionScript - C - C# -

    C++ - Clojure - Common Lisp - Dart - Erlang - Fancy - Go* - Haskell - Io - Java - Lua - Node.js - Objective-C - Perl - PHP - Pure Data - Python - Ruby - Scala - Smalltalk - Tcl * Just as much clients for Go (5) as for Java (5)!! WTF!?! :-)
  15. Spring Data - redis - Requires > redis 2.0 (2.2

    recommended) - Support for multiple clients (Jedis, JRedis, SRP, RJC) - RedisTemplate, collections, atomic counter - Exception translation - Pubsub support - Implementation for Spring 3.1 cache abstraction