$30 off During Our Annual Pro Sale. View Details »

Going reactive with Spring Data

Going reactive with Spring Data

Mark Paluch

March 10, 2017
Tweet

More Decks by Mark Paluch

Other Decks in Technology

Transcript

  1. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Going reactive with Spring Data
    Mark Paluch, Pivotal Software, Inc.
    @mp911de

    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/
    @mp911de
    github.com/mp911de
    paluch.biz
    Mark Paluch

    View Slide

  3. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Accessing Data today
    3
    Request Data access
    Remote data
    service

    View Slide

  4. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Accessing Data today
    3
    Request Data access
    Remote data
    service

    View Slide

  5. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Accessing Data today
    3
    Request Data access
    Remote data
    service
    I/O Wait

    View Slide

  6. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Accessing Data today
    3
    Request Data access
    Remote data
    service
    I/O Wait

    View Slide

  7. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Accessing Data today
    3
    Request Data access
    Remote data
    service
    I/O Wait
    This one is
    waiting

    View Slide

  8. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Accessing Data today
    3
    Request Data access
    Remote data
    service
    I/O Wait
    This one is
    waiting

    View Slide

  9. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Accessing Data today
    4
    Request Data access
    Remote data
    service

    View Slide

  10. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Accessing Data today
    4
    Request Data access
    Remote data
    service
    Bulk fetch

    View Slide

  11. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Accessing Data today
    4
    Request Data access
    Remote data
    service
    Multiple
    calls
    Bulk fetch

    View Slide

  12. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Accessing Data today
    4
    Request Data access
    Remote data
    service
    Multiple
    calls
    Bulk fetch

    View Slide

  13. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Accessing Data today
    4
    Request Data access
    Remote data
    service
    Multiple
    calls
    Bulk fetch

    View Slide

  14. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Asynchronous data access
    5
    Request Data access


    View Slide

  15. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Asynchronous data access
    5
    Request Data access


    View Slide

  16. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Asynchronous data access
    5
    Request Data access


    View Slide

  17. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Asynchronous data access
    5
    Request Data access


    View Slide

  18. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Asynchronous data access
    5
    Request Data access
    Requires
    synchronization


    View Slide

  19. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Asynchronous data access
    5
    Request Data access
    Requires
    synchronization
    Still waiting, eh?


    View Slide

  20. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Asynchronous data access
    5
    Request Data access
    Requires
    synchronization
    Still waiting, eh?


    View Slide

  21. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Conditional asynchronous access
    6

    View Slide

  22. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    7

    View Slide

  23. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    8

    View Slide

  24. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    9

    View Slide

  25. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    10

    View Slide

  26. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    11

    View Slide

  27. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Conditional asynchronous access
    12
    Future Hell

    View Slide

  28. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Conditional asynchronous access
    12
    Future Hell

    View Slide

  29. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    ! Keeps more Threads busy
    ! Transformation improved (since Java 5, java.util.concurrent)
    ! Processing starts at the time all elements are available (effectively)
    13
    Asynchronous Data Access

    View Slide

  30. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    ! Keep resources busy
    ! Connection contention
    ! Usually synchronous/blocking
    ! Multiple requests
    ! Asynchronous isn’t always a good answer
    14
    Todays’ Data Access

    View Slide

  31. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Scaling
    15

    View Slide

  32. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Scaling
    15




    View Slide

  33. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Scaling
    15







    View Slide

  34. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Change the level of abstraction
    16
    Data as Stream
    Reactive execution model

    View Slide

  35. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    17
    List
    Iterator
    Future
    Future>

    View Slide

  36. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    17
    List
    Iterator
    Future
    Future>
    .get()
    for-each .hasNext()

    View Slide

  37. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    17
    List
    Iterator
    Future
    Future>
    .get()
    for-each .hasNext()
    Request thread pulls

    View Slide

  38. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    18
    Publisher

    View Slide

  39. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    18
    Publisher
    I/O thread
    push

    View Slide

  40. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    19
    Publisher
    Stream
    (Publisher)
    Subscriber
    Demand
    Data

    View Slide

  41. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    19
    Publisher
    Stream
    (Publisher)
    Subscriber
    Demand
    Data

    View Slide

  42. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    20
    Publisher
    Stream
    (Publisher)
    Subscriber
    Demand
    Data

    View Slide

  43. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    20
    Publisher
    Stream
    (Publisher)
    Subscriber
    Demand
    Data

    View Slide

  44. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    ! Immutable
    ! Push-Stream
    ! Iterator, java.stream.Stream are Pull-Streams
    ! Demand-Aware (Backpressure)
    21
    Reactive Streams

    View Slide

  45. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Going reactive with Spring Data
    22
    Project Reactor 3.0 Spring Data 2.0
    Spring Framework 5.0

    View Slide

  46. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    23
    Mono
    Flux

    View Slide

  47. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    23
    Mono
    Flux
    0..1..Error
    0..N..Error

    View Slide

  48. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    ! Reactive Template API
    ! Reactive Repository support
    ! Supported data stores (Reactive Streams)
    ! MongoDB
    ! Apache Cassandra
    ! Redis
    24
    Spring Data

    View Slide

  49. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    25
    Reactive Template API
    Mono insert(T objectToSave)
    Mono insert(Mono objectToSave)

    View Slide

  50. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    26
    Demo
    Reactive Template API

    View Slide

  51. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    27
    Reactive Repository API
    public interface ReactivePersonRepository extends
    ReactiveCrudRepository {


    Flux findByLastname(String lastname);


    @Query("{ 'firstname': ?0 }")

    Mono customQuery(String firstname);
    Flux findByLastname(Mono lastname);
    }

    View Slide

  52. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    28
    Reactive Repository API
    public interface ReactivePersonRepository extends
    ReactiveCrudRepository {


    Flux findByLastname(String lastname);


    @Query("{ 'firstname': ?0 }")

    Mono customQuery(String firstname);
    Flux findByLastname(Mono lastname);
    }

    View Slide

  53. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    29
    Reactive Repository API
    public interface ReactivePersonRepository extends
    ReactiveCrudRepository {


    Flux findByLastname(String lastname);


    @Query("{ 'firstname': ?0 }")

    Mono customQuery(String firstname);
    Flux findByLastname(Mono lastname);
    }

    View Slide

  54. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    30
    Reactive Repository API
    public interface ReactivePersonRepository extends
    ReactiveCrudRepository {


    Flux findByLastname(String lastname);


    @Query("{ 'firstname': ?0 }")

    Mono customQuery(String firstname);
    Flux findByLastname(Mono lastname);
    }

    View Slide

  55. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    31
    Demo
    Reactive Repositories

    View Slide

  56. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    ! End-to-End reactive experience
    ! RxJava Interoperability at Repository level
    ! Infinite Streams
    ! MongoDB Tailable Cursors
    ! Redis Pub/Sub
    32
    Spring Data Reactive Features

    View Slide

  57. 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/
    Feedback Welcome

    View Slide

  58. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
    Resources
    ! Spring Data Examples – Repository @ Github
    ! Spring Data wiki (Release trains, planning) – Wiki
    ! Spring projects release calendar – Google Calendar
    ! Slides – Speakerdeck (mp911.de/going-reactive)
    ! Chat with us on Gitter!
    34
    @mp911de

    View Slide