Couchbase SDK observe-functions indicate whether a document is on disk or on a replica node. Documents in Couchbase Server can be in RAM only, can be persisted to disk, or can also be on a replica node as a copy. When data is persisted onto disk or is on a replica node, when the node that contains that data fails, you can still recover the data.
Once the node fails, the document can be recovered from disk back into RAM and then retrieved by your application. If the document is available on a replica node that is still functioning, you can request the document and it will be retrieved from the replica node. You use observe-functions to determine whether important application data has been persisted or replicated so that you have some assurance you can recreate the document or not if a Couchbase node is down.
There are two approaches for providing 'observe'/monitoring functionality in Couchbase SDKs:
Provide ability to monitor the state of a document and determine if it is persisted or on replica node.
Provide ability to explicitly persist or replicate documents to a certain number of disks or replica nodes.
The first example we demonstrate in the Ruby SDK takes the first
approach where you can monitor a given key. The Couchbase Ruby SDK
will return a Result object with the status of
a given key:
stats = conn.observe("foo")
In this case, we perform the observe with
a Couchbase cluster containing one replica node. The results we
receive will be as follows:
<Couchbase::Result:0x0000000182d588 error=0x0 key="foo" status=:persisted cas=4640963567427715072 from_master=true time_to_persist=0 time_to_replicate=0>
This Results object provides the status for the
key foo: the symbol
:persisted tells us that it has been persisted
to disk, and the from_master=true result
indicates that the document has been replicated. The Couchbase
Ruby SDK also supports the second approach where we can specify
our preferences for replica and persistence when we store a
document:
conn.set("foo", "bar", :observe => {:persisted => 2, :timeout => 5})
For store and update operations, we can provide a parameter to
specify that a document be persisted or replicated a certain
number of times. In this example above we indicate that the key
foo be persisted onto disk on two nodes. The
:timeout is specific to this operation and
indicates the operation should timeout after 5 seconds of waiting
for the two document writes onto disk.
One common approach for using an observe-function is to verify that a document is on at least one replica node. If you want to be extremely certain about the durability of some documents, you may want to verify that the document is replicated to at lease three nodes and persisted to at least four servers. This represents the maximum number of replicas and on-disk copies that Couchbase Server currently supports.
For asynchronous observe requests, a Couchbase SDK determines that an observe request is complete by polling the Couchbase Server. A Couchbase SDK will determine which observe requests have completed all the events that are being observed for a key, namely replication and persistence.
The types of errors that can occur during this operation include 1) inability to connect to a node, or 2) some error exists while attempting to format a value being used. If you have a connection-level error you may need to reattempt connection, and possibly check the status of the server. If you have an error with the size of your value or formatting, you need to check the value itself, and how it is encoded and see if there are any issues that make the document incompatible with Couchbase Server.
For more information about connections and connection-level settings, see Section 7.5.4, “Optimizing Client Instances” and Section 7.7.1, “Client-Side Timeouts”