Wrapping CompletableFuture to Mono with DynamoDb

936 Views Asked by At

I know the java DynamoDB's SDK is considered async non blocking but my question is:

Q. Does the dynamo update still need to be on another thread with operator publishOn to be safer in a reactive context?

Mono.fromFuture(table.update()).publishOn();

1

There are 1 best solutions below

2
On

Assuming that you are using the AWS Java SDK v2, you don't need to worry about delegating to a thread pool. AWS SDK already makes sure that you don't block an important thread. See documentation for details:

The AWS SDK for Java 2.x uses Netty, an asynchronous event-driven network application framework, to handle I/O threads. The AWS SDK for Java 2.x creates an ExecutorService behind Netty, to complete the futures returned from the HTTP client request through to the Netty client. This abstraction reduces the risk of an application breaking the async process if developers choose to stop or sleep threads. By default, 50 Threads are generated for each asynchronous client, and managed in a queue within the ExecutorService.

Actually, it even use more threads than it would be desired in a truly reactive application. Fortunately, the SDK also gives option to opt out of that behavior if needed.

One more thing that's good to be aware of is CompletableFuture is eager unlike reactive type Mono. For this reason it is better to use the lambda version of the fromFuture method:

Mono.fromFuture(() -> table.update())