vert.xvertx-verticlevertx-httpclientvert.x-webclient

Vertx WebClient shared vs single across multiple verticles?


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


Solution

  • 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.