title: Vert.x: simplest server with 1000 rps
body: <p>Suppose you need to write a server with 1000 rps. The load may grow in the future. Server is serving only one kind of request - <code>getGender(name)</code> which accept a name, and return <code>Male</code>/<code>Female</code>. Determinition of sex is simplest operation which requires single index lookup, where index is an in-memory data structure. </p>
  
  <p>If understand it right - you create single <code>ServerVerticle</code>, and run <code>Runtime.getRuntime().availableProcessors()</code> worker verticles to which delegate the job (see code below). </p>
  
  <p><strong><em>Questions:</em></strong></p>
  
  <ol>
  <li><em>Is this the best scheme for the task of 1000 rps?</em></li>
  <li><em>What will happen on request peak when there will be unsufficient 15 workers?</em> Suppose one worker can handle 100 rps. You have 15 workers. But at peak time you have 3000 rps. 
  
  <ul>
  <li>Suppose <code>NetServer</code> can handle 3000 rps, but workers stuck to handle them. <em>Have Vert.x got any queue to keep waiting requests? How to do that? If it has - what happens on worker fail?</em></li>
  <li>Suppose <code>NetServer</code> can't handle 3000 rps - just run few instances of a server. <em>No pitfalls, right?</em></li>
  </ul></li>
  <li><em>Is TCP the better choice for the task?</em></li>
  <li>Vert.x is multi-reactor, which like Node runs it event-loop.
  <em><code>ServerVerticle</code> is run in the same thread as event-loop, right?</em></li>
  <li><em>If you have 16 cores, 1 core is dedicated for event-loop, so Vert.x will run 15 GenderVerticles, right?</em> No more thereads?</li>
  </ol>
  
  <p><strong><em>ServerVerticle.java</em></strong></p>
  
  <pre><code>public class ServerVerticle extends AbstractVerticle {
  
      public static void main(String[] args) {
          Consumer&lt;Vertx&gt; runner = vertx -&gt; vertx.deployVerticle("ServerVerticle", new DeploymentOptions());
          Vertx vertx = Vertx.vertx();
          runner.accept(vertx);
      }
  
      @Override
      public void start() throws Exception {
          NetServerOptions options = new NetServerOptions();
          NetServer server = vertx.createNetServer(options);
          server.connectHandler(socket -&gt; {
              socket.handler(buffer -&gt; {
                  vertx.eventBus.send("get.gender", buffer, res -&gt; socket.write(res.toString()));
              });
          });
          server.listen(1234, "localhost");
  
          //Deploy worker verticles
          DeploymentOptions deploymentOptions = new DeploymentOptions()
              .setInstances(Runtime.getRuntime().availableProcessors())
              .setWorker(true);
         vertx.deployVerticle("GenderServiceVerticle", deploymentOptions);
      } 
  }
  </code></pre>
  
  <p><strong><em>GenderVerticle.java</em></strong></p>
  
  <pre><code>public class GenderVerticle extends AbstractVerticle {
  
      @Override
      public void start() throws Exception {
          vertx.eventBus().consumer("get.gender", message -&gt; {
              String gender = singleIndexLookup(message);
              message.reply(gender);
          });
      }
  
      singleIndexLookup() { ... }
  }
  </code></pre>