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

Next Level Redis With Spring Data (JavaDay2016)

Next Level Redis With Spring Data (JavaDay2016)

Redis is one of the most popular Open Source Key Value Stores these days. Spring Data Redis Lead Christoph Strobl takes you on a tour through high availability and cluster scenarios. He explains Redis commands and data structures, object hash mapping and secondary indexes as well as the Spring Data Repository abstraction that let’s you seamlessly interact with Redis.

Christoph Strobl

October 15, 2016
Tweet

More Decks by Christoph Strobl

Other Decks in Programming

Transcript

  1. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    1
    Next Level Redis
    With Spring
    Christoph Strobl
    @stroblchristoph
    JAVADAY KYIV 2016

    View Slide

  2. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    2

    View Slide

  3. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    3
    Remote
    Directory
    Server
    KeyValue Store
    Cache
    Queues
    Analytics
    Geo Indexing
    Session Store
    Distributed Locks
    Strings
    Hashes
    Lists
    Sets
    Sorted Sets
    HyperLogLog
    Transactions
    Pub/Sub
    Scripting

    View Slide

  4. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    4
    DataTypes & Commands

    View Slide

  5. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    5
    V A L U E
    SET
    GET
    K E Y
    STRLEN
    APPEND
    GETSET
    GETRANGE
    SETRANGE
    3 . 1 4 1
    INCR
    INCRBY
    DECR
    DECRBY
    INCRBYFLOAT
    DECRBYFLOAT
    SETNX
    Strings

    View Slide

  6. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Arya
    6
    S T A R K
    LPUSH
    LPOP
    RPUSH
    RPOP
    LLEN
    LSET
    LGET
    LINSERT
    LRANGE
    LREM
    Lists
    Jon Bran

    View Slide

  7. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    7
    Sets
    L A N N I S T E R
    Tyrion
    Jaime
    Cersei
    SADD
    SPOP
    SREM
    SCARD
    SMEMBERS
    SISMEMBER
    Tywin

    View Slide

  8. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    8
    ZSets
    L A N N I S T E R
    Tywin
    Cersei
    Jaime 2
    1
    2
    Tyrion 3
    ZADD
    ZRANGE
    ZRANGEBYSCORE
    ZCARD
    ZCOUNT
    ZSCORE
    ZREM
    ZREVRANGEBYSCORE
    ZREVRANGE
    ZINCRBY
    score

    View Slide

  9. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    9
    Hashes
    D A E N E R Y S
    lastname
    occupation
    pet
    targaryen
    queen
    dragon
    HSET
    HGET
    HMSET
    HMGET
    HVALS
    HKEYS
    HDEL
    HLEN
    HGETALL

    View Slide

  10. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    10
    Geo
    W E S T E R O S
    GEOADD
    GEODIST
    GEOHASH
    GEOPOS Castle Black
    Winterfell
    King’s Landing
    13.361389, 38.115556
    15.087269, 37.502669
    13.583333, 37.316667
    347903…
    347909…
    347944…
    GEORADIUS
    GEORADIUSBYMEMBER
    ZREM = GEODEL ¯\_(ツ)_/¯

    View Slide

  11. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    11
    multi/exec
    MULTI
    EXEC
    SET key value
    GET key
    QUEUED
    QUEUED
    [OK, value]
    DISCARD
    OK

    View Slide

  12. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    12
    Single Node / HA / Cluster

    View Slide

  13. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    13

    View Slide

  14. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    14

    View Slide

  15. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    15
    S
    S
    S

    View Slide

  16. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    16
    Slots:

    0 - 8192
    Slots:

    8993 - 16384

    View Slide

  17. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    17
    Spring Data Redis

    View Slide

  18. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    18
    Redis Driver

    Jedis / Lettuce
    Serializer
    Template
    Connection
    Factory
    String
    Jackson
    JDK Connection
    ClusterConnection

    View Slide

  19. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    19
    DEMO

    cluster commands

    View Slide

  20. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    20
    Object Hash Mapping

    View Slide

  21. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    21
    class Person {
    String id;
    String firstname;
    String lastname;
    Address address;
    }
    SET {person}:123
    {
    "id":123,
    "firstname":"jon",
    "lastname":"snow",
    "address": { "country":"the north" }
    }
    HMSET {person}:123
    _class org.example.Person
    id 123
    firstname jon
    lastname snow
    address.country the north
    GET {person}:123
    HGETALL {person}:123
    HGET {person}:123 id lastname
    HSET {person}:123 lastname doe
    HEXISTS {person}:123 age
    ...

    View Slide

  22. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    22
    DEMO

    Object Hash Mapping

    View Slide

  23. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    23
    Secondary Indexes

    View Slide

  24. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    24
    class Person {
    String id;
    String firstname;
    String lastname;
    Address address;
    }
    HMSET {person}:123
    _class org.example.Person
    id 123
    firstname jon
    lastname snow
    address.country the north
    SADD {person}:firstname:jon 123
    SADD {person}:lastname:snow 123
    123
    {person}:firstname:jon
    101
    567
    721
    {person}:lastname:snow
    122
    133
    987
    SINTER {person}:firstname:jon {person}:lastname:snow
    SUNION {person}:firstname:jon {person}:lastname:snow

    View Slide

  25. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    25
    @RedisHash("{person}")
    class Person {
    @Id String id;
    @Indexed String firstname;
    @Indexed String lastname;
    Address address;
    }
    HMSET {person}:123 id 123 firstname john ...
    SADD {person}:firstname:john 123
    SADD {person}:lastname:snow 123
    SINTER {person}:firstname:jon {person}:lastname:snow
    interface PersonRepository extends CrudRepository {
    "
    "
    }
    repo.save(new Person("123", "john", "snow"));
    repo.update(
    newPartialUpdate("123", Person.class)
    .set("firstname", "jon"));
    HSET {person}:123 firstname jon
    SREM {person}:firstname:john 123
    SADD {person}:firstname:jon 123
    List findByFirstnameAndLastname(String firstname, String lastname);
    repo.findByFirstnameAndLastname("jon", "snow");
    HGETALL {person}:123

    View Slide

  26. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    26
    DEMO

    Redis Repositories

    View Slide

  27. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    27
    Expire Commands
    Custom Conversions
    SpEL based indexing
    References

    View Slide

  28. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    28
    CC BY 2.0 

    https://www.flickr.com/photos/crystalflickr/2317183342

    View Slide

  29. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    29
    Reactive Data Access
    Geo Indexes

    View Slide

  30. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    30
    @RedisHash("{person}")
    class Person {
    @Id String id;
    Address address;
    }
    HMSET {person}:123 id 123 firstname jon ...
    GEOADD {person}:address:location 15.087269 37.502669 123
    GEORADIUS {person}:address:location 15.0 37.0 200.0 km
    interface PersonRepository extends CrudRepository {
    "
    "
    }
    repo.save(new Person("123", "jon", "snow")
    .withAddress(
    new Address("winterfell",
    new Point(15.087269D, 37.502669D)))
    );
    List findByAddressLocationNear(Point location, Distance distance);
    repo.findByAddressLocationNear(
    new Point(15D, 37D),
    new Distance(200, Metrics.KILOMETERS));
    HGETALL {person}:123
    class Address {
    String city;
    @GeoIndexed Point location;
    }

    View Slide

  31. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    31
    public interface ReactiveStringCommands {
    "
    /**
    * Set {@literal value} for {@literal key}.
    *
    * @param key must not be {@literal null}.
    * @param value must not be {@literal null}.
    * @return
    */
    default Mono set(ByteBuffer key, ByteBuffer value) {
    return set(Mono.just(SetCommand.value(key).forKey(value)))
    .next().map(BooleanResponse::getOutput);
    }
    "
    /**
    * Execute each {@link SetCommand} separately.
    *
    * @param commands must not be {@literal null}.
    * @return
    */
    Flux> set(Publisher commands);
    "
    /**
    * Get single element stored at {@literal key}.
    *
    * @param key must not be {@literal null}.
    * @return empty {@link Mono} in case {@literal key} does not exist.
    */
    default Mono get(ByteBuffer key) {
    return get(Mono.just(new KeyCommand(key))).next().map((result) -> result.getOutput());
    }

    View Slide

  32. Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a
    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Learn More. Stay Connected.
    Follow us @SpringData
    @springcentral
    spring.io/blog
    @pivotal
    pivotal.io/blog
    @pivotalcf
    http://engineering.pivotal.io
    Talk to us spring-projects/spring-data

    View Slide