SlideShare une entreprise Scribd logo
1  sur  83
Télécharger pour lire hors ligne
Redis & Groovy & Grails



                               by Ted Naleid
                               http://naleid.com

Monday, June 20, 2011
“Redis is a collection of data
                        structures exposed over the
                                  network”


                                                  from: http://nosql.mypopescu.com/post/5403851771/what-is-redis

Monday, June 20, 2011
key/value store

               like memcached on steroids


Monday, June 20, 2011
Strings, Integers,
                                Lists, Hashes,
                              Sets & Sorted Sets
                        (& commonly expected operations with each data type)




Monday, June 20, 2011
in-memory storage



Monday, June 20, 2011
single-threaded



Monday, June 20, 2011
fast



Monday, June 20, 2011
“Memory is the new Disk,
                         Disk is the new Tape”
                                 - Jim Gray




Monday, June 20, 2011
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
CPU Register
                            1 yard




                                       photo: http://www.flickr.com/photos/limonada/904754668/

Monday, June 20, 2011
L2 Cache
                         10 yards




                                    photo: http://www.flickr.com/photos/plentyofants/2749262107

Monday, June 20, 2011
Memory
                        100 yards




                                    photo: http://www.flickr.com/photos/billmcintyre/264905933

Monday, June 20, 2011
Disk
        Minneapolis to New York to Miami
                   to Seattle
                        ~5600 miles




Monday, June 20, 2011
Simple wire protocol that
                              matches API


Monday, June 20, 2011
% 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
    second


Monday, June 20, 2011
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 one




Monday, June 20, 2011
No dependencies
                            (451KB download)




Monday, June 20, 2011
Used in production by high
                  traffic sites that you’ve used


Monday, June 20, 2011
stackoverflow, craigslist,
                         github, disqus, digg &
                         blizzard entertainment


Monday, June 20, 2011
financially supported by
                         VMware/SpringSource


Monday, June 20, 2011
simple data structures make
                       redis flexible


Monday, June 20, 2011
write-through caching/
                             memoization


Monday, June 20, 2011
producer/consumer
                         message queues


Monday, June 20, 2011
publish/subscribe



Monday, June 20, 2011
atomic sequences/counters



Monday, June 20, 2011
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/sorting




Monday, June 20, 2011
Redis Commands



Monday, June 20, 2011
Great Online Reference
                               http://redis.io/commands




Monday, June 20, 2011
Strings
                                            set


                        Redis REPL                            Groovy



                 > set foo bar                          redis.set("foo", "bar")
                                      foo         bar
                 OK                                     OK




Monday, June 20, 2011
Strings
                                            get


                        Redis REPL                            Groovy


                                      foo         bar

                 > get foo                              redis.get("foo")
                 "bar"                                  "bar"

                                                  bar




Monday, June 20, 2011
Exists
                                     exists (check key existence)


                        Redis REPL                                   Groovy


                                              foo     (nil)

           > exists foo                                             redis.exists("foo")
           (integer) 0                                              <= false

                                                        0




Monday, June 20, 2011
Integers
                                     incr (increment)


                        Redis REPL                       Groovy


                                        foo      1

           > incr foo                                   redis.incr("foo")
           (integer) 1                                  <= 1

                                                 1




Monday, June 20, 2011
Integers
                                     decr (decrement)


                        Redis REPL                       Groovy


                                        foo      0

           > decr foo                                   redis.decr("foo")
           (integer) 0                                  <= 0

                                                 0




Monday, June 20, 2011
Lists
                                     rpush (right push)


                        Redis REPL                                 Groovy



           > rpush foo baz                                redis.rpush("foo", "baz")
                                         foo     baz
           (integer) 1                                    <= 1




Monday, June 20, 2011
Lists
                                     rpush (right push)


                        Redis REPL                                 Groovy



           > rpush foo qux                                redis.rpush("foo", "qux")
                                      foo     baz qux
           (integer) 2                                    <= 2




Monday, June 20, 2011
Lists
                                     lpush (left push)


                        Redis REPL                                    Groovy



           > lpush foo bar                                   redis.lpush("foo", "bar")
                                     foo     bar   baz qux
           (integer) 3                                       <= 3




