Skip to content

Cassandra\Cluster\Builder ​

Obtained from Cassandra::cluster(). Every method returns the builder, so calls chain. build() produces an immutable Cassandra\Cluster.

php
$cluster = Cassandra::cluster()
    ->withContactPoints('10.0.0.1')
    ->build();

Every method ​

MethodParametersDefaultDescription
withContactPointsstring ...$host127.0.0.1Addresses the driver dials first
withPortint $port9042The CQL native port on every node
withCredentialsstring $username, string $passwordnonePassword authentication. The password is a SensitiveParameter
withSSLCassandra\SSLOptions $optionsnoneTLS settings from Cassandra::ssl()
withDefaultConsistencyint $consistencyCONSISTENCY_LOCAL_QUORUMConsistency for statements that set none
withDefaultPageSizeint $pageSize5000Rows per page for statements that set none
withDefaultTimeoutfloat $timeoutnoneSeconds. Timeout for statements that set none
withConnectTimeoutfloat $timeout5.0Seconds. Bounds one connection handshake
withRequestTimeoutfloat $timeout12.0Seconds. The hard ceiling on one request
withRoundRobinLoadBalancingPolicy—activeCycle through every node in the cluster
withDatacenterAwareRoundRobinLoadBalancingPolicystring $localDatacenter, int $hostPerRemoteDatacenter, bool $useRemoteDatacenterForLocalConsistencies—Prefer the local datacenter. All three arguments are required
withRackAwareLoadBalancingPolicystring $localDatacenter = '', string $localRack = ''—ScyllaDB only. Prefer the local rack, then the local datacenter. Empty strings let the driver infer both from the first contact point
withTokenAwareRoutingbool $enabled = truetrueSend a request straight to a replica that owns the data
withLatencyAwareRoutingbool $enabled = truetruePush consistently slow nodes down the candidate list
withWhiteListHostsstring ...$hostsnoneUse only these nodes
withBlackListHostsstring ...$hostsnoneNever use these nodes
withWhiteListDCsstring ...$dcsnoneUse only these datacenters
withBlackListDCsstring ...$dcsnoneNever use these datacenters
withConnectionsPerHostint $core, int $max = 21, 2Pool size per node. Each value must be 1 to 128
withIOThreadsint $count1Event loop threads in the C driver. 1 to 128
withReconnectIntervalfloat $interval2.0Seconds between attempts to reach a node that is down
withConnectionHeartbeatIntervalfloat $interval30.0Seconds between keepalive requests on an idle connection
withExponentialReconnectfloat $baseInterval, float $maxInterval—Back off from base to max, in seconds, with jitter. Replaces the constant delay
withApplicationNamestring $name—Reported as APPLICATION_NAME in system.clients.client_options
withApplicationVersionstring $version—Reported as APPLICATION_VERSION in the same map
withConstantSpeculativeExecutionPolicyfloat $delay, int $maxSpeculativeExecutions = 2offRe-send a slow request to another replica after $delay seconds. Idempotent statements only
withNoSpeculativeExecutionPolicy—defaultTurn speculative execution off
withCoalesceDelayint $microseconds200How long the driver batches writes into one system call
withNewRequestRatioint $ratio50Split IO thread time between new and outstanding requests, 1 to 100
withExecutionProfilestring|\UnitEnum $name, ExecutionProfile $profile—Register a named profile. Select it with the third argument of execute(). See execution profiles
withTCPNodelaybool $enabled = truetrueDisable Nagle's algorithm
withTCPKeepalive?float $delaydisabledSeconds. Pass null to disable
withProtocolVersionint $version4CQL protocol version
withPersistentSessionsbool $enabled = truetrueCache the session in the PHP worker process
withSchemaMetadatabool $enabled = truetrueFetch and track the schema. Required for token awareness
withHostnameResolutionbool $enabled = truefalseResolve peer addresses to host names
withRandomizedContactPointsbool $enabled = truetrueShuffle the contact point order
withRetryPolicyCassandra\RetryPolicy $policyDefaultPolicySee retry policies
withTimestampGeneratorCassandra\TimestampGenerator $generatorserver sideClient-side write timestamps
build——Produce the Cassandra\Cluster

