Search code examples
jetty

Reduce number of threads with multiple Connectors in Jetty


We have a server configured with multiple connectors. We use the Jetty class ServerConnector to open multiple ports. Our problem is, that each connector has its selector and acceptor, too. Thats 3 reserved threads from our thread pool.

Is it possible to reduce the reserved threads by sharing the selector and / or acceptors? Maybe the connectors can share the same thread, too?


Solution

  • Short answer, no, you cannot share the selectors / acceptors across the ServerConnectors.

    But you can reduce the number of acceptors to 1 and the number of selectors to 1 on each ServerConnector.

    For tiny web servers this is completely normal setup.

    For busy servers, the selectors will likely need to increase, acceptors might need to be increase.

    Acceptors

    A correct Acceptors count is dependent on how many new connections per second your webapp is experiencing.
    1 is a good value for a surprisingly high rate (a saturated 1 Gbit network adaptor with tiny short lived connections can survive quite well on 1 acceptor, but faster networks, or higher rates of new connections will require a bigger value here. Don't hardcode it in your own code, allow it to be configured) On some very high traffic web servers we've seen, this value can be quite high.

    Jetty default acceptor count is calculated via (up to Jetty 12.0.6)...

    int cores = Runtime.getRuntime().availableProcessors();
    int acceptorCount = Math.max(1, Math.min(4, cores / 8));
    

    Starting in Jetty 12.0.7 the default on acceptors is being changed to 1. See: https://github.com/jetty/jetty.project/issues/11432

    Selectors

    A correct Selectors count is dependent on how many available processors your machine has.

    Jetty default selector count is calculated via:

    int cores = Runtime.getRuntime().availableProcessors();
    int selectorCount = Math.max(1, cores / 2);