Monday, June 20, 2011
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 qux




Monday, June 20, 2011
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   baz




Monday, June 20, 2011
Lists
                                     lpop (left pop)


                        Redis REPL                       Groovy


                                     foo      baz qux

           > lpop foo                                   redis.lpop("foo")
           "bar"                                        <= "bar"

                                                bar




Monday, June 20, 2011
Lists
                                     rpop (right pop)


                        Redis REPL                       Groovy


                                        foo     baz

           > rpop foo                                   redis.rpop("foo")
           "qux"                                        <= "qux"

                                                qux




Monday, June 20, 2011
Hashes
                                     hset (set key → value)


                        Redis REPL                                        Groovy



           > hset foo bar baz                                 redis.hset("foo", "bar", "baz")
                                     foo     bar    baz
           (integer) 1                                        <= 1




Monday, June 20, 2011
Hashes
                                     hset (set key → value)


                        Redis REPL                                         Groovy



                                              bar     baz
           > hset foo qux quxx                                redis.hset("foo", "qux", "quxx")
                                      foo
           (integer) 1                                        <= 1
                                              qux     quxx




Monday, June 20, 2011
Hashes
                                     hget (get value for key)


                        Redis REPL                                       Groovy

                                               bar         baz
                                      foo
                                               qux         quxx
           > hget foo bar                                         redis.hget("foo", "bar")
           "baz"                                                  <= "baz"

                                                     baz




Monday, June 20, 2011
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     quxx




Monday, June 20, 2011
Hashes
                                     hvals (hash values)


                        Redis REPL                              Groovy
                                              bar     baz
                                      foo
                                              qux     quxx

           > hvals foo                                       redis.hvals("foo")
           1) "baz"                                          <= [baz, quxx]
           2) "quxx"
                                                baz quxx




Monday, June 20, 2011
Hashes
                                     hkeys (hash keys)


                        Redis REPL                            Groovy
                                             bar     baz
                                     foo
                                             qux    quxx

           > hkeys foo                                     redis.hkeys("foo")
           1) "bar"                                        <= [bar, qux]
           2) "qux"
                                               bar qux




Monday, June 20, 2011
Sets
                                     sadd (set add)


                        Redis REPL                           Groovy



            > sadd m1 jan                             redis.sadd("m1", "jan")
                                       m1      jan
            (integer) 1                               <= 1




Monday, June 20, 2011
Sets
                                     sadd (set add)


                        Redis REPL                          Groovy



                                               feb
            > sadd m1 feb                             redis.sadd("m1", "feb")
                                       m1
            (integer) 1                               <= 1
                                               jan




Monday, June 20, 2011
Sets
                                     sismember (membership test)


                        Redis REPL                                           Groovy
                                                     feb
                                              m1
                                                     jan
            > sismember m1 jan                                     redis.sismember("m1", "jan")
            (integer) 1                                            <= true

                                                      1




Monday, June 20, 2011
Sets
                                     sismember (membership test)


                        Redis REPL                                          Groovy
                                                     feb
                                              m1
                                                     jan
            > sismember m1 mar                                     redis.sismember("m1", "mar")
            (integer) 0                                            <= false

                                                      0




Monday, June 20, 2011
Sets
                                     smembers (get full set)


                        Redis REPL                                   Groovy
                                                    feb
                                           m1
                                                    jan
           > smembers m1                                       redis.smembers("m1")
           1) "feb"                                            <= [feb, jan]
           2) "jan"
                                                    feb

                                                    jan



Monday, June 20, 2011
Sets
                                     sinter (set intersection)


                        Redis REPL                                       Groovy
                                             feb            feb
                                     m1             m2
                                             jan            mar
         > sinter m1 m2                                           redis.sinter("m1", "m2")
         1) "feb"                                                 <= ["feb"]

                                                     feb




Monday, June 20, 2011
Sets
                                      sdiff (set difference)


                        Redis REPL                                      Groovy
                                             feb           feb
                                     m1            m2
                                             jan           mar
          > sdiff m1 m2                                          redis.sdiff("m1", "m2")
          1) "jan"                                               <= ["jan"]

                                                    jan




Monday, June 20, 2011
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

                                                      feb