All time values are seconds, given as a float. 0.25 is 250 milliseconds.

Grouped by purpose ​

Where to connect ​

php
->withContactPoints('10.0.0.1', '10.0.0.2', '10.0.0.3')
->withPort(9042)
->withRandomizedContactPoints(true)
->withHostnameResolution(false)

See clusters and sessions.

Security ​

php
->withCredentials('app_user', getenv('SCYLLA_PASSWORD'))
->withSSL($sslOptions)

See authentication and TLS and SSL.

Which node gets the request ​

php
->withDatacenterAwareRoundRobinLoadBalancingPolicy('eu-west-1', 0, false)
->withTokenAwareRouting(true)
->withLatencyAwareRouting(true)
->withWhiteListDCs('eu-west-1')

See load balancing and routing.

Pool and timeouts ​

php
->withConnectionsPerHost(2, 8)
->withIOThreads(1)
->withConnectTimeout(5.0)
->withRequestTimeout(12.0)
->withReconnectInterval(2.0)
->withConnectionHeartbeatInterval(30.0)   // 30 seconds
->withTCPNodelay(true)
->withTCPKeepalive(null)

See connection pool and timeouts.

Query defaults ​

php
->withDefaultConsistency(Cassandra::CONSISTENCY_LOCAL_QUORUM)
->withDefaultPageSize(5000)
->withDefaultTimeout(10.0)
->withRetryPolicy(new Cassandra\RetryPolicy\DefaultPolicy())
->withTimestampGenerator(new Cassandra\TimestampGenerator\Monotonic())

Every one of these is overridable per statement. See queries and statements.

Validation ​

Invalid values raise Cassandra\Exception\InvalidArgumentException at the call, not at build().

MethodRule
withDefaultPageSize0 or greater
withIOThreads1 to 128
withConnectionsPerHostEach value 1 to 128
withProtocolVersion1 or greater
withDatacenterAwareRoundRobinLoadBalancingPolicyhostPerRemoteDatacenter 0 or greater
Every timeout0 or greater

Reading the configuration back ​

The builder exposes its state as read-only properties, which is convenient in tests.

php
$builder = Cassandra::cluster()
    ->withContactPoints('10.0.0.1')
    ->withTokenAwareRouting(true);

get_object_vars($builder);
// ['contactPoints' => '10.0.0.1', 'useTokenAwareRouting' => true, ...]

The exposed names are: contactPoints, loadBalancingPolicy, localDatacenter, hostPerRemoteDatacenter, useRemoteDatacenterForLocalConsistencies, useTokenAwareRouting, username, password, connectTimeout, requestTimeout, sslOptions, defaultConsistency, defaultPageSize, defaultTimeout, usePersistentSessions, protocolVersion, ioThreads, coreConnectionPerHost, maxConnectionsPerHost, reconnectInterval, latencyAwareRouting, tcpNodelay, tcpKeepalive, retryPolicy, timestampGenerator, schemaMetadata, blacklist_hosts, whitelist_hosts, blacklist_dcs, whitelist_dcs, hostnameResolution, randomizedContactPoints, and connectionHeartbeatInterval. The port is not among them.

The password is redacted, unless an operator turns that off

password returns ***. var_dump(), print_r(), and framework debug panels therefore print the placeholder, not the credential.

The cassandra.expose_credentials INI setting puts the real value back:

ini
cassandra.expose_credentials = On

The setting is PHP_INI_SYSTEM, so ini_set() inside a request cannot turn the redaction off. Only an operator can, through php.ini or php -d. Use it in development only.

Persistent session cache ​

With withPersistentSessions(true), build() hashes the whole configuration and reuses a cached cluster when the hash matches. Two builders with identical settings share one underlying cluster inside a PHP worker process.

This is why the configuration must come from constants, not from per-request values. A configuration that varies per request fills the cache. See performance.

Version 1.5. Released under the Apache License 2.0.