Monday, 19 November 2012

Couchbase smart clients

While doing some research on replacing Memcached with Couchbase for the company I work for, I came across the term "Couchbase smart client". The problem is I couldn't find a decent explanation(in my opinion) for what exactly it does. As you will find out next, the smart client is maybe the most important aspect when it comes to connecting to a Couchbase cluster.

Let's have a look first at what the Couchbase documentation says about smart clients:

When using a smart client, the client library provides an interface to the cluster, and performs server selection directly via the vBucket mechanism. The clients communicate with the cluster using a custom Couchbase protocol which enables the sharing of the vBucket map, and the selection within the client of the required vBucket when obtaining and storing information

I couldn't figure out how that is affecting me as a client of the cluster, so I decided to have a look at the low levels calls that a simple client is doing in order to connect to the cluster and write a key. Below is the source code(PHP):

The above is a massive simplification of what is happening, but it's enough to give us a clue of what the client is doing:

at line 1 the client is connecting to the first server in the pool on port 8091

at line 2 the client is requesting information about the servers in the cluster

at line 3 the client starts receiving that information

at line 4 the client is initiating a connection to the server in the pool that is responsible for the key that we want to write, port 11210

To make this short and sweet, the client is connecting to one of the servers in the pool and by receiving information about the cluster, it is able to decide which of the servers is handling the keys it needs. So what makes the client "smart" is the fact that it has knowledge about the cluster and it makes decisions based on the current state of the cluster.

You can start seeing the advantages of such a smart client when you think about not so smart clients like memcached, where you would have to implement all the logic about the state of the cluster on the client side, which is no trivial task by most people's standards.

1 comment:

About me

I'm a pragmatic software developer with more than 14 years of commercial experience accumulated in full time employment, contracting and as an entrepreneur. Worked in a variety of industries such as investment banking, security, aviation, online gaming, education and publishing. Founder of an investment banking back office solution implemented in 10 investment banks. A keen microservices advocate.