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

Neo4j and the JVM

Neo4j and the JVM

This talk is mostly about using the Neo4j Java Driver, displays the different types of sessions as well as transaction types that can be used.

It was originally given at the R&D Day at GraphAware: https://graphaware.com

Michael Simons

December 17, 2020
Tweet

More Decks by Michael Simons

Other Decks in Programming

Transcript

  1. Neo4j and the JVM
    Michael Simons, 16th Dezember 2020
    The manifold ways of connecting to Neo4j from the JVM

    View full-size slide

  2. Michael Simons @rotnroll666
    About me
    „Michael Simons of Spring Data Neo4j/Reactive/Quarkus/GraalVM fame“
    • This ^^ is a quote (and the pressure is high)
    • Official role: Maintainer of Neo4j-OGM and Spring Data Neo4j
    • We released Neo4j-OGM 3.2 in 2019, with 20 patch releases following
    • Not many changes in Spring Data Neo4j 5.x
    • Changed most everything in Spring Data Neo4j 6

    View full-size slide

  3. Michael Simons @rotnroll666
    About me
    • (:Team {name: "OGM / SDN"}) - [:IS_PART_OF] -> (:Team {name: "Drivers"})
    • Very close feedback loop
    • Happy to test beta code
    • Nice position between ! users on the one hand, drivers and database on
    the other "

    View full-size slide

  4. Michael Simons @rotnroll666
    About me
    The rest…
    • #
    • $, % and & (no swimming this year… ')
    • Twitter addict… @rotnroll666
    • Enjoys databases with declarative languages in general
    • Active in both Java and database communities
    • Likes Spring (wrote a best selling German book about it)
    • But enjoys other ecosystems too…
    (Not actually evil, though…)

    View full-size slide

  5. Topics today
    Where did I use the Neo4j Java Driver?

    View full-size slide

  6. Neo4j-OGM
    The elephant in the room

    View full-size slide

  7. Michael Simons @rotnroll666
    Neo4j-OGM
    Multiple transport modes, why are they causing issues?
    • Embedded
    • Dependency management is a nightmare
    • Bringing up the database from the mapper is the wrong abstraction layer
    • Close coupling between OGM and Neo4j versions
    • Definitely not native ready
    • HTTP
    • Not typesafe (we are working on that)
    • Bolt (Aka the Neo4j Java Driver)
    • Mostly naming, people mistake bolt-driver with the Java driver itself
    • Transactional functions (to some extend, later more)

    View full-size slide

  8. Michael Simons @rotnroll666
    Neo4j-OGM
    Spring Data Neo4j 5.x without reactive support. (
    • No (sane) way to add a shared abstraction over all modes
    • Just wrapping lists in reactive datatypes isn’t gonna cut it

    View full-size slide

  9. On the plus side

    View full-size slide

  10. Michael Simons @rotnroll666
    Neo4j-OGM
    On the plus side
    • More than 20 releases in the last 2 years
    • Ticket numbers closed to about 4 or 5
    • Stable API, ways to mark queries explicitly as r/w and r/o, supports multi
    database
    • Native ready since this month
    • Usage of bolt taught us it’s API and usage
    • Super flexible mapping, both a benefit and sometimes a PITA
    • Personal note: I do like maintaining it.

    View full-size slide

  11. Two German Java developers just before Graph Tour 2019 in London
    „Hmm… Lightweight Spring Data JDBC is
    cool, should we try this with SDN?“ )

    View full-size slide

  12. Michael Simons @rotnroll666
    Goals of SDN⚡RX
    These days: Spring Data Neo4j 6
    • Full support of the reactive story



    • Neo4j native types
    • Dedicated ways of decomposing objects into maps to be stored on single nodes / relationships
    • Clean levels of abstracts (from low to high: Managed driver, Neo4j client, template, repositories, from
    client onwards with Spring TX Support)

    • Compatible with GraalVM native

    • Full support of immutable domain objects (Kotlin data classes, Java 16 records, Lombok Data classes)
    • Full support of all derived finder query building parts
    • Full support of find by example



    • Performance

    View full-size slide

  13. Michael Simons @rotnroll666
    (:Topic) - [:RELATED_TO {type: "directly"}] -> (the:Driver)
    Goals of SDN⚡RX
    • Full support of the reactive story
    • Neo4j native types
    • Dedicated ways of decomposing objects into maps to be stored on single nodes / relationships
    • Clean levels of abstracts (from low to high: Managed driver, Neo4j client, template, repositories, from
    client onwards with Spring TX Support)
    • Compatible with GraalVM native

    View full-size slide

  14. Michael Simons @rotnroll666
    Brought in order for today
    • Full support of the reactive story
    • Clean levels of abstracts (from low to high: Managed driver, Neo4j client,
    template, repositories, from client onwards with Spring TX Support)
    • Neo4j native types
    • Compatible with GraalVM native

    View full-size slide

  15. Michael Simons @rotnroll666
    Session and transaction APIs
    The big all-in-one-package
    • org.neo4j.driver.GraphDatabase is the general Driver factory
    • Instances of org.neo4j.driver.Driver are heavyweight objects and
    maintain a pool of physical connections
    • Open as few as possible and keep them around
    • All sessions are lightweight and basically a lease on connections
    • Don’t keep them around
    • Open and close as needed
    • Concepts are not 1:1 comparable to JDBC wording!

    View full-size slide

  16. Michael Simons @rotnroll666
    Session and transaction APIs
    What you want application wide is as few instances of a driver as possible.

    View full-size slide

  17. Michael Simons @rotnroll666
    Session and transaction APIs
    All major Java platforms do support something like this now

    View full-size slide

  18. Michael Simons @rotnroll666
    Session and transaction APIs
    Reduce driver footprint
    • i.e. in scenarios where there is more than instance needed
    (different credentials, different clusters etc.)
    • Scripting

    View full-size slide

  19. Michael Simons @rotnroll666
    Session and transaction APIs
    Reduce driver footprint
    • i.e. in scenarios where there is more than instance needed
    (different credentials, different clusters etc.)
    • Scripting

    View full-size slide

  20. Michael Simons @rotnroll666
    Driver instance hands out sessions
    Config options apply for all types

    View full-size slide

  21. Michael Simons @rotnroll666
    Session purpose
    Mostly transaction chaining
    • On the surface
    • Executes transactional work
    • Hands out unmanaged transactions
    • On the inside
    • Retries your work if necessary and possible
    • Chains causal bookmarks together

    View full-size slide

  22. Fan out: Transactions
    • Each session type provides
    • Auto-commit aka invisible
    transactions
    • Managed transactions
    • Unmanaged transactions
    • Following examples only
    showing imperative style
    3 x 3 ways of running queries

    View full-size slide

  23. Michael Simons @rotnroll666
    Auto-commit transactions
    The invisible kid.

    View full-size slide

  24. Michael Simons @rotnroll666
    Managed transactions
    The transactions formerly known as transaction functions.

    View full-size slide

  25. Michael Simons @rotnroll666
    Unmanaged transactions
    There might be dragons.

    View full-size slide

  26. Michael Simons @rotnroll666
    Unmanaged transactions
    There might be dragons.
    • You want to integrate with an
    external tx manager
    • You want control over
    exception handling and
    retries
    • Your transactional work is
    not idempotent

    View full-size slide

  27. Bookmarks
    • Don’t parse them
    • Don’t persist them
    • Sessions handles them for you
    • Relevant for working against a
    cluster
    • Use them when chaining
    sessions
    Which I initially though is hard but isn’t

    View full-size slide

  28. Michael Simons @rotnroll666
    Bookmarks
    Usage pattern

    View full-size slide

  29. Michael Simons @rotnroll666
    Bookmarks
    Possible bookmark manager (From SDN 6)

    View full-size slide

  30. The async session

    View full-size slide

  31. The async session
    Not today.

    View full-size slide

  32. Non blocking, back pressure up into the Bolt server.
    RxSession

    View full-size slide

  33. Michael Simons @rotnroll666
    RxSession: Reactive database access
    Key facts

    View full-size slide

  34. Michael Simons @rotnroll666
    RxSession: Reactive database access
    Key facts
    • Fully non blocking, required changes in the Bolt server
    • Internally using
    • Netty and existing async behavior
    • Small parts build with Project Reactor
    • Build based on PULL_N and TX_STREAMING (See 7687.org, Server v4)
    • Externally adhering to https://www.reactive-streams.org
    • Publisher compliant with the TCK (See https://github.com/reactive-streams/
    reactive-streams-jvm/tree/master/tck)

    View full-size slide

  35. RxSession drives SDN 6
    Well, the reactive parts.

    View full-size slide

  36. Michael Simons @rotnroll666
    RxSession: Observations
    Your mileage may vary.
    • Better resource usage
    • More efficient use of threads (non blocking IO is key!)
    • Slightly less memory usage on the client side
    • Steep learning curve
    • All reactive libraries offer a plethora of operators
    • What is happens on LIMIT / cancel?
    • Resource handling can be tricky
    • Reactive context works awesome for user / tenancy information

    View full-size slide

  37. Michael Simons @rotnroll666
    RxSession: Observations
    Your mileage may vary.
    • For resource usage and some unqualified benchmarking have a look at
    https://github.com/michael-simons/neo4j-from-the-jvm-ecosystem

    View full-size slide

  38. Michael Simons @rotnroll666
    RxSession: Use it with 3rd party libraries
    Don’t use the publishers alone.
    • Officially under test are
    • Project Reactor https://projectreactor.io (Flux and Mono)
    • RxJava 2 https://github.com/ReactiveX/RxJava (Flowable and Observable)
    • Works with
    • SmallRye Mutiny! https://smallrye.io/smallrye-mutiny/ (Multi)
    • Helidons reactive implementation
    • Most efficient usage: With Project Reactor (no conversion required)

    View full-size slide

  39. A couple of examples

    View full-size slide

  40. Reading things

    View full-size slide

  41. Michael Simons @rotnroll666
    Project Reactor
    Standard for Spring environments

    View full-size slide

  42. Michael Simons @rotnroll666
    Project Reactor
    Standard for Spring environments
    Important:
    This delays the creation of the session
    until something subscribes!

    View full-size slide

  43. Michael Simons @rotnroll666
    Project Reactor
    Standard for Spring environments
    Pass the new resource to the actual work

    View full-size slide

  44. Michael Simons @rotnroll666
    Project Reactor
    Standard for Spring environments
    Clean up the resource. Not needed for tx as this
    is done via the tx function below

    View full-size slide

  45. Michael Simons @rotnroll666
    Project Reactor
    Standard for Spring environments
    Create something usable from the publisher
    API. I usually do this as early as possible.

    View full-size slide

  46. Michael Simons @rotnroll666
    Project Reactor
    Standard for Spring environments
    Apply back pressure here, i.e.
    - take(n) (Can replace a LIMIT inside the query*)
    - skip(n) (Can replace a SKIP inside the query*)
    - takeUntil()

    - elementAt(i)

    - bufferUntil

    View full-size slide

  47. Michael Simons @rotnroll666
    RxJava2
    For example with Micronaut

    View full-size slide

  48. Michael Simons @rotnroll666
    RxJava2
    For example with Micronaut
    Yikes… No async clean up here…
    At least, session creation is correctly deferred

    View full-size slide

  49. Michael Simons @rotnroll666
    SmallRye Mutiny!
    Used in Quarkus

    View full-size slide

  50. Michael Simons @rotnroll666
    SmallRye Mutiny!
    Used in Quarkus
    Better. Async clean up, too.

    View full-size slide

  51. Writing things

    View full-size slide

  52. Michael Simons @rotnroll666
    Very much the same with tx functions

    View full-size slide

  53. Michael Simons @rotnroll666
    Very much the same with tx functions
    Better don’t do a take(n), skip here in case of multiple results.
    Also not the best idea to expose such a flow directly as an endpoint.
    take(n) is effectively a „cancel“ and so is an interrupted browser request.
    in such case a transaction would be rolled back!

    View full-size slide

  54. Michael Simons @rotnroll666
    Take control on cancel
    You will need unmanaged transactions.

    View full-size slide

  55. Michael Simons @rotnroll666
    Take control on cancel
    You will need unmanaged transactions.

    View full-size slide

  56. Michael Simons @rotnroll666
    Take control on cancel
    You will need unmanaged transactions.
    Create a nested resource: RxSession provides
    transactions, transactions provides supplier for
    committing or rolling them back.

    View full-size slide

  57. Michael Simons @rotnroll666
    Take control on cancel
    You will need unmanaged transactions.
    The usingWhen form has distinct hooks for async
    complete, error and cancel.
    Cancel can be „good“ (taken enough) or „bad“ (somethings gone…)

    View full-size slide

  58. Michael Simons @rotnroll666
    Just a couple of pitfalls
    • Inner project semantics, i.e. here Project Reactor, on a Flux
    • flatMap: Takes every item and maps it concurrently into a new Publisher
    • concatMap: Same as above, but one at a time
    • switchMap: Takes the next item, cancels the inner publisher and maps that
    item into a new Publisher
    • Different behaviors between libraries
    Flux/Mono are both Publishers (never null items, strict failures)
    Multi/Uni feel like Publishers, but only Multi is. Uni drops everything after 1st

    View full-size slide

  59. Michael Simons @rotnroll666
    Just a couple of pitfalls
    • Cancel: Good or bad? Intended or not accident?
    • Be careful with transaction boundaries

    View full-size slide

  60. SDN 6
    For a better living.
    Imperative and reactive repositories
    Imperative and reactive templates
    Imperative and reactive client
    Integrated with Spring Transactions and Context
    Driver

    View full-size slide

  61. Michael Simons @rotnroll666
    SDN 6 reactive examples

    View full-size slide

  62. Michael Simons @rotnroll666
    SDN 6 reactive examples

    View full-size slide

  63. https://github.com/neo4j-examples/sdn6-reactive-multidatabase
    More examples
    https://github.com/michael-simons/neo4j-from-the-jvm-ecosystem
    Spring Data Neo4j 6 centric, with focus on multi db and fabric
    JVM in general: Plain driver and various OGMs in various frameworks

    View full-size slide

  64. The holy Graal?
    • High-performance runtime
    • GraalVM compiler: JIT written in Java,
    integrated with Hotspot VM through
    JVM Compiler interface
    • Language independent representation
    of guest languages: A graph
    • The Truffle framework for creating new
    interpreters for additional languages
    • The polyglot runtime
    • Native Image
    Speaking about GraalVM

    View full-size slide

  65. Just an overview, too many things to cover.

    View full-size slide

  66. Michael Simons @rotnroll666
    GraalVM Native image
    Ahead-of-Time-Compilation
    • Create native executables or shared libraries
    • Restrictions apply
    • Classes / methods used via reflections must be enumerated in most cases
    • Same applies for resources
    • There’s an agent helping you todo this
    • Faster startup, lower memory overhead
    • Maybe less throughput than a fully warmed up JVM

    View full-size slide

  67. Michael Simons @rotnroll666
    GraalVM Native image
    Does it work with the Neo4j Java driver?
    YES!
    • Netty needed some „support“ (a couple of substitutions, mainly inspired by our work
    with the Quarkus people)
    • SSL is kinda hard to get right, requires JNI and others enabled on native image
    generation
    • Tested to work with
    • Quarkus
    • Spring Boot (with SDN 6, currently in Beta)
    • Helidon (from Oracle, currently in Alpha, PR-Stage)

    View full-size slide

  68. Michael Simons @rotnroll666
    GraalVM Native image
    Use cases
    • Workloads that needs to be super elastic
    • „Scale to 0“
    • CLI

    View full-size slide

  69. Michael Simons @rotnroll666
    GraalVM Polyglot programming
    Lots of fun.

    View full-size slide

  70. Thank you for your time.

    View full-size slide

  71. Michael Simons @rotnroll666
    Image sources
    • Ventilator by Ronan Furuta: https://unsplash.com/photos/7p8cFwM0Y1s
    • Bookmarks by Kasturi Roy: https://unsplash.com/photos/bYy9hndOx0k
    • Goblet by Shannon Douglas: https://unsplash.com/photos/8yeyzVfMv0M
    • Graal and Neo4j polyglot by Oleg Šelajev for my Medium article:
    https://medium.com/graalvm/3-ways-to-polyglot-with-graalvm-
    fb28c1542b45

    View full-size slide