Monday, June 20, 2011
Sorted Sets
                                      zadd (add with score)


                        Redis REPL                                    Groovy



          > zadd z1 1 jan                                     redis.zadd("z1", 1, "jan")
                                           z1      1 jan
          (integer) 1                                         <= 1




Monday, June 20, 2011
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


                                                      2




Monday, June 20, 2011
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 feb


Monday, June 20, 2011
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 mar


Monday, June 20, 2011
Groovy Usage



Monday, June 20, 2011
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
Producer/Consumer
                             Example


Monday, June 20, 2011
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
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
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.html
Monday, June 20, 2011
Groovy Demo



Monday, June 20, 2011
Grails Redis Plugin
                        https://github.com/grails-plugins/grails-redis




Monday, June 20, 2011
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
Provides Caching Through
                              Memoization


Monday, June 20, 2011
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
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
RedisService
                        template methods manage pooled Redis connection




          redisService.withRedis { Jedis redis ->
              redis.set("foo", "bar")
          }




Monday, June 20, 2011
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
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
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
Example
                        Showing Products with Sort/Filter/Pagination Criteria




Monday, June 20, 2011
Other Memoization Methods
                        memoizeHash, memoizeHashField,
                         memoizeScore (sorted set score)




Monday, June 20, 2011
Grails Redis-GORM Plugin
                          http://grails.github.com/inconsequential/redis/




Monday, June 20, 2011
Uses SpringData to abstract
                          data layer


Monday, June 20, 2011
Can be used in conjunction
                        with Hibernate


Monday, 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.example

          class Author {

                        String name

                        static mapWith = "redis"
                        static hasMany = [books: Book]

                        static mapping = {
                            name index:true
                        }
          }
Monday, June 20, 2011
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
Questions?



Monday, June 20, 2011

Contenu connexe

Tendances

What's new in MariaDB TX 3.0
What's new in MariaDB TX 3.0What's new in MariaDB TX 3.0
What's new in MariaDB TX 3.0MariaDB plc
 
TỔ CHỨC THI CÔNG CÔNG TRÌNH
TỔ CHỨC THI CÔNG CÔNG TRÌNHTỔ CHỨC THI CÔNG CÔNG TRÌNH
TỔ CHỨC THI CÔNG CÔNG TRÌNHTPHCM
 
MySQL_SQL_Tunning_v0.1.3.docx
MySQL_SQL_Tunning_v0.1.3.docxMySQL_SQL_Tunning_v0.1.3.docx
MySQL_SQL_Tunning_v0.1.3.docxNeoClova
 
[Đồ án môn học] - Đề tài: Nghiên cứu xây dựng giải pháp thu thập và quản lý ...
[Đồ án môn học] - Đề tài: Nghiên cứu xây dựng giải pháp thu thập  và quản lý ...[Đồ án môn học] - Đề tài: Nghiên cứu xây dựng giải pháp thu thập  và quản lý ...
[Đồ án môn học] - Đề tài: Nghiên cứu xây dựng giải pháp thu thập và quản lý ...Ý Như Lê
 
New holland tc40 da tractor service repair manual
New holland tc40 da tractor service repair manualNew holland tc40 da tractor service repair manual
New holland tc40 da tractor service repair manualujdfjjjdfkkskemm
 
Huong dan tinh toan thanh phan dong cua tt gio
Huong dan tinh toan thanh phan dong cua tt gioHuong dan tinh toan thanh phan dong cua tt gio
Huong dan tinh toan thanh phan dong cua tt giovinhx1b
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Traceoysteing
 
MongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To TransactionsMongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To TransactionsMydbops
 
Performance Tuning Oracle Weblogic Server 12c
Performance Tuning Oracle Weblogic Server 12cPerformance Tuning Oracle Weblogic Server 12c
Performance Tuning Oracle Weblogic Server 12cAjith Narayanan
 
InnoDB Performance Optimisation
InnoDB Performance OptimisationInnoDB Performance Optimisation
InnoDB Performance OptimisationMydbops
 
Replication Troubleshooting in Classic VS GTID
Replication Troubleshooting in Classic VS GTIDReplication Troubleshooting in Classic VS GTID
Replication Troubleshooting in Classic VS GTIDMydbops
 
