Native connection between PHP and Couchbase

The repcached software works, but I’ve found that the quirks and overall risks far outweigh it’s long term reliability. A couple of months ago, I started a side project where I attempted to connect my company’s software to a new caching solution called Couchbase. We rely way too much on caching so if a node were to go down, our middleware goes tits up without much of a fight.

Initial connections to the database software were successful, but we were no better off then just using the same memcached daemon. Couchbase supports being a “drop in replacement” to memcached, but that alone gives you no real good features since you’re just using the same php-pecl-memcache/php-pecl-memcached module to interface with it.

With more research, I found that Couchbase offers binaries for native client connections to their software, operating in their preferred “couchbase mode”. This consists of securing your connection to the server over a series of “buckets” that can be clustered together in a high availability fashion. These buckets can be open or closed with a user name and password restriction, but there’s more! The client module is capable of detecting all the servers in the cluster so you can fail over gracefully to a different node in the event of a cache server failure. The only downside is that there is an enforced minimum wait time of 30 seconds before the cluster fails over. To workaround that, I set my clients to timeout appx 40 seconds in the event of a network interruption. This gives the middleware enough wait time so it doesn’t get crushed by frontend load.

You can find the php libraries (as well as others) and installation instructions for the native Courchbase client here:

The software I have to support uses the Zend framwork’s caching backend. To this date, I could not find a pre-made caching plugin that was specifically written for PHP, so I wrote my own! I based my code off the Libmemcached.php backend file.

You can download my version of the backend for Couchbase here:

If you intend to interface the software through the zend framework, please make sure you follow the Couchbase instructions on installing their PHP module before using my library! Unpack the gz archive and move the Couchbase.php file into your zend cache backend folder. From here all you need to do is setup some .ini parameters for your main application: = Couchbase = your_couchbase_server_address
resources.cacheManager.default.backend.options.servers.server1.timeout = 40 (Change this depending on your needs)
resources.cacheManager.default.backend.options.servers.server1.port = 8091 (default)
resources.cacheManager.default.backend.options.servers.server1.user = your_couchbase_bucket_user_name
resources.cacheManager.default.backend.options.servers.server1.password = your_couchbase_bucket_password
resources.cacheManager.default.backend.options.servers.server1.bucket = your_couchbase_bucket_name (default is called default)
resources.cacheManager.default.backend.options.servers.server1.persistent = 1 (Change this depending on your needs)

The prefixs that I used in the above example might be vastly different from what you have setup with your application so make sure you adjust it accordingly. If you don’t know what’s going on here, then please consult the developer of your application because they will more than likely know the correct syntax.

(Optional) I also set my PHP sessions to cache to the server with the following parameter:

resources.session.save_path = "http://your_couchbase_server_address:8091/pools/default" (Change this based on the information you establish in the parameters mentioned earlier)

Assuming you did this all correctly, you should able to interface your old memcached application directly with the Couchbase server over it’s native PHP api over the zend framework while utilizing all their new features!

I offer consulting for people who are in need of these kinds of solutions but lack the resources to implement them. If you are in need, I am always willing to help people with these sort of problems in my spare time. Feel free to leave any comments or suggestions in this post. I will revise this information whenever I feel that there is a need to do so.

Posted in News and tagged , , , , , , , , , , , , , , , , , , , , , , .