Handling common errors

Handling common errors

This section describes resources and approaches to handling client- and server- side errors.

Client-side timeouts

Timeouts that occur during your application run time can be configured on the Couchbase Client SDK side. In most cases, you should also assume that your client application will need to receive Couchbase timeout information and handle the timeout appropriately.

This section is not intended to be a comprehensive guide on timeouts and performance tuning for your application; it provides some of the most useful, basic information about SDK-level timeout settings. It also describes considerations and trade-offs you should have in mind when you design your application to address timeouts.

You can handle timeouts from Couchbase client SDKs in the following ways:

  • Trigger another action, such as displaying default content or providing user with alternative action.

  • Slow down the number of requests you are making from your application.

  • Indicate system overload.

In many cases, a developer’s first instinct is to set very low timeouts, such as 100ms or less, in the hope that this will guarantee requests/responses happen in a certain time. However one possible consequence of setting very low timeouts is even a minor network delay of a few milliseconds can trigger a timeout. Timeouts usually mean that a Couchbase SDK will retry an operation which puts more unneeded traffic on the networks and other systems used in your application. In other scenarios developers may have inherited an application they want to use with Couchbase Server, but the application has not been originally designed to handle timeout information returned by Couchbase Server.

In either of these scenarios, the risk is to assume that setting aggressive timeouts at the SDK-level will make a transaction occur in a certain time. Doing so may erroneously lead to even more performance bottlenecks for your application.

Timeouts are actually intended as a way to trigger your application to make a decision should a response not happen by a certain time. In other words, it is still your application’s responsibility, and your responsibility as a developer to appropriately handle types of requests that will take longer, or not return at all. Time requirements for your application are best handled in your own application logic.

Setting timeouts so that an operation can fail fast can frequently be the right approach if your application is appropriately designed to do something in the case of a timeout, or slow response. For instance, if you want to set data and it does not happen quickly, it may not be significant for your application’s usability. You may want your application to provide some default result to display, or do nothing. In other cases, your application may have to display a general “all systems busy” to reflect the heavy concurrent demand.

One common error to avoid is the case where you destroy a client instance and recreate a new client object every time a timeout occurs. This is especially so when you set an aggressively low timeout. Creating a new client is a heavier, slower process; if you developed an application that responded to timeouts this way, you would significantly impact performance.

The other approach to consider in your application logic is to actually provide a ‘relief valve’ by slowing your application down during certain operations. For instance, in the context of bulk loading, you want to load as much information as quickly as possible, but slow down the response of your application for the user when the load is not progressing.

Be aware too that with web applications, there may be a timeout that occurs elsewhere in the system which is generated outside of the control of Couchbase Server and Couchbase Client SDKs. For instance, most web application servers will not let a request continue indefinitely.

The default timeout for any given node in a Couchbase cluster is 2.5 seconds. If a Couchbase SDK does not receive a response from the server by this time, it will drop the connection to Couchbase Server and attempt to connect to another node.

Applications built on Couchbase can have timeouts configured at the following levels:

  • Connection-level settings

  • Authentication-level settings

  • Request-level settings

The Couchbase PHP SDK does not have any connection, authentication, or request timeouts which are configurable, however you can build timeouts which are managed by your own application.

The following are timeouts that can be set for a connection to Couchbase Server from the Java SDK:

Parameter Description Default Setting
MaxReconnectDelay Maximum number of milliseconds to wait between reconnect attempts. 30 seconds
ShouldOptimize If multiple requests simultaneously occur for same record, the record will be retrieved only once from the database and then returned to each request. This eliminates the need for multiple retrievals. true
OpQueueMaxBlockTime The maximum amount of time, in milliseconds a client will wait to add a new item to a queue. 10 milliseconds
OpTimeout Operation timeout used by this connection. 2.5 seconds
TimeoutExceptionThreshold Maximum number of timeouts allowed before this connection will shut down. 998 timeouts

If you performing asynchronous transactions, you should be aware that performing these operations allocates memory at a low level. This can create a lot of information that needs to be garbage-collected by a VM, which will also slow down your application performance.

The following are some of the frequently-used timeouts that you set for a connection to Couchbase Server from the .Net SDK. for a full list of timeouts, their defaults, and their descriptions for .Net, see the Couchbase .Net SDK Developer Guide .

Parameter Description Default Setting
retryTimeout The amount of time to wait in between failed attempts to read cluster configguration. 2 seconds
observeTimeout Time to wait attempting to connect to pool when all nodes are down. In milliseconds. 1 minute
httpRequestTimeout The amount of time to wait for the HTTP streaming connection to receive cluster configuration 1 minute
connectionTimeout The amount of time the client waits to establish a connection to the server, or get a free connection from the pool 10 seconds

The following are timeouts that typically can be set for a connection to Couchbase Server from the Ruby SDK:

Parameter Description Default Setting
\:timeout Maximum number of microseconds to wait for a connection or a read/write to occur. 2500000 microseconds.

In the case of the Ruby SDK, you can set a timeout for the initial connection, and then change the timeout setting. This new connection-level setting will apply to any subsequent read/write requests made with the client instance:

conn = Couchbase.connect(:timeout => 3_000_000) conn.timeout = 1_500_000 conn.set("foo", "bar") 

In this example, we create a new Couchbase client instance with Couchbase.connect() and set the connection time out to 3 seconds. If the client instance fails to connect in the three seconds, it will timeout and return a failure to connect error. Then we set the timeout to 1.5, which will be the timeout level of any requests made with that client instance, such as the set() .

The following is the standard, default, non-configurable timeout for the Couchbase C and PHP SDKs. This timeout applies to creating a connection to the server and all read- and write- operations:

Description Default Setting
Default, maximum number of microseconds to wait for a connection or a read/write to occur. 2500000 microseconds.