Bai giang-nguyen-ly-kinh-te-va-quan-ly-xay-dung-pgs.-luong-duc-long
Bai giang-nguyen-ly-kinh-te-va-quan-ly-xay-dung-pgs.-luong-duc-longBai giang-nguyen-ly-kinh-te-va-quan-ly-xay-dung-pgs.-luong-duc-long
Bai giang-nguyen-ly-kinh-te-va-quan-ly-xay-dung-pgs.-luong-duc-longtinh vo
 
Đồ Án Thiết Kế Cầu Dàn Thép L=55m (Kèm Bản Vẽ CAD)
Đồ Án Thiết Kế Cầu Dàn Thép L=55m (Kèm Bản Vẽ CAD) Đồ Án Thiết Kế Cầu Dàn Thép L=55m (Kèm Bản Vẽ CAD)
Đồ Án Thiết Kế Cầu Dàn Thép L=55m (Kèm Bản Vẽ CAD) nataliej4
 
Introduction to Drools
Introduction to DroolsIntroduction to Drools
Introduction to Droolsgiurca
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query TuningAlexander Rubin
 
Phan tich thiet_ke_he_thong_quan_ly_part_4
Phan tich thiet_ke_he_thong_quan_ly_part_4Phan tich thiet_ke_he_thong_quan_ly_part_4
Phan tich thiet_ke_he_thong_quan_ly_part_4caolanphuong
 
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEOClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEOAltinity Ltd
 
Tính toán thiết kế cần trục tháp sức nâng 6/1,3 tấn- tầm với lớn nhất 50m
Tính toán thiết kế cần trục tháp sức nâng 6/1,3 tấn- tầm với lớn nhất 50m Tính toán thiết kế cần trục tháp sức nâng 6/1,3 tấn- tầm với lớn nhất 50m
Tính toán thiết kế cần trục tháp sức nâng 6/1,3 tấn- tầm với lớn nhất 50m anh hieu
 

Tendances (20)

What's new in MariaDB TX 3.0
What's new in MariaDB TX 3.0What's new in MariaDB TX 3.0
What's new in MariaDB TX 3.0
 
TỔ CHỨC THI CÔNG CÔNG TRÌNH
TỔ CHỨC THI CÔNG CÔNG TRÌNHTỔ CHỨC THI CÔNG CÔNG TRÌNH
TỔ CHỨC THI CÔNG CÔNG TRÌNH
 
MySQL 5.5 Guide to InnoDB Status
MySQL 5.5 Guide to InnoDB StatusMySQL 5.5 Guide to InnoDB Status
MySQL 5.5 Guide to InnoDB Status
 
MySQL_SQL_Tunning_v0.1.3.docx
MySQL_SQL_Tunning_v0.1.3.docxMySQL_SQL_Tunning_v0.1.3.docx
MySQL_SQL_Tunning_v0.1.3.docx
 
[Đồ án môn học] - Đề tài: Nghiên cứu xây dựng giải pháp thu thập và quản lý ...
[Đồ án môn học] - Đề tài: Nghiên cứu xây dựng giải pháp thu thập  và quản lý ...[Đồ án môn học] - Đề tài: Nghiên cứu xây dựng giải pháp thu thập  và quản lý ...
[Đồ án môn học] - Đề tài: Nghiên cứu xây dựng giải pháp thu thập và quản lý ...
 
New holland tc40 da tractor service repair manual
New holland tc40 da tractor service repair manualNew holland tc40 da tractor service repair manual
New holland tc40 da tractor service repair manual
 
Huong dan tinh toan thanh phan dong cua tt gio
Huong dan tinh toan thanh phan dong cua tt gioHuong dan tinh toan thanh phan dong cua tt gio
Huong dan tinh toan thanh phan dong cua tt gio
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Trace
 
MongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To TransactionsMongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To Transactions
 
Performance Tuning Oracle Weblogic Server 12c
Performance Tuning Oracle Weblogic Server 12cPerformance Tuning Oracle Weblogic Server 12c
Performance Tuning Oracle Weblogic Server 12c
 
InnoDB Performance Optimisation
InnoDB Performance OptimisationInnoDB Performance Optimisation
InnoDB Performance Optimisation
 
