0

I have an endpoint which connects to postgres db and returns the data back to the client. And the endpoint is looks like below

public Multi<String> getData(RequestBody body) { // application code }

Q) How to implement circuit breaker for Mutiny , where return type is Multi

Anything to help better understand will be of real help.

Jaiprasad
  • 149
  • 11
  • If the method/endpoint returns data from a PostgreSQL database, I'm pretty sure it won't be a stream. It's rather a list of results, possibly obtained asynchronously. `Uni>` is a more appropriate return type, and that supports fault tolerance in a straightforward way. (Out of interest, how would you imagine a circuit breaker works for a stream of data?) – Ladicek Jul 20 '22 at 17:56
  • the dao layer return type is Multi , want to understand how it provides fault tolerance straightforward , any references will be of help. Circuit breaker is basically to limit the number of failures , say incase of a db connection failure , it is better to stop sending the requests if there are say 5 back to back failures and then stop accepting the request for a small period of say 5 to 10 seconds and then start accepting the requests. – Jaiprasad Jul 20 '22 at 19:58
  • 1
    Okay, let me rephrase. The fault tolerance features of Quarkus, provided by SmallRye Fault Tolerance, are defined in a request/response manner. That's why `CompletionStage` or `Uni` are supported (they represent a single value), but `Publisher` or `Multi` are not (they represent a stream of values). Defining semantics for streams might be doable, but would likely be tricky. That said, databases like PostgreSQL do not stream the results, so your DAO shouldn't pretend they do. Instead of `Multi`, your DAO should return `Uni>`. Then, you can just add `@CircuitBreaker`. – Ladicek Jul 21 '22 at 08:53
  • I am using a quarkus-reactive-pg-client extension which executes the query and returns a Multi> , there is an option to convert this to Uni> , and the reason we should be returning Uni is because fault tolerance does not support Multi , is that right ? – Jaiprasad Jul 21 '22 at 09:27
  • 1
    The Vert.x reactive database drivers, in their Mutiny variant, actually never return a `Multi` -- they always return a `Uni` of `List`, `RowSet` or so. (There are examples that show converting to `Multi`, which is IMHO questionable.) – Ladicek Jul 21 '22 at 10:00
  • https://quarkus.io/guides/reactive-sql-clients#query-results-traversal Taking from the above reference , Multi fruits = rowSet .onItem().transformToMulti(set -> Multi.createFrom().iterable(set)) .onItem().transform(Fruit::from); in this block of code rowSet is Uni which is being converted to Multi and then transformed to Multi why is this so and finally the endpoint returns a Multi , does converting Uni to Multi offer fast processing ? – Jaiprasad Jul 21 '22 at 10:44
  • Also updating the question to limit the discussion around fault tolerance , mutiny and circuit breaker. Will post separate questions for others. – Jaiprasad Jul 21 '22 at 10:46
  • 1
    I have no idea why the guide converts the `Uni` to a `Multi`, but it's inefficient (because of the cost of the back-pressure protocol, which is wasted, as everything is already in memory). I can only speak with confidence about SmallRye Fault Tolerance -- and that doesn't support guarding methods that return `Multi` (or a `Publisher`), for the reason stated above. – Ladicek Jul 21 '22 at 14:20
  • Thank you for these input @Ladicek , as you said have changed the return type to Uni and now able to work with Circuit Breaker and Bulkhead . I will still have to evaluate the performance on changing the processing of db rows from Multi to Uni. – Jaiprasad Jul 21 '22 at 14:55
  • Ladicek , this question is on cassandra , cassandra-quarkus-client this extension of quarkus to connect with cassandra , all of its apis return Multi and not Uni. Is the extension using the vertx-cassandra-client underneath ? Followed this guide https://quarkus.io/guides/cassandra#reactive , am I using the right library here . I am using QuarkusCqlSession to execute my prepared stmt and returning Multi>. – Jaiprasad Jul 21 '22 at 15:21
  • does quarkus support smallrye-mutiny-vertx-cassandra-client to interact with cassandra ? – Jaiprasad Jul 21 '22 at 15:31
  • Sorry, I don't know anything about the Cassandra extension. – Ladicek Jul 22 '22 at 06:42

1 Answers1

0

Smallrye Fault Tolerance does not support Multi.

Here is the reference: https://smallrye.io/docs/smallrye-fault-tolerance/5.5.0/usage/programmatic-api.html#_mutiny_support

You need to work with Uni or CompletionStage

Once I switched to Uni from Multi , able to implement Circuit Breaker and Bulkhead.

Note: If you are working with reactive pg client , process Uni<Rowset> rather than Multi.

Thanks to Ladicek for all the inputs and leads.

Jaiprasad
  • 149
  • 11