1

I am using vert.x as an api gateway to route calls to downstream services.

As of now, I am using single web client instance which is shared across multiple verticles (injected through guice)

Does it make sense for each verticle to have it's own webclient? Will it help in boosting performance? (My each gateway instance runs 64 vericles and handles approximately 1000 requests per second)

What are the pros and cons of each approach?

Can someone help to figure out what's the ideal strategy for the same?

Thanks

Nitish Goyal
  • 97
  • 10

1 Answers1

4

Vert.x is optimized for using a single WebClient per-Verticle. Sharing a single WebClient instance between threads might work, but it can negatively affect performance, and could lead to some code running on the "wrong" event-loop thread, as described by Julien Viet, the lead developer of Vert.x:

So if you share a web client between verticles, then your verticle might reuse a connection previously open (because of pooling) and you will get callbacks on the event loop you won't expect. In addition there is synchronization in the web client that might become contented when used intensively from different threads.

Additionally, the Vert.x documentation for HttpClient, which is the underlying object used by WebClient, explicitly states not to share it between Vert.x Contexts (each Verticle gets its own Context):

The HttpClient can be used in a Verticle or embedded.

When used in a Verticle, the Verticle should use its own client instance.

More generally a client should not be shared between different Vert.x contexts as it can lead to unexpected behavior.

For example a keep-alive connection will call the client handlers on the context of the request that opened the connection, subsequent requests will use the same context.

dano
  • 91,354
  • 19
  • 222
  • 219
  • 1
    The vertx documentation says something else.. https://vertx.io/docs/vertx-web-client/java/#_creating_a_web_client. – Nitish Goyal Aug 12 '20 at 02:09
  • @NitishGoyal The documentation says to only create the `WebClient` at application startup, which is true. Creating new ones for each request is expensive and should be avoided. It just doesn't say anything about sharing them between threads. But, I included the recommendation from the lead Vert.x developer on that topic in my answer. – dano Aug 12 '20 at 13:39
  • @NitishGoyal Also see here, in the `HttpClient` documentation, it is explicit about not sharing between contexts: https://vertx.io/docs/vertx-core/java/#_httpclient_usage – dano Aug 12 '20 at 13:41
  • So when a new WebClient should be created when not using Verticles? Should not be created for each request and should not be shared between threads? – Panu Haaramo Jan 13 '22 at 08:34