Replication Troubleshooting in Classic VS GTID
Replication Troubleshooting in Classic VS GTIDReplication Troubleshooting in Classic VS GTID
Replication Troubleshooting in Classic VS GTID
 
Bai giang-nguyen-ly-kinh-te-va-quan-ly-xay-dung-pgs.-luong-duc-long
Bai giang-nguyen-ly-kinh-te-va-quan-ly-xay-dung-pgs.-luong-duc-longBai giang-nguyen-ly-kinh-te-va-quan-ly-xay-dung-pgs.-luong-duc-long
Bai giang-nguyen-ly-kinh-te-va-quan-ly-xay-dung-pgs.-luong-duc-long
 
Đồ Án Thiết Kế Cầu Dàn Thép L=55m (Kèm Bản Vẽ CAD)
Đồ Án Thiết Kế Cầu Dàn Thép L=55m (Kèm Bản Vẽ CAD) Đồ Án Thiết Kế Cầu Dàn Thép L=55m (Kèm Bản Vẽ CAD)
Đồ Án Thiết Kế Cầu Dàn Thép L=55m (Kèm Bản Vẽ CAD)
 
Introduction to Drools
Introduction to DroolsIntroduction to Drools
Introduction to Drools
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query Tuning
 
Phan tich thiet_ke_he_thong_quan_ly_part_4
Phan tich thiet_ke_he_thong_quan_ly_part_4Phan tich thiet_ke_he_thong_quan_ly_part_4
Phan tich thiet_ke_he_thong_quan_ly_part_4
 
Spring Batch
Spring BatchSpring Batch
Spring Batch
 
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEOClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
ClickHouse tips and tricks. Webinar slides. By Robert Hodges, Altinity CEO
 
Tính toán thiết kế cần trục tháp sức nâng 6/1,3 tấn- tầm với lớn nhất 50m
Tính toán thiết kế cần trục tháp sức nâng 6/1,3 tấn- tầm với lớn nhất 50m Tính toán thiết kế cần trục tháp sức nâng 6/1,3 tấn- tầm với lớn nhất 50m
Tính toán thiết kế cần trục tháp sức nâng 6/1,3 tấn- tầm với lớn nhất 50m
 

En vedette

Migrating from MySQL to MongoDB at Wordnik
Migrating from MySQL to MongoDB at WordnikMigrating from MySQL to MongoDB at Wordnik
Migrating from MySQL to MongoDB at WordnikTony Tam
 
MongoDB 2.8 Replication Internals: Fitting it all together
MongoDB 2.8 Replication Internals: Fitting it all togetherMongoDB 2.8 Replication Internals: Fitting it all together
MongoDB 2.8 Replication Internals: Fitting it all togetherScott Hernandez
 
redis 适用场景与实现
redis 适用场景与实现redis 适用场景与实现
redis 适用场景与实现iammutex
 
Lessons Learned Migrating 2+ Billion Documents at Craigslist
Lessons Learned Migrating 2+ Billion Documents at CraigslistLessons Learned Migrating 2+ Billion Documents at Craigslist
Lessons Learned Migrating 2+ Billion Documents at CraigslistJeremy Zawodny
 
深入了解Redis
深入了解Redis深入了解Redis
深入了解Redisiammutex
 
Redis for the Everyday Developer
Redis for the Everyday DeveloperRedis for the Everyday Developer
Redis for the Everyday DeveloperRoss Tuck
 
Redis in Practice
Redis in PracticeRedis in Practice
Redis in PracticeNoah Davis
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to RedisDvir Volk
 

En vedette (10)

Redis介绍
Redis介绍Redis介绍
Redis介绍
 
Migrating from MySQL to MongoDB at Wordnik
Migrating from MySQL to MongoDB at WordnikMigrating from MySQL to MongoDB at Wordnik
Migrating from MySQL to MongoDB at Wordnik
 
MongoDB 2.8 Replication Internals: Fitting it all together
MongoDB 2.8 Replication Internals: Fitting it all togetherMongoDB 2.8 Replication Internals: Fitting it all together
MongoDB 2.8 Replication Internals: Fitting it all together
 
redis 适用场景与实现
redis 适用场景与实现redis 适用场景与实现
redis 适用场景与实现
 
