Redis & Groovy & Grails                               by Ted Naleid                               http://naleid.comMonday,...
“Redis is a collection of data                        structures exposed over the                                  network...
key/value store               like memcached on steroidsMonday, June 20, 2011
Strings, Integers,                                Lists, Hashes,                              Sets & Sorted Sets          ...
in-memory storageMonday, June 20, 2011
single-threadedMonday, June 20, 2011
fastMonday, June 20, 2011
“Memory is the new Disk,                         Disk is the new Tape”                                 - Jim GrayMonday, J...
Relative Latency                           CPU Register - 1x                            L2 Cache - 10x                    ...
CPU Register                            1 yard                                       photo: http://www.flickr.com/photos/li...
L2 Cache                         10 yards                                    photo: http://www.flickr.com/photos/plentyofan...
Memory                        100 yards                                    photo: http://www.flickr.com/photos/billmcintyre...
Disk        Minneapolis to New York to Miami                   to Seattle                        ~5600 milesMonday, June 2...
Simple wire protocol that                              matches APIMonday, June 20, 2011
% telnet localhost 6379    Escape character is ^].    set foo bar    +OK    get foo    $3    bar    rpush mylist first    ...
clients for every* language             *well not every language, but all the popular/semi-popular ones, you can easily wr...
No dependencies                            (451KB download)Monday, June 20, 2011
Used in production by high                  traffic sites that you’ve usedMonday, June 20, 2011
stackoverflow, craigslist,                         github, disqus, digg &                         blizzard entertainmentMo...
financially supported by                         VMware/SpringSourceMonday, June 20, 2011
simple data structures make                       redis flexibleMonday, June 20, 2011
write-through caching/                             memoizationMonday, June 20, 2011
producer/consumer                         message queuesMonday, June 20, 2011
publish/subscribeMonday, June 20, 2011
atomic sequences/countersMonday, June 20, 2011
other uses...                             distributed locks, tag clouds, session tokens,                        auto-compl...
Redis CommandsMonday, June 20, 2011
Great Online Reference                               http://redis.io/commandsMonday, June 20, 2011
Strings                                            set                        Redis REPL                            Groovy...
Strings                                            get                        Redis REPL                            Groovy...
Exists                                     exists (check key existence)                        Redis REPL                 ...
Integers                                     incr (increment)                        Redis REPL                       Groo...
Integers                                     decr (decrement)                        Redis REPL                       Groo...
Lists                                     rpush (right push)                        Redis REPL                            ...
Lists                                     rpush (right push)                        Redis REPL                            ...
Lists                                     lpush (left push)                        Redis REPL                             ...
Lists                                     lrange (slice of list)                        Redis REPL                        ...
Lists                                     lrange (slice of list)                        Redis REPL                        ...
Lists                                     lpop (left pop)                        Redis REPL                       Groovy  ...
Lists                                     rpop (right pop)                        Redis REPL                       Groovy ...
Hashes                                     hset (set key → value)                        Redis REPL                       ...
Hashes                                     hset (set key → value)                        Redis REPL                       ...
Hashes                                     hget (get value for key)                        Redis REPL                     ...
Hashes                                     hgetall (get all keys/values)                        Redis REPL                ...
Hashes                                     hvals (hash values)                        Redis REPL                          ...
Hashes                                     hkeys (hash keys)                        Redis REPL                            ...
Sets                                     sadd (set add)                        Redis REPL                           Groovy...
Sets                                     sadd (set add)                        Redis REPL                          Groovy ...
Sets                                     sismember (membership test)                        Redis REPL                    ...
Sets                                     sismember (membership test)                        Redis REPL                    ...
Sets                                     smembers (get full set)                        Redis REPL                        ...
Sets                                     sinter (set intersection)                        Redis REPL                      ...
Sets                                      sdiff (set difference)                        Redis REPL                        ...
Sets                                          sunion (set union)                        Redis REPL                        ...
Sorted Sets                                      zadd (add with score)                        Redis REPL                  ...
Sorted Sets                                     zscore (score for member)                        Redis REPL               ...
Sorted Sets                                        zrange (sorted subset)                        Redis REPL               ...
Sorted Sets                                zrangebyscore (subset having score range)                        Redis REPL    ...
Groovy UsageMonday, June 20, 2011
Grape @Grab Annotation          #! /usr/bin/env groovy          @Grab(redis.clients:jedis:2.0.0)          def redis = new ...
Producer/Consumer                             ExampleMonday, June 20, 2011
Producer                          pushes work on a list with lpush         @Grab(redis.clients:jedis:2.0.0)         redis ...
Consumer                        uses blpop (blocking left pop from list)         @Grab(redis.clients:jedis:2.0.0)         ...
Mass Producer                        srandmember to randomly pick female name from set         @Grab(redis.clients:jedis:2...
Groovy DemoMonday, June 20, 2011
Grails Redis Plugin                        https://github.com/grails-plugins/grails-redisMonday, June 20, 2011
Plugin Config                                        in Config.xml          grails {              redis {                 ...
Provides Caching Through                              MemoizationMonday, June 20, 2011
RedisTagLib          <redis:memoize key="mykey" expire="3600">              <!--                   insert expensive to gen...
RedisService                          Spring bean wraps pool connection          // overrides propertyMissing and methodMi...
RedisService                        template methods manage pooled Redis connection          redisService.withRedis { Jedi...
RedisService                            template methods manage pooled Redis connection          redisService.withRedis { ...
RedisService                                   String memoization          redisService.memoize("my-key") { Jedis redis ->...
RedisService                        Domain Class memoization (stores IDs hydrates from DB)           def key = "user:$id:f...
Example                        Showing Products with Sort/Filter/Pagination CriteriaMonday, June 20, 2011
Other Memoization Methods                        memoizeHash, memoizeHashField,                         memoizeScore (sort...
Grails Redis-GORM Plugin                          http://grails.github.com/inconsequential/redis/Monday, June 20, 2011
Uses SpringData to abstract                          data layerMonday, June 20, 2011
Can be used in conjunction                        with HibernateMonday, June 20, 2011
Partial support for GORM     including Dynamic Finders, Criteria, Named Queries and “Transactions”Monday, June 20, 2011
Limitations                        It requires explicit index mapping on fields you want to query          package com.exa...
Under The Covers                        MONITOR output for new Author(name: "Stephen King").save()          1308027697.922...
Questions?Monday, June 20, 2011
Upcoming SlideShare
Loading in …5
×

Redis and Groovy and Grails - gr8conf 2011

49,576
-1

Published on

Presentation at the gr8conf US 2011 on using Redis with some additional info about usage within Groovy and Grails applications.

Published in: Technology
6 Comments
61 Likes
Statistics
Notes
  • The section of introducing redis commands is pretty good.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • pretty good
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • can i download it ?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • 介绍 redis 的操作很生动,一看就懂
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • introduce
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
49,576
On Slideshare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
1,045
Comments
6
Likes
61
Embeds 0
No embeds

No notes for slide

Redis and Groovy and Grails - gr8conf 2011

  1. 1. Redis & Groovy & Grails by Ted Naleid http://naleid.comMonday, June 20, 2011
  2. 2. “Redis is a collection of data structures exposed over the network” from: http://nosql.mypopescu.com/post/5403851771/what-is-redisMonday, June 20, 2011
  3. 3. key/value store like memcached on steroidsMonday, June 20, 2011
  4. 4. Strings, Integers, Lists, Hashes, Sets & Sorted Sets (& commonly expected operations with each data type)Monday, June 20, 2011
  5. 5. in-memory storageMonday, June 20, 2011
  6. 6. single-threadedMonday, June 20, 2011
  7. 7. fastMonday, June 20, 2011
  8. 8. “Memory is the new Disk, Disk is the new Tape” - Jim GrayMonday, June 20, 2011
  9. 9. Relative Latency CPU Register - 1x L2 Cache - 10x Memory - 100x Disk - 10,000,000x analogy from “Redis - Memory as the New Disk” - Tim Lossen & http://en.wikipedia.org/wiki/Orders_of_magnitude_(speed)Monday, June 20, 2011
  10. 10. CPU Register 1 yard photo: http://www.flickr.com/photos/limonada/904754668/Monday, June 20, 2011
  11. 11. L2 Cache 10 yards photo: http://www.flickr.com/photos/plentyofants/2749262107Monday, June 20, 2011
  12. 12. Memory 100 yards photo: http://www.flickr.com/photos/billmcintyre/264905933Monday, June 20, 2011
  13. 13. Disk Minneapolis to New York to Miami to Seattle ~5600 milesMonday, June 20, 2011
  14. 14. Simple wire protocol that matches APIMonday, June 20, 2011
  15. 15. % telnet localhost 6379 Escape character is ^]. set foo bar +OK get foo $3 bar rpush mylist first :1 rpush mylist second :2 lrange mylist 0 -1 *2 $5 first $6 secondMonday, June 20, 2011
  16. 16. clients for every* language *well not every language, but all the popular/semi-popular ones, you can easily write one if your language doesn’t have oneMonday, June 20, 2011
  17. 17. No dependencies (451KB download)Monday, June 20, 2011
  18. 18. Used in production by high traffic sites that you’ve usedMonday, June 20, 2011
  19. 19. stackoverflow, craigslist, github, disqus, digg & blizzard entertainmentMonday, June 20, 2011
  20. 20. financially supported by VMware/SpringSourceMonday, June 20, 2011
  21. 21. simple data structures make redis flexibleMonday, June 20, 2011
  22. 22. write-through caching/ memoizationMonday, June 20, 2011
  23. 23. producer/consumer message queuesMonday, June 20, 2011
  24. 24. publish/subscribeMonday, June 20, 2011
  25. 25. atomic sequences/countersMonday, June 20, 2011
  26. 26. other uses... distributed locks, tag clouds, session tokens, auto-complete prefixes, API rate limiting, leaderboards, capped logs, random set items, A/B testing data storage, unique per user product pricing/sortingMonday, June 20, 2011
  27. 27. Redis CommandsMonday, June 20, 2011
  28. 28. Great Online Reference http://redis.io/commandsMonday, June 20, 2011
  29. 29. Strings set Redis REPL Groovy > set foo bar redis.set("foo", "bar") foo bar OK OKMonday, June 20, 2011
  30. 30. Strings get Redis REPL Groovy foo bar > get foo redis.get("foo") "bar" "bar" barMonday, June 20, 2011
  31. 31. Exists exists (check key existence) Redis REPL Groovy foo (nil) > exists foo redis.exists("foo") (integer) 0 <= false 0Monday, June 20, 2011
  32. 32. Integers incr (increment) Redis REPL Groovy foo 1 > incr foo redis.incr("foo") (integer) 1 <= 1 1Monday, June 20, 2011
  33. 33. Integers decr (decrement) Redis REPL Groovy foo 0 > decr foo redis.decr("foo") (integer) 0 <= 0 0Monday, June 20, 2011
  34. 34. Lists rpush (right push) Redis REPL Groovy > rpush foo baz redis.rpush("foo", "baz") foo baz (integer) 1 <= 1Monday, June 20, 2011
  35. 35. Lists rpush (right push) Redis REPL Groovy > rpush foo qux redis.rpush("foo", "qux") foo baz qux (integer) 2 <= 2Monday, June 20, 2011
  36. 36. Lists lpush (left push) Redis REPL Groovy > lpush foo bar redis.lpush("foo", "bar") foo bar baz qux (integer) 3 <= 3Monday, June 20, 2011
  37. 37. Lists lrange (slice of list) Redis REPL Groovy foo bar baz qux > lrange foo 0 -1 redis.lrange("foo", 0, -1) 1) "bar" <= [bar, baz, qux] 2) "baz" 3) "qux" bar baz quxMonday, June 20, 2011
  38. 38. Lists lrange (slice of list) Redis REPL Groovy foo bar baz qux > lrange foo 0 1 redis.lrange("foo", 0, 1) 1) "bar" <= [bar, baz] 2) "baz" bar bazMonday, June 20, 2011
  39. 39. Lists lpop (left pop) Redis REPL Groovy foo baz qux > lpop foo redis.lpop("foo") "bar" <= "bar" barMonday, June 20, 2011
  40. 40. Lists rpop (right pop) Redis REPL Groovy foo baz > rpop foo redis.rpop("foo") "qux" <= "qux" quxMonday, June 20, 2011
  41. 41. Hashes hset (set key → value) Redis REPL Groovy > hset foo bar baz redis.hset("foo", "bar", "baz") foo bar baz (integer) 1 <= 1Monday, June 20, 2011
  42. 42. Hashes hset (set key → value) Redis REPL Groovy bar baz > hset foo qux quxx redis.hset("foo", "qux", "quxx") foo (integer) 1 <= 1 qux quxxMonday, June 20, 2011
  43. 43. Hashes hget (get value for key) Redis REPL Groovy bar baz foo qux quxx > hget foo bar redis.hget("foo", "bar") "baz" <= "baz" bazMonday, June 20, 2011
  44. 44. Hashes hgetall (get all keys/values) Redis REPL Groovy bar baz foo > hgetall foo qux quxx 1) "bar" redis.hgetAll("foo") 2) "baz" <= [bar:baz, qux:quxx] 3) "qux" 4) "quxx" bar baz qux quxxMonday, June 20, 2011
  45. 45. Hashes hvals (hash values) Redis REPL Groovy bar baz foo qux quxx > hvals foo redis.hvals("foo") 1) "baz" <= [baz, quxx] 2) "quxx" baz quxxMonday, June 20, 2011
  46. 46. Hashes hkeys (hash keys) Redis REPL Groovy bar baz foo qux quxx > hkeys foo redis.hkeys("foo") 1) "bar" <= [bar, qux] 2) "qux" bar quxMonday, June 20, 2011
  47. 47. Sets sadd (set add) Redis REPL Groovy > sadd m1 jan redis.sadd("m1", "jan") m1 jan (integer) 1 <= 1Monday, June 20, 2011
  48. 48. Sets sadd (set add) Redis REPL Groovy feb > sadd m1 feb redis.sadd("m1", "feb") m1 (integer) 1 <= 1 janMonday, June 20, 2011
  49. 49. Sets sismember (membership test) Redis REPL Groovy feb m1 jan > sismember m1 jan redis.sismember("m1", "jan") (integer) 1 <= true 1Monday, June 20, 2011
  50. 50. Sets sismember (membership test) Redis REPL Groovy feb m1 jan > sismember m1 mar redis.sismember("m1", "mar") (integer) 0 <= false 0Monday, June 20, 2011
  51. 51. Sets smembers (get full set) Redis REPL Groovy feb m1 jan > smembers m1 redis.smembers("m1") 1) "feb" <= [feb, jan] 2) "jan" feb janMonday, June 20, 2011
  52. 52. Sets sinter (set intersection) Redis REPL Groovy feb feb m1 m2 jan mar > sinter m1 m2 redis.sinter("m1", "m2") 1) "feb" <= ["feb"] febMonday, June 20, 2011
  53. 53. Sets sdiff (set difference) Redis REPL Groovy feb feb m1 m2 jan mar > sdiff m1 m2 redis.sdiff("m1", "m2") 1) "jan" <= ["jan"] janMonday, June 20, 2011
  54. 54. Sets sunion (set union) Redis REPL Groovy feb feb m1 m2 > sunion m1 m2 jan mar 1) "mar" redis.sunion("m1", "m2") 2) "jan" mar <= ["mar", "jan", "feb"] 3) "feb" jan febMonday, June 20, 2011
  55. 55. Sorted Sets zadd (add with score) Redis REPL Groovy > zadd z1 1 jan redis.zadd("z1", 1, "jan") z1 1 jan (integer) 1 <= 1Monday, June 20, 2011
  56. 56. Sorted Sets zscore (score for member) Redis REPL Groovy 1 jan z1 2 feb > zscore z1 feb 3 mar redis.zscore("z1", "feb") "2" <= 2.0 2Monday, June 20, 2011
  57. 57. Sorted Sets zrange (sorted subset) Redis REPL Groovy 1 jan z1 2 feb > zrange z1 0 1 withscores 1) "jan" 3 mar redis.zrangeWithScores("z1", 0, 1) 2) "1" <= [["jan", 1], ["feb", 2]] 3) "feb" 4) "2" 1 jan 2 febMonday, June 20, 2011
  58. 58. Sorted Sets zrangebyscore (subset having score range) Redis REPL Groovy 1 jan z1 2 feb > zrangebyscore z1 2 3 withscores 1) "feb" 3 mar redis.zrangeByScoreWithScores("z1",2,3) 2) "2" <= [["feb", 2], ["mar", 3]] 3) "mar" 4) "3" 2 feb 3 marMonday, June 20, 2011
  59. 59. Groovy UsageMonday, June 20, 2011
  60. 60. Grape @Grab Annotation #! /usr/bin/env groovy @Grab(redis.clients:jedis:2.0.0) def redis = new redis.clients.jedis.Jedis("localhost") assert "PONG" == redis.ping()Monday, June 20, 2011
  61. 61. Producer/Consumer ExampleMonday, June 20, 2011
  62. 62. Producer pushes work on a list with lpush @Grab(redis.clients:jedis:2.0.0) redis = new redis.clients.jedis.Jedis("localhost") args.each { redis.lpush("welcome-wagon", it) }Monday, June 20, 2011
  63. 63. Consumer uses blpop (blocking left pop from list) @Grab(redis.clients:jedis:2.0.0) redis = new redis.clients.jedis.Jedis("localhost") println "Joining the welcome-wagon!" while (true) { def name = redis.blpop(0, "welcome-wagon")[1] println "Welcome ${name}!" }Monday, June 20, 2011
  64. 64. Mass Producer srandmember to randomly pick female name from set @Grab(redis.clients:jedis:2.0.0) redis = new redis.clients.jedis.Jedis("localhost") if (!redis.exists("female-names")) { new File("./female-names.txt").eachLine {redis.sadd("female-names",it)} } for (i in 1..100000) { redis.lpush("welcome-wagon", redis.srandmember("female-names")) if (i % 1000 == 0) println "Adding $i" } female-names.txt from: http://antirez.com/post/autocomplete-with-redis.htmlMonday, June 20, 2011
  65. 65. Groovy DemoMonday, June 20, 2011
  66. 66. Grails Redis Plugin https://github.com/grails-plugins/grails-redisMonday, June 20, 2011
  67. 67. Plugin Config in Config.xml grails { redis { poolConfig { // jedis pool specific tweaks here, see jedis docs & src // ex: numTestsPerEvictionRun = 4 } port = 6379 host = "localhost" } }Monday, June 20, 2011
  68. 68. Provides Caching Through MemoizationMonday, June 20, 2011
  69. 69. RedisTagLib <redis:memoize key="mykey" expire="3600"> <!-- insert expensive to generate GSP content here content will be executed once, subsequent calls will pull from redis (redis.get(“mykey”)) till the key expires --> </redis:memoize>Monday, June 20, 2011
  70. 70. RedisService Spring bean wraps pool connection // overrides propertyMissing and methodMissing to delegate to redis def redisService redisService.foo = "bar" assert "bar" == redisService.foo redisService.sadd("months", "february") assert true == redisService.sismember("months", "february")Monday, June 20, 2011
  71. 71. RedisService template methods manage pooled Redis connection redisService.withRedis { Jedis redis -> redis.set("foo", "bar") }Monday, June 20, 2011
  72. 72. RedisService template methods manage pooled Redis connection redisService.withRedis { Jedis redis -> redisService.withTransaction { Transaction transaction -> transaction.set("qux", "baz") assertNull redis.get("qux") } assertEquals "baz", redis.get("qux") }Monday, June 20, 2011
  73. 73. RedisService String memoization redisService.memoize("my-key") { Jedis redis -> // expensive operation we only want to execute once } def ONE_HOUR = 3600 // with optional timeout in seconds redisService.memoize("my-key-with-timeout", ONE_HOUR) { Jedis redis -> // expensive operation we want to execute every hour }Monday, June 20, 2011
  74. 74. RedisService Domain Class memoization (stores IDs hydrates from DB) def key = "user:$id:friends-books" redisService.memoizeDomainList(Book, key, ONE_HOUR) { redis -> // expensive process to calculate all friend’s books // stores list of Book ids, hydrates them from DB }Monday, June 20, 2011
  75. 75. Example Showing Products with Sort/Filter/Pagination CriteriaMonday, June 20, 2011
  76. 76. Other Memoization Methods memoizeHash, memoizeHashField, memoizeScore (sorted set score)Monday, June 20, 2011
  77. 77. Grails Redis-GORM Plugin http://grails.github.com/inconsequential/redis/Monday, June 20, 2011
  78. 78. Uses SpringData to abstract data layerMonday, June 20, 2011
  79. 79. Can be used in conjunction with HibernateMonday, June 20, 2011
  80. 80. Partial support for GORM including Dynamic Finders, Criteria, Named Queries and “Transactions”Monday, June 20, 2011
  81. 81. Limitations It requires explicit index mapping on fields you want to query package com.example class Author { String name static mapWith = "redis" static hasMany = [books: Book] static mapping = { name index:true } }Monday, June 20, 2011
  82. 82. Under The Covers MONITOR output for new Author(name: "Stephen King").save() 1308027697.922839 "INCR" "com.example.Author.next_id" 1308027697.940021 "HMSET" "com.example.Author:1" "name" "Stephen King" "version" "0" 1308027697.940412 "SADD" "com.example.Author.all" "1" 1308027697.943318 "SADD" "com.example.Author:id:1" "1" 1308027697.943763 "ZADD" "com.example.Author:id:sorted" "1.0" "1" 1308027697.944911 "SADD" "com.example.Author:name:Stephen+King" "1"Monday, June 20, 2011
  83. 83. Questions?Monday, June 20, 2011
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×