title: Slow connection to Cassandra
body: <p>I have trouble connecting to my 3-node cassandra cluster via Datastax PHP- and Java-Driver.</p>
  
  <p>Especially for the PHP driver it is crucial that i can connect fast to improve loading times of my website.</p>
  
  <p>How can i debug this or what is the reason?</p>
  
  <p>Java output shows this:</p>
  
  <pre><code>09:59:40,284 [main] DEBUG - com.datastax.driver.NEW_NODE_DELAY_SECONDS is undefined, using default value 1
  09:59:40,284 [main] DEBUG - com.datastax.driver.NON_BLOCKING_EXECUTOR_SIZE is undefined, using default value 4
  09:59:40,297 [main] DEBUG - com.datastax.driver.NOTIF_LOCK_TIMEOUT_SECONDS is undefined, using default value 60
  09:59:40,357 [main] DEBUG - Starting new cluster with contact points [/XXX.XXX.XXX.XXX:9042, /XXX.XXX.XXX.YYY:9042, /XXX.XXX.XXX.ZZZ:9042]
  09:59:40,402 [main] DEBUG - Using SLF4J as the default logging framework
  09:59:40,489 [main] DEBUG - java.nio.Buffer.address: available
  09:59:40,490 [main] DEBUG - sun.misc.Unsafe.theUnsafe: available
  09:59:40,490 [main] DEBUG - sun.misc.Unsafe.copyMemory: available
  09:59:40,490 [main] DEBUG - java.nio.Bits.unaligned: true
  09:59:40,492 [main] DEBUG - Java version: 8
  09:59:40,492 [main] DEBUG - -Dio.netty.noUnsafe: false
  09:59:40,492 [main] DEBUG - sun.misc.Unsafe: available
  09:59:40,492 [main] DEBUG - -Dio.netty.noJavassist: false
  09:59:40,665 [main] DEBUG - Javassist: available
  09:59:40,665 [main] DEBUG - -Dio.netty.tmpdir: /var/folders/4y/t4b47lbn1zjbjpb6x09l30wm0000gn/T (java.io.tmpdir)
  09:59:40,666 [main] DEBUG - -Dio.netty.bitMode: 64 (sun.arch.data.model)
  09:59:40,666 [main] DEBUG - -Dio.netty.noPreferDirect: false
  09:59:40,708 [main] DEBUG - com.datastax.driver.FORCE_NIO is undefined, using default value false
  09:59:40,710 [main] INFO  - Did not find Netty's native epoll transport in the classpath, defaulting to NIO.
  09:59:40,714 [main] DEBUG - -Dio.netty.eventLoopThreads: 8
  09:59:40,723 [main] DEBUG - -Dio.netty.noKeySetOptimization: false
  09:59:40,723 [main] DEBUG - -Dio.netty.selectorAutoRebuildThreshold: 512
  09:59:40,747 [main] DEBUG - -Dio.netty.leakDetectionLevel: simple
  09:59:41,035 [main] DEBUG - com.datastax.driver.DISABLE_COALESCING is undefined, using default value false
  09:59:41,046 [main] DEBUG - Generated: io.netty.util.internal.__matchers__.com.datastax.driver.core.Message$ResponseMatcher
  09:59:41,066 [main] DEBUG - -Dio.netty.allocator.numHeapArenas: 4
  09:59:41,066 [main] DEBUG - -Dio.netty.allocator.numDirectArenas: 4
  09:59:41,066 [main] DEBUG - -Dio.netty.allocator.pageSize: 8192
  09:59:41,066 [main] DEBUG - -Dio.netty.allocator.maxOrder: 11
  09:59:41,067 [main] DEBUG - -Dio.netty.allocator.chunkSize: 16777216
  09:59:41,067 [main] DEBUG - -Dio.netty.allocator.tinyCacheSize: 512
  09:59:41,067 [main] DEBUG - -Dio.netty.allocator.smallCacheSize: 256
  09:59:41,067 [main] DEBUG - -Dio.netty.allocator.normalCacheSize: 64
  09:59:41,067 [main] DEBUG - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
  09:59:41,067 [main] DEBUG - -Dio.netty.allocator.cacheTrimInterval: 8192
  09:59:41,078 [main] DEBUG - Generated: io.netty.util.internal.__matchers__.com.datastax.driver.core.FrameMatcher
  09:59:41,082 [main] DEBUG - Generated: io.netty.util.internal.__matchers__.com.datastax.driver.core.Message$RequestMatcher
  09:59:41,104 [main] DEBUG - -Dio.netty.initialSeedUniquifier: 0x24d6f22f78c5a924 (took 8 ms)
  09:59:41,130 [main] DEBUG - -Dio.netty.allocator.type: unpooled
  09:59:41,130 [main] DEBUG - -Dio.netty.threadLocalDirectBufferSize: 65536
  09:59:41,197 [cluster1-nio-worker-0] DEBUG - Connection[/XXX.XXX.XXX.YYY:9042-1, inFlight=0, closed=false] Connection opened successfully
  09:59:41,218 [cluster1-nio-worker-0] DEBUG - -Dio.netty.recycler.maxCapacity.default: 262144
  09:59:41,432 [main] DEBUG - [Control connection] Refreshing node list and token map
  09:59:41,518 [main] DEBUG - [Control connection] Refreshing schema
  09:59:42,137 [main] DEBUG - [Control connection] Refreshing node list and token map
  09:59:42,315 [main] DEBUG - [Control connection] Successfully connected to /XXX.XXX.XXX.YYY:9042
  09:59:42,315 [main] INFO  - Using data-center name '168' for DCAwareRoundRobinPolicy (if this is incorrect, please provide the correct datacenter name with DCAwareRoundRobinPolicy constructor)
  09:59:42,315 [main] INFO  - New Cassandra host /XXX.XXX.XXX.XXX:9042 added
  09:59:42,315 [main] INFO  - New Cassandra host /XXX.XXX.XXX.YYY:9042 added
  09:59:42,315 [main] INFO  - New Cassandra host /XXX.XXX.XXX.ZZZ:9042 added
  09:59:42,342 [cluster1-nio-worker-1] DEBUG - Connection[/XXX.XXX.XXX.XXX:9042-2, inFlight=0, closed=false] Connection opened successfully
  09:59:42,345 [cluster1-nio-worker-2] DEBUG - Connection[/XXX.XXX.XXX.YYY:9042-1, inFlight=0, closed=false] Connection opened successfully
  09:59:42,348 [cluster1-nio-worker-3] DEBUG - Connection[/XXX.XXX.XXX.ZZZ:9042-1, inFlight=0, closed=false] Connection opened successfully
  09:59:42,580 [cluster1-nio-worker-2] DEBUG - Added connection pool for /XXX.XXX.XXX.XXX:9042
  09:59:42,591 [cluster1-nio-worker-3] DEBUG - Added connection pool for /XXX.XXX.XXX.YYY:9042
  09:59:42,609 [cluster1-nio-worker-1] DEBUG - Added connection pool for /XXX.XXX.XXX.ZZZ:9042
  </code></pre>
  
  <p>As you can see, it takes ~2.5 seconds which is too slow for my use case.</p>
  
  <p>Same happens with the PHP driver, but i don't have a log for this.</p>
  
  <p>Queries are very fast once the driver is connected. Only issue is the slow connection time. I have set up all the three nodes as contact points.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Just to clarify: My PHP driver is the problem. I'm wondering why it isn't using pooling/persisting connections. When i call the script two times in a row, every call takes 2-5 seconds. I think the second call should be using the persisting pool. phpinfo() shows persistent clusters &amp; sessions = 0. This is the code that i'm using:</p>
  
  <pre><code>$cluster = Cassandra::cluster()
          -&gt;withContactPoints('XXX.XXX.XXX.XXX', 'XXX.XXX.XXX.YYY, 'XXX.XXX.XXX.ZZZ')
          -&gt;withCredentials('USERNAME', 'PASSWORD')
          -&gt;build();
  $keyspace = 'myKeyspace';
  $session = $cluster-&gt;connect($keyspace);
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The problem was my network. Had too little bandwidth.</p>