Lessons Learned Migrating 2+ Billion Documents at Craigslist
Lessons Learned Migrating 2+ Billion Documents at CraigslistLessons Learned Migrating 2+ Billion Documents at Craigslist
Lessons Learned Migrating 2+ Billion Documents at Craigslist
 
深入了解Redis
深入了解Redis深入了解Redis
深入了解Redis
 
Redis for the Everyday Developer
Redis for the Everyday DeveloperRedis for the Everyday Developer
Redis for the Everyday Developer
 
A Brief MongoDB Intro
A Brief MongoDB IntroA Brief MongoDB Intro
A Brief MongoDB Intro
 
Redis in Practice
Redis in PracticeRedis in Practice
Redis in Practice
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 

Dernier

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 

Dernier (20)

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 

Redis & Groovy integration for caching and messaging

  • 1. Redis & Groovy & Grails by Ted Naleid http://naleid.com Monday, June 20, 2011
  • 2. “Redis is a collection of data structures exposed over the network” from: http://nosql.mypopescu.com/post/5403851771/what-is-redis Monday, June 20, 2011
  • 3. key/value store like memcached on steroids Monday, June 20, 2011
  • 4. Strings, Integers, Lists, Hashes, Sets & Sorted Sets (& commonly expected operations with each data type) Monday, June 20, 2011
  • 8. “Memory is the new Disk, Disk is the new Tape” - Jim Gray Monday, June 20, 2011
  • 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. CPU Register 1 yard photo: http://www.flickr.com/photos/limonada/904754668/ Monday, June 20, 2011
  • 11. L2 Cache 10 yards photo: http://www.flickr.com/photos/plentyofants/2749262107 Monday, June 20, 2011
  • 12. Memory 100 yards photo: http://www.flickr.com/photos/billmcintyre/264905933 Monday, June 20, 2011
  • 13. Disk Minneapolis to New York to Miami to Seattle ~5600 miles Monday, June 20, 2011
  • 14. Simple wire protocol that matches API Monday, June 20, 2011
  • 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 second Monday, June 20, 2011
  • 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 one Monday, June 20, 2011
  • 17. No dependencies (451KB download) Monday, June 20, 2011
  • 18. Used in production by high traffic sites that you’ve used Monday, June 20, 2011
  • 19. stackoverflow, craigslist, github, disqus, digg & blizzard entertainment Monday, June 20, 2011
  • 20. financially supported by VMware/SpringSource Monday, June 20, 2011
  • 21. simple data structures make redis flexible Monday, June 20, 2011
  • 22. write-through caching/ memoization Monday, June 20, 2011
  • 23. producer/consumer message queues Monday, June 20, 2011
  • 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/sorting Monday, June 20, 2011
  • 28. Great Online Reference http://redis.io/commands Monday, June 20, 2011
  • 29. Strings set Redis REPL Groovy > set foo bar redis.set("foo", "bar") foo bar OK OK Monday, June 20, 2011
  • 30. Strings get Redis REPL Groovy foo bar > get foo redis.get("foo") "bar" "bar" bar Monday, June 20, 2011
  • 31. Exists exists (check key existence) Redis REPL Groovy foo (nil) > exists foo redis.exists("foo") (integer) 0 <= false 0 Monday, June 20, 2011
  • 32. Integers incr (increment) Redis REPL Groovy foo 1 > incr foo redis.incr("foo") (integer) 1 <= 1 1 Monday, June 20, 2011
  • 33. Integers decr (decrement) Redis REPL Groovy foo 0 > decr foo redis.decr("foo") (integer) 0 <= 0 0 Monday, June 20, 2011
  • 34. Lists rpush (right push) Redis REPL Groovy > rpush foo baz redis.rpush("foo", "baz") foo baz (integer) 1 <= 1 Monday, June 20, 2011
  • 35. Lists rpush (right push) Redis REPL Groovy > rpush foo qux redis.rpush("foo", "qux") foo baz qux (integer) 2 <= 2 Monday, June 20, 2011
  • 36. Lists lpush (left push) Redis REPL Groovy > lpush foo bar redis.lpush("foo", "bar") foo bar baz qux (integer) 3 <= 3 Monday, June 20, 2011
  • 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 qux Monday, June 20, 2011
  • 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 baz Monday, June 20, 2011
  • 39. Lists lpop (left pop) Redis REPL Groovy foo baz qux > lpop foo redis.lpop("foo") "bar" <= "bar" bar Monday, June 20, 2011
  • 40. Lists rpop (right pop) Redis REPL Groovy foo baz > rpop foo redis.rpop("foo") "qux" <= "qux" qux Monday, June 20, 2011
  • 41. Hashes hset (set key → value) Redis REPL Groovy > hset foo bar baz redis.hset("foo", "bar", "baz") foo bar baz (integer) 1 <= 1 Monday, June 20, 2011
  • 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 quxx Monday, June 20, 2011
  • 43. Hashes hget (get value for key) Redis REPL Groovy bar baz foo qux quxx > hget foo bar redis.hget("foo", "bar") "baz" <= "baz" baz Monday, June 20, 2011
  • 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 quxx Monday, June 20, 2011
  • 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 quxx Monday, June 20, 2011
  • 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 qux Monday, June 20, 2011
  • 47. Sets sadd (set add) Redis REPL Groovy > sadd m1 jan redis.sadd("m1", "jan") m1 jan (integer) 1 <= 1 Monday, June 20, 2011
  • 48. Sets sadd (set add) Redis REPL Groovy feb > sadd m1 feb redis.sadd("m1", "feb") m1 (integer) 1 <= 1 jan Monday, June 20, 2011
  • 49. Sets sismember (membership test) Redis REPL Groovy feb m1 jan > sismember m1 jan redis.sismember("m1", "jan") (integer) 1 <= true 1 Monday, June 20, 2011
  • 50. Sets sismember (membership test) Redis REPL Groovy feb m1 jan > sismember m1 mar redis.sismember("m1", "mar") (integer) 0 <= false 0 Monday, June 20, 2011
  • 51. Sets smembers (get full set) Redis REPL Groovy feb m1 jan > smembers m1 redis.smembers("m1") 1) "feb" <= [feb, jan] 2) "jan" feb jan Monday, June 20, 2011
  • 52. Sets sinter (set intersection) Redis REPL Groovy feb feb m1 m2 jan mar > sinter m1 m2 redis.sinter("m1", "m2") 1) "feb" <= ["feb"] feb Monday, June 20, 2011
  • 53. Sets sdiff (set difference) Redis REPL Groovy feb feb m1 m2 jan mar > sdiff m1 m2 redis.sdiff("m1", "m2") 1) "jan" <= ["jan"] jan Monday, June 20, 2011
  • 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 feb Monday, June 20, 2011
  • 55. Sorted Sets zadd (add with score) Redis REPL Groovy > zadd z1 1 jan redis.zadd("z1", 1, "jan") z1 1 jan (integer) 1 <= 1 Monday, June 20, 2011
  • 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 2 Monday, June 20, 2011
  • 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 feb Monday, June 20, 2011
  • 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 mar Monday, June 20, 2011
  • 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. Producer/Consumer Example Monday, June 20, 2011
  • 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. 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. 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.html Monday, June 20, 2011
  • 66. Grails Redis Plugin https://github.com/grails-plugins/grails-redis Monday, June 20, 2011
  • 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. Provides Caching Through Memoization Monday, June 20, 2011
  • 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. 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. RedisService template methods manage pooled Redis connection redisService.withRedis { Jedis redis -> redis.set("foo", "bar") } Monday, June 20, 2011
  • 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. 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. 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. Example Showing Products with Sort/Filter/Pagination Criteria Monday, June 20, 2011
  • 76. Other Memoization Methods memoizeHash, memoizeHashField, memoizeScore (sorted set score) Monday, June 20, 2011
  • 77. Grails Redis-GORM Plugin http://grails.github.com/inconsequential/redis/ Monday, June 20, 2011
  • 78. Uses SpringData to abstract data layer Monday, June 20, 2011
  • 79. Can be used in conjunction with Hibernate Monday, June 20, 2011
  • 80. Partial support for GORM including Dynamic Finders, Criteria, Named Queries and “Transactions” Monday, June 20, 2011
  • 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. 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