title: Concept of vert.x concerning a webserver?
body: <p>I don't quite get how vert.x is applied for a webserver.</p>
  
  <p>The concept I know for webserver is the thread-based one.</p>
  
  <ol>
  <li>You start your webserver, which then is running.</li>
  <li>Then for every client that connects, you get a socket, which is then passed to its own thread handler.</li>
  <li>The thread handler then processes the tasks for this specific socket.</li>
  </ol>
  
  <p>So it is clearly defined which thread is doing the work for which socket.
  However for every socket you need a new thread, which is expensive in the long run for many sockets.</p>
  
  <p>Then there is the event-based concept that vert.x supplies. 
  So far I have understood, it should work anyhow like this:</p>
  
  <ol>
  <li>The Vertx instance deploys Verticles.</li>
  <li>Verticles run in background threads, but not every Verticle has its own thread. As an example there could be 1000 verticles deployed in a Vertx instance, but the Vertx instance handles only 8 threads (nr of cores * 2).</li>
  <li>Then there are the event loops. I'm not sure how they refer to verticles. I've read that every verticle has 2 event loops, but don't really know how that works.</li>
  </ol>
  
  <p>As a webserver example:</p>
  
  <pre><code>class WebServer: AbstractVerticle() {
      lateinit var server: HttpServer
  
      override fun start() {
          server = vertx.createHttpServer(HttpServerOptions().setPort(1234).setHost("localhost"))
          var router = Router.router(vertx);
          router.route("/test").handler { routingContext -&gt;
              var response = routingContext.response();
              response.end("Hello from my first HttpServer")
          }
          server.requestHandler(router).listen()
      }
  }
  </code></pre>
  
  <p>This WebServer can be deployed multiple times in a Vertx instance. And as it seems, each WebServer instance gets its own thread. When I try to connect 100 Clients and reply with a simple response, it seems like each Client is handled synchronously. Because when I do a Thread.sleep statement in each server handler, then the every second one client gets a response. However it should be that all server handlers should start their 1 second sleep and then almost identically reply to all clients after this time.</p>
  
  <p>This is the code to start 100 clients:</p>
  
  <pre><code>fun main(){
      Vertx.vertx().deployVerticle(object : AbstractVerticle(){
          override fun start() {
              for(i in 0 .. 100)
                  MyWebClient(vertx)
          }
      })
  }
  
  class MyWebClient(val vertx: Vertx) {
      init {
          println("Client starting ...")
          val webClient = WebClient.create(vertx, WebClientOptions().setDefaultPort(1234).setDefaultHost("localhost"))
          webClient.get("/test").send { ar -&gt;
              if(ar.succeeded()){
                  val response: HttpResponse&lt;Buffer&gt; = ar.result()
  
                  println("Received response with status code ${response.statusCode()} + ${response.body()}")
              } else {
                  println("Something went wrong " + ar.cause().message)
              }
          }
      }
  }
  </code></pre>
  
  <p>Does anybody know an explanation for this?</p>