Efficient, asynchronous batching and caching in clustered environments, port of Facebook DataLoader
APACHE-2.0 License
DataLoader
Note: A pure java 8 (non-Vert.x) port of this project is now an official graphql-java project: java-dataloader
This small and simple utility library is a port of Facebook DataLoader to Java 8 for use with Vert.x. It can serve as integral part of your application's data layer to provide a consistent API over various back-ends and reduce message communication overhead through batching and caching.
An important use case for DataLoader
is improving the efficiency of GraphQL query execution, but there are
many other use cases where you can benefit from using this utility.
Most of the code is ported directly from Facebook's reference implementation, with one IMPORTANT adaptation to make it work for Java 8 and Vert.x. (more on this below).
But before reading on, be sure to take a short dive into the original documentation provided by Lee Byron (@leebyron) and Nicholas Schrock (@schrockn) from Facebook, the creators of the original data loader.
Vert.x DataLoader
is a feature-complete port of the Facebook reference implementation with one major difference. These features are:
Future<V>
of requested valueCompositeFuture
CompositeFuture
CompositeFuture
s results are ordered according to insertion order of load requestsCacheMap<K, V>
implementationsThe original data loader was written in Javascript for NodeJS. NodeJS is single-threaded in nature, but simulates asynchronous logic by invoking functions on separate threads in an event loop, as explained in this post on StackOverflow.
Vert.x on the other hand also uses an event loop (that you should not block!!), but comes
with actor-like Verticle
s and a
distributed EventBus
that make it inherently asynchronous, and non-blocking.
Now in NodeJS generates so-call 'ticks' in which queued functions are dispatched for execution, and Facebook DataLoader
uses
the nextTick()
function in NodeJS to automatically dequeue load requests and send them to the batch execution function for processing.
And here there is an IMPORTANT DIFFERENCE compared to how this data loader operates!!
In NodeJS the batch preparation will not affect the asynchronous processing behaviour in any way. It will just prepare batches in 'spare time' as it were.
This is different in Vert.x as you will actually delay the execution of your load requests, until the moment where you make a call
to dataLoader.dispatch()
in comparison to when you would just handle futures directly.
Does this make Java DataLoader
any less useful than the reference implementation? I would argue this is not the case,
and there are also gains to this different mode of operation:
However, with batch execution control comes responsibility! If you forget to make the call to dispatch()
then the futures
in the load request queue will never be batched, and thus will never complete! So be careful when crafting your loader designs.
Gradle users configure the vertx-dataloader
dependency in build.gradle
:
repositories {
maven {
jcenter()
}
}
dependencies {
compile 'io.engagingspaces:vertx-dataloader:1.0.0'
}
To build from source use the Gradle wrapper:
./gradlew clean build
Or when using Maven add the following repository to your pom.xml
:
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
And add the dependency to vertx-dataloader
:
<dependency>
<groupId>io.engagingspaces</groupId>
<artifactId>vertx-dataloader</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>
Please take a look at the example project vertx-graphql-example created by Bruno Santos.
1.0.0
Initial releaseCompletableFuture
implementationAll your feedback and help to improve this project is very welcome. Please create issues for your bugs, ideas and enhancement requests, or better yet, contribute directly by creating a PR.
When reporting an issue, please add a detailed instruction, and if possible a code snippet or test that can be used as a reproducer of your problem.
When creating a pull request, please adhere to the Vert.x coding style where possible, and create tests with your code so it keeps providing an excellent test coverage level. PR's without tests may not be accepted unless they only deal with minor changes.
This library is entirely inspired by the great works of Lee Byron and Nicholas Schrock from Facebook whom I like to thank, and especially @leebyron for taking the time and effort to provide 100% coverage on the codebase. A set of tests which I also ported.
This project vertx-dataloader is licensed under the Apache Commons v2.0 license.
Copyright © 2016 Arnold Schrijver and other contributors