Skip to content

Cluster and Session

Cassandra\Cluster

An interface. Cassandra\Cluster\Builder::build() returns the Cassandra\DefaultCluster implementation. A cluster holds configuration only. It opens no sockets.

php
interface Cluster
{
    public function connect(?string $keyspace = null, ?int $timeout = null): Session;
    public function connectAsync(?string $keyspace = null): Future;
}

connect()

php
$session = $cluster->connect();            // no keyspace
$session = $cluster->connect('shop');      // set the keyspace
$session = $cluster->connect('shop', 10);  // 10 second timeout

Opens the connection pool and returns a Cassandra\Session. Blocks until the pool is ready.

$timeout is in seconds, as an integer.

Pass null rather than an empty string when there is no keyspace. An empty string produces an invalid USE statement.

Throws:

ExceptionCause
Cassandra\Exception\RuntimeExceptionNo contact point could be reached
Cassandra\Exception\AuthenticationExceptionThe credentials were rejected
Cassandra\Exception\InvalidQueryExceptionThe keyspace does not exist

connectAsync()

php
$future  = $cluster->connectAsync('shop');
$session = $future->get(10.0);

Returns a Cassandra\FutureSession. See asynchronous queries.

Cassandra\Session

An interface. connect() returns the Cassandra\DefaultSession implementation.

php
interface Session
{
    public function execute(string|Statement $statement, array|ExecutionOptions|null $options = null, string|\UnitEnum|null $executionProfile = null): Rows;
    public function executeAsync(string|Statement $statement, array|ExecutionOptions|null $options = null, string|\UnitEnum|null $executionProfile = null): FutureRows;
    public function prepare(string $cql, array|ExecutionOptions|null $options = null): PreparedStatement;
    public function prepareAsync(string $cql, array|ExecutionOptions|null $options = null): FuturePreparedStatement;
    public function close(int|float|null $timeout = null): void;
    public function closeAsync(): FutureClose;
    public function metrics(): array;
    public function schema(): Schema;
}

execute()

php
$rows = $session->execute($statement, $options, $executionProfile);

Runs one statement and returns one page of results. $statement is a CQL string, a Cassandra\SimpleStatement, a Cassandra\PreparedStatement, or a Cassandra\BatchStatement.

$options is an array. Every key is optional:

KeyTypeDefault
argumentsarraynone
consistencyintThe cluster default
serial_consistencyintnone
page_sizeintThe cluster default, 5000
paging_state_tokenstringnone
timeoutint or floatThe cluster default
retry_policyCassandra\RetryPolicyThe cluster policy
timestampint or numeric stringServer side

$executionProfile selects a profile registered with withExecutionProfile(). It takes a string or an enum case, and null uses the cluster settings. See execution profiles.

php
$rows = $session->execute($statement, null, 'analytics');
$rows = $session->execute($statement, null, Profile::Analytics);

See queries and statements.

executeAsync()

php
$future = $session->executeAsync($statement, $options, $executionProfile);
$rows   = $future->get();

Returns a Cassandra\FutureRows at once. Same options as execute().

prepare()

php
$statement = $session->prepare('SELECT * FROM users WHERE id = ?');

Sends the CQL to the cluster and returns a Cassandra\PreparedStatement. One network round trip. Prepare once, keep the object.

prepareAsync()

php
$future    = $session->prepareAsync('SELECT * FROM users WHERE id = ?');
$statement = $future->get();

Returns a Cassandra\FuturePreparedStatement.

close()

php
$session->close();
$session->close(5.0);   // wait at most 5 seconds

Closes the connection pool. $timeout is in seconds.

The session closes at script shutdown as well. Call close() when you want the sockets released at a known point.

With persistent sessions on, the underlying cluster stays cached in the worker process, so a later connect() with the same configuration is cheap.

closeAsync()

php
$future = $session->closeAsync();
$future->get();

Returns a Cassandra\FutureClose.

metrics()

php
$m = $session->metrics();

$m['requests']['median'];              // microseconds
$m['requests']['p99'];
$m['requests']['m1_rate'];             // requests per second
$m['stats']['total_connections'];
$m['stats']['available_connections'];
$m['errors']['request_timeouts'];

Reads process memory. No network call. The full key list is in metrics and logging.

schema()

php
$keyspace = $session->schema()->keyspace('shop');
$table    = $keyspace->table('users');

Returns the live schema metadata. See schema metadata.

Lifetime

ObjectCost to createLifetime
Cluster\BuilderFreeLocal to the configuration function
ClusterFreeFree to keep, holds no sockets
SessionExpensive: handshake, auth, schema fetchOne per process, or per worker
PreparedStatementOne round tripFor as long as the session lives

See performance.

Version 1.4. Released under the Apache License 2.0.