ScyllaDB University Live | Free Virtual Training Event
Learn more
ScyllaDB Documentation Logo Documentation
  • Deployments
    • Cloud
    • Server
  • Tools
    • ScyllaDB Manager
    • ScyllaDB Monitoring Stack
    • ScyllaDB Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
    • Supported Driver Versions
  • Resources
    • ScyllaDB University
    • Community Forum
    • Tutorials
Install
Search Ask AI
ScyllaDB Docs ScyllaDB gocql driver Connecting to the cluster
For AI agents: a documentation index is available at https://gocql-driver.docs.scylladb.com/master/llms.txt. A Markdown version of this page is at https://gocql-driver.docs.scylladb.com/master/connecting/index.md.

Connecting to the cluster¶

Create one Session during application startup and share it across goroutines. A session discovers cluster topology, maintains connection pools, and routes concurrent requests. Creating a session per request wastes connections and discovery work.

Supply several reachable contact points when possible:

cluster := gocql.NewCluster(
	"192.0.2.10:9042",
	"192.0.2.11:9042",
	"192.0.2.12:9042",
)
cluster.Keyspace = "application"

session, err := cluster.CreateSession()
if err != nil {
	return err
}
defer session.Close()

Contact points bootstrap discovery; they are not the complete list of hosts used by the session. Prefer node broadcast addresses or IP addresses. DNS names that resolve to multiple addresses can make topology events difficult to match to discovered hosts.

Shard-aware port¶

ScyllaDB’s shard-aware native transport port lets the driver establish the correct per-shard connections with fewer attempts. The built-in host dialer uses it automatically when advertised by the server.

If a custom net.Dialer is required, wrap it so the driver-selected source port is honored:

dialer := net.Dialer{
	Timeout: 5 * time.Second,
}
cluster.Dialer = &gocql.ScyllaShardAwareDialer{Dialer: dialer}

Custom gocql.Dialer implementations can read the requested source port with gocql.ScyllaGetSourcePort(ctx). If the port is already bound, return gocql.ErrScyllaSourcePortAlreadyInUse or syscall.EADDRINUSE so the driver can try another suitable port.

A custom ClusterConfig.HostDialer replaces the built-in host-level dialing and fallback logic. It must implement gocql.ShardDialer to receive shard-targeted calls, and its DialShard method is responsible for any fallback after a shard-targeted dial fails.

Shard-aware ports require nodes configured with native_shard_aware_transport_port for plaintext connections or native_shard_aware_transport_port_ssl for TLS, a network path that preserves source ports, and custom gocql.Dialer implementations that bind the selected source port. The driver falls back to the regular native transport port when using its built-in host dialer. Set ClusterConfig.DisableShardAwarePort to true only when the advertised port is unreachable and the network cannot be fixed.

Reconnection policies¶

Reconnection policies restore failed host connections; they do not retry queries. Configure steady-state and initial connection behavior separately:

cluster.ReconnectionPolicy = &gocql.ExponentialReconnectionPolicy{
	MaxRetries:      10,
	InitialInterval: time.Second,
	MaxInterval:     30 * time.Second,
}
cluster.InitialReconnectionPolicy = &gocql.ConstantReconnectionPolicy{
	MaxRetries: 5,
	Interval:   2 * time.Second,
}

NoReconnectionPolicy performs only the initial connection attempt. A custom policy must return a positive maximum attempt count.

Was this page helpful?

PREVIOUS
Quick start
NEXT
Compression
  • Create an issue
  • Edit this page

On this page

  • Connecting to the cluster
    • Shard-aware port
    • Reconnection policies
ScyllaDB gocql driver
Search Ask AI
  • master
    • master
  • Quick start
  • Connecting to the cluster
    • Compression
    • Authentication
    • TLS
    • Client routes (PrivateLink / Private Service Connect)
  • Executing CQL statements
    • Paging
    • Batch statements
    • Lightweight transactions
    • Request timeouts
  • Data types
  • Load balancing
  • Retry policy configuration
  • Speculative execution
Docs Tutorials University Contact Us About Us
© 2026 ScyllaDB | Terms of Service | Privacy Policy | ScyllaDB, and ScyllaDB Cloud, are registered trademarks of ScyllaDB, Inc.
Last updated on 14 Sep 2026.
Powered by Sphinx 9.1.0 & ScyllaDB Theme 1.9.3