Managing Connections
This section describes how to connect the Go SDK to a Couchbase cluster. It contains best practices as well as information on TLS/SSL and advanced connection options, and a sub-page on troubleshooting Cloud connections.
Our Getting Started pages cover the basics of making a connection to a Capella or self-managed Couchbase cluster. This page is a wider look at the topic.
Connecting to a Cluster
A connection to a Couchbase Operational cluster is represented by a Cluster object.
A Cluster provides access to Buckets, Scopes, and Collections, as well as various Couchbase services and management interfaces.
The simplest way to create a Cluster object is to call gocb.Connect() with a connection string, username, and password:
-
Couchbase Capella
-
Self-Managed Couchbase Server
-
Cloud Native Gateway (CNG)
options := gocb.ClusterOptions{
Authenticator: gocb.PasswordAuthenticator{
Username: username,
Password: password,
},
}
// Sets a pre-configured profile called "wan-development" to help avoid latency issues
// when accessing Capella from a different Wide Area Network
// or Availability Zone (e.g. your laptop).
if err := options.ApplyProfile(gocb.ClusterConfigProfileWanDevelopment); err != nil {
log.Fatal(err)
}
// Initialize the Connection
cluster, err := gocb.Connect("couchbases://"+connectionString, options)
if err != nil {
log.Fatal(err)
}
Note, the client certificate for connecting to a Capella operational cluster is included in the SDK installation.
opts := gocb.ClusterOptions{
Username: "Administrator",
Password: "password",
}
cluster, err := gocb.Connect("10.112.193.101", opts)
if err != nil {
panic(err)
}
Couchbase’s large number of ports across the URLs of many services can be proxied by using a couchbase2:// endpoint as the connection string — currently only compatible with recent versions of Couchbase Autonomous Operator:
cluster, err := gocb.Connect("couchbase2://"+connectionString, gocb.ClusterOptions{
Authenticator: gocb.PasswordAuthenticator{
Username: username,
Password: password,
}
Read more on the Connections page.
In a production environment, your connection string should include the addresses of multiple server nodes in case some are currently unavailable. Multiple addresses may be specified in a connection string by delimiting them with commas:
opts := gocb.ClusterOptions{
Username: "Administrator",
Password: "password",
}
cluster, err := gocb.Connect("192.168.56.101,192.168.56.102", opts)
if err != nil {
panic(err)
}
| You don’t need to include the address of every node in the cluster. The client fetches the full address list from the first node it is able to contact. |
Waiting for Bootstrap Completion
Opening resources is asynchronous.
That is, the call to cluster.Bucket or gocb.Connect will complete instantly, and opening that resource will continue in the background.
You can force waiting for the resource to be opened with a call to WaitUntilReady, which is available on both the Cluster and Bucket.
Here is an example of using it on the bucket:
opts := gocb.ClusterOptions{
Username: "Administrator",
Password: "password",
}
cluster, err := gocb.Connect("couchbase://10.112.193.101", opts)
if err != nil {
panic(err)
}
bucket := cluster.Bucket("default")
err = bucket.WaitUntilReady(5*time.Second, nil)
if err != nil {
panic(err)
}
If not present, then the first Key Value (KV) operation on the bucket will wait for it to be ready. Any issues opening that bucket (for instance, if it does not exist), will result in an error being raised from that data operation.
Other timeout issues may occur when using the SDK located geographically separately from the Couchbase Server cluster — this is not recommended in production deployments, but often occurs during development. See the Cloud section below for some suggestions of settings adjustments.
Connection Strings
A Couchbase connection string is a comma-delimited list of IP addresses and/or hostnames, optionally followed by a list of parameters.
The parameter list is just like the query component of a URI; name-value pairs have an equals sign (=) separating the name and value, with an ampersand (&) between each pair.
Just as in a URI, the first parameter is prefixed by a question mark (?).
127.0.0.1
nodeA.example.com,nodeB.example.com
127.0.0.1?io.networkResolution=external&timeout.kvTimeout=10s
The full list of recognized parameters is documented in the client settings reference.
Connection Lifecycle
All high-level objects in the Go SDK are designed to be safe for concurrent use by multiple goroutines.
You will get the best performance by using only a single Cluster object per cluster,
and as few Bucket, Scope, and Collection as is reasonable for your application.
We recommend creating a single Cluster instance when your application starts up, and sharing this instance throughout your application.
If you know at startup time which buckets, scopes, and collections your application will use,
we recommend obtaining them from the Cluster at startup time and sharing those instances throughout your application as well.
Before your application stops, gracefully shut down the client by calling the Close() method of each Cluster you created.
Secure Connections
Both Couchbase Capella, and the Enterprise Edition of self-managed Couchbase Server, support full encryption of client-side traffic using Transport Layer Security (TLS). That includes data (key-value type) operations, queries, and configuration communication. Make sure you have the Enterprise Edition of Couchbase Server, or a Couchbase Capella account, before proceeding with configuring encryption on the client side.
-
Couchbase Capella
-
Self-Managed Couchbase Server
The Go SDK bundles Capella’s standard root certificate by default.
This means you don’t need any additional configuration to enable TLS — simply use couchbases:// in your connection string.
| Capella’s root certificate is not signed by a well known CA (Certificate Authority). However, as the certificate is bundled with the SDK, it is trusted by default. |
You connect to a Couchbase Server cluster with a root certificate issued by a trusted CA (Certificate Authority), you no longer need to configure this in the securityConfig settings.
The cluster’s root certificate just needs to be issued by a CA whose certificate is in the JVM’s trust store. This includes well known CAs (e.g., GoDaddy, Verisign, etc…), plus any other CA certificates that you wish to add.
You can still provide a certificate explicitly if necessary:
-
Get the CA certificate from the cluster and save it in a text file.
-
Enable encryption on the client side and point it to the file containing the certificate.
It is important to make sure you are transferring the certificate in an encrypted manner from the server to the client side, so either copy it through SSH or through a similar secure mechanism.
If you are running on localhost and just want to enable TLS for a development machine, just copying and pasting it suffices — so long as you use 127.0.0.1 rather than localhost in the connection string.
This is because the certificate will not match the name localhost.
Navigate in the admin UI to and copy the input box of the TLS certificate into a file on your machine (which we will refer to as cluster.cert).
It looks similar to this:
-----BEGIN CERTIFICATE----- MIICmDCCAYKgAwIBAgIIE4FSjsc3nyIwCwYJKoZIhvcNAQEFMAwxCjAIBgNVBAMT ASowHhcNMTMwMTAxMDAwMDAwWhcNNDkxMjMxMjM1OTU5WjAMMQowCAYDVQQDEwEq MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzz2I3Gi1XcOCNRVYwY5R ................................................................ mgDnQI8nw2arBRoseLpF6WNw22CawxHVOlMceQaGOW9gqKNBN948EvJJ55Dhl7qG BQp8sR0J6BsSc86jItQtK9eQWRg62+/XsgVCmDjrB5owHPz+vZPYhsMWixVhLjPJ mkzeUUj/kschgQ0BWT+N+pyKAFFafjwFYtD0e5NwFUUBfsOyQtYV9xu3fw+T2N8S itfGtmmlEfaplVGzGPaG0Eyr53g5g2BgQbi5l5Tt2awqhd22WOVbCalABd9t2IoI F4+FjEqAEIr1mQepDaNM0gEfVcgd2SzGhC3yhYFBAH//8W4DUot5ciEhoBs= -----END CERTIFICATE-----
The next step is to enable encryption and pass it the path to the certificate file.
// We use the system certificate pool here and assume the Couchbase root certificate(s) have
// been installed there, but it is also possible to load these from a file.
rootCAs, err := x509.SystemCertPool()
if err != nil {
panic(err)
}
opts := gocb.ClusterOptions{
Username: "Administrator",
Password: "password",
SecurityConfig: gocb.SecurityConfig{
TLSRootCAs: rootCAs,
},
}
cluster, err := gocb.Connect("couchbases://10.112.193.101", opts)
if err != nil {
panic(err)
}
Then use this custom ClusterEnvironment when opening the connection to the cluster.
See [cluster-environment] for an example of creating a Cluster with a custom environment.
If you want to verify it’s actually working, you can use a tool like tcpdump.
For example, an unencrypted upsert request looks like this (using sudo tcpdump -i lo0 -A -s 0 port 11210):
E..e..@.@.............+......q{...#..Y.....
.E...Ey........9........................id{"key":"value"}
After enabling encryption, you cannot inspect the traffic in cleartext (same upsert request, but watched on port 11207 which is the default encrypted port):
E.....@.@.............+....Z.'yZ..#........ ..... ...xuG.O=.#.........?.Q)8..D...S.W.4.-#....@7...^.Gk.4.t..C+......6..)}......N..m..o.3...d.,. ...W.....U.. .%v.....4....m*...A.2I.1.&.*,6+..#..#.5
The SDK includes the certificate for Capella. If you are working with a self-managed Couchbase cluster, see Certificate Authentication.
Alternate Addresses and Custom Ports
If your Couchbase Server cluster is running in a containerized, port mapped, or otherwise NAT’d environment like Docker or Kubernetes, a client running outside that environment may need additional information in order to connect the cluster. Both the client and server require special configuration in this case.
On the server side, each server node must be configured to advertise its external address as well as any custom port mapping.
This is done with the setting-alternate-address CLI command.
A node configured in this way will advertise two addresses: one for connecting from the same network, and another for connecting from an external network.
On the client side, the externally visible ports must be used when connecting. If the external ports are not the default, you can specify custom ports as part of your connection string.
opts := gocb.ClusterOptions{
Username: "Administrator",
Password: "password",
}
cluster, err := gocb.Connect("couchbase://192.168.56.101:1234,192.168.56.102:5678", opts)
if err != nil {
panic(err)
}
| In a deployment that uses multi-dimensional scaling, a custom KV port is only applicable for nodes running the KV service. A custom manager port may be specified regardless of which services are running on the node. |
In many cases the client is able to automatically select the correct set of addresses to use when connecting to a cluster that advertises multiple addresses.
If the detection heuristic fails in your environment, you can override it by setting the io.networkResolution client setting to default if the client and server are on the same network, or external if they’re on different networks.
| Any TLS certificates must be set up at the point where the connections are being made. |
Using DNS SRV records
As an alternative to specifying multiple hosts in your program, you can get the actual bootstrap node list from a DNS SRV record. For Capella, where you only have one endpoint provided, it’s good practice to always enable DNS-SRV on the client.
The following steps are necessary to make it work:
-
Set up your DNS server to respond properly from a DNS SRV request.
-
Enable it on the SDK and point it towards the DNS SRV entry.
Setting up the DNS Server
Capella gives you DNS-SRV by default — these instructions are for self-managed clusters, where you are responsible for your own DNS records.
Your DNS server zone file should be set up like this, with one row for each bootstrap (KV, a.k.a. Data Service) node:
; Service.Protocol.Domain TTL Class Type Priority Weight Port Target _couchbases._tcp.example.com. 3600 IN SRV 0 0 11207 node1.example.com. _couchbases._tcp.example.com. 3600 IN SRV 0 0 11207 node2.example.com. _couchbases._tcp.example.com. 3600 IN SRV 0 0 11207 node3.example.com.
The first line comment is not needed in the record, we are showing the column headers here for illustration purposes.
The myriad complexities of DNS are beyond the scope of this document, but note that SRV records must point to an A record, not a CNAME.
The order in which you list the nodes — and any value entered for Priority or Weight — will be ignored by the SDK.
Nevertheless, best practice here is to set them to 0, avoiding ambiguity.
Also note, the above is for connections using TLS. Should you be using an insecure connection (in testing or development, or totally within a firewalled environment), then your records would look like:
_couchbase._tcp.example.com. 3600 IN SRV 0 0 11210 node1.example.com. _couchbase._tcp.example.com. 3600 IN SRV 0 0 11210 node2.example.com. _couchbase._tcp.example.com. 3600 IN SRV 0 0 11210 node3.example.com.
Specifying DNS-SRV for the SDK
-
The connection string must be to a single hostname, with no explicit port specifier, pointing to the DNS SRV entry —
couchbases://example.com. -
DNS-SRV must be enabled in the client settings.
DNS SRV bootstrapping is enabled by default in the Go SDK.
In order to make the SDK use the SRV records, you need to pass in the hostname from your records (here example.com):
opts := gocb.ClusterOptions{
Username: "Administrator",
Password: "password",
}
cluster, err := gocb.Connect("couchbase://mysrvrecord.hostname.com", opts)
if err != nil {
panic(err)
}
If the DNS SRV records could not be loaded properly you’ll get the exception logged and the given host name will be used as a A record lookup.
WARNING: DNS SRV lookup failed, proceeding with normal bootstrap. javax.naming.NameNotFoundException: DNS name not found [response code 3]; remaining name '_couchbase._tcp.example.com' at com.sun.jndi.dns.DnsClient.checkResponseCode(DnsClient.java:651) at com.sun.jndi.dns.DnsClient.isMatchResponse(DnsClient.java:569)
Also, if you pass in more than one node, DNS SRV bootstrap will not be initiated:
INFO: DNS SRV enabled, but less or more than one seed node given. Proceeding with normal bootstrap.
Cloud Native Gateway
Couchbase’s next generation connection protocol, introduced in Go SDK 2.7 and Couchbase Autonomous Operator 2.6.1, is enabled by changing the connection string to couchbase2:// — but there are a few differences to be aware of, described below.
The protocol implements a gRPC-style interface between the SDK and Couchbase Server (in this case, only available in the Server running on Kubernetes or OpenShift, with a recent version of Couchbase Autonomous Operator).
Limitations
The underlying protocol will not work with certain legacy features: MapReduce Views (a deprecated Service — use Query instead) and Memcached buckets (superseded by the improved Ephemeral Buckets).
The following are not currently implemented over the couchbase2:// protocol:
-
Authentication by client certificate.
-
Multi-document ACID transactions.
-
Analytics service.
-
Health Check.
There are some different behaviors seen with this protocol:
-
Some config options are unsupported — see the Settings page.
-
The SDK will poll the gRPC channels until they are in a good state, or return an error, or timeout while waiting — in our standard protocol there is an option of setting
waitUntilReady()for just certain services to become available. -
Some error codes are more generic — in cases where the client would not be expected to need to take specific action — but should cause no problem, unless you have written code looking at individual strings within the error messages.
-
Although documents continue to be stored compressed by Couchbase Server, they will not be transmitted in compressed form (to and from the client) over the wire, using
couchbase2://.
Working in the Cloud
For most use cases, connecting client software using a Couchbase SDK to the Couchbase Capella service is similar to connecting to an on-premises Couchbase Cluster. The use of DNS-SRV, Alternate Address, and TLS is covered above.
We strongly recommend that the client and server are in the same LAN-like environment (e.g. AWS Region). As this may not always be possible during development, read the guidance on working with constrained network environments. More details on connecting your client code to Couchbase Capella can be found in the Cloud docs.
Troubleshooting Connections to Cloud
Some DNS caching providers (notably, home routers) can’t handle an SRV record that’s large — if you have DNS-SRV issues with such a set-up, reduce your DNS-SRV to only include three records. [For development only, not production.]. Our Troubleshooting Cloud Connections page will help you to diagnose this and other problems — as well as introducing the SDK doctor tool.