DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Content provided by Couchbase logo

Multiple Bucket Connections in Couchbase with Ruby

Don Pinto user avatar by
Don Pinto
·
Aug. 09, 13 · Interview
Like (0)
Save
Tweet
Share
3.43K Views
This post comes from Robin Johnson at the Couchbase blog.

c = Couchbase.new(“http://localhost:8091/pools/default/buckets/my-bucket”)

I believe it’s safe to say we are mostly all familiar with the above Ruby code.  It establishes a connection from the Ruby SDK to a bucket of our choosing inside our Couchbase cluster.  There are, of course, multiple ways of writing this line, such as specifying the URI and Port as Ruby Symbols (:pool => “default”,  :bucket => “my-bucket”), etc.  The point I am making here is that we write these simple one-line commands and get one simple connection to our chosen bucket.  But what if our application uses multiple buckets?  In this article I’m going to back to basics and take you through setting up and connecting to multiple buckets.

Why would I use multiple buckets in Couchbase?

Using multiple buckets in Couchbase is an interesting concept. (To me, anyway.)

On the one hand, there are benefits to using multiple buckets, but it’s important to remember that, because of Couchbase’s Flexible Schema, we could store JSON docs of any structure in the same bucket, even if every document’s schema was different.

Many Couchbase consumers are now, however, seeing benefits in using multiple buckets in their applications.  For instance, if you are a hosting provider and you want to have different users using the same database server, buckets can be password-protected and would prevent one user from accessing another user’s data.

Some people create multiple buckets for organizational purposes. For example, you could be running two different applications and want the data to be separate, or maybe you just want to split data up by category.  These are all very reasonable use-cases and, by all means, very possible.  So now that we know why we might use multiple buckets, let’s take a look at how we can connect to and utilize those buckets.
 

Connecting to Multiple Buckets in Couchbase

Let’s dive in.  Open up your Admin Console.  Click into the “Buckets” tab, and let’s create 2 new Buckets:  bucket1 and bucket2.

So now that we’ve got our data buckets let’s write some Ruby to create our client connections to them!

First, make sure you’ve got Libcouchbase installed.  Then, make sure you’ve got the latest Couchbase Ruby Gem installed.  Then go ahead and create a Ruby file called cb_connect.rb.  I am using Ruby 2.0, but you just need to be using 1.9+.

require 'rubygems'
require 'couchbase'

CONFIG = {
  :node_list => ["localhost:8091"],
  :key_prefix => "pool_",
  :pool_size => 3
}

Here, we’re setting some connection config parameters.  Our node list is standard.  We are setting a key prefix to prepend our docs with "pool_" upon setting.  We are also setting our pool_size to three.

Now let’s write a thread-safe pool object, which will proxy all methods to the Couchbase::Bucket instance:

def connection(bucket)
  @servers ||= {}
  @servers[bucket] ||= begin
                       size = CONFIG[:pool_size]
                       params = CONFIG.merge(:bucket => bucket
                       Couchbase::ConnectionPool.new(size, params)
  end
end

Here you will notice that there is a distinct lack of specificity regarding buckets.  In the config above, we set params to take in the already specified :pool_size,  and the :bucket  that we specify at ops-time.  By writing this small connection method, we are allowing ourselves to specify multiple buckets within our Ruby code.

Let’s test our connection method by writing some ops for Couchbase:

connection("bucket1").set('foo', 'bar')
connection("bucket2").set('bar', 'foo')
 
threads = []
5.times do
  threads << Thread.new do
    connection("bucket1").get('foo')
    connection("bucket2").get('bar')
  end
end

threads.map do |t|
  puts [t.object_id, t.value].join("\t")
end
Here, we’re setting some sample data but specifying our bucket connection within the set command. We are then using the built-in threading within Ruby to perform get operations on our sample objects set.  Here, we are also specifying our connection within the get command.

After writing out this code, you should see some nice data visualization within the Admin UI.  The last block we wrote will join the keys of the sample data with the value and print them into our terminal.

In your terminal, run:

ruby cb_connect.rb

You should see the aforementioned visualization and the output:


 

So we’ve set up our connection using the ConnectionPool property within Couchbase (Only available in Ruby 1.9+), and specified our buckets at ops-time. That is how we can set up multiple bucket connections through pooling in the Couchbase Ruby client.

To see the complete code from this blog, see the Gist.

For those of you Rubyists who are partial to using Rails, I am currently writing a sample application on Rails based around a social gamified leaderboard.  I am using the Couchbase-Model gem that allows for Active_Record-like data modeling.  This app and open-source code will be published alongside another how-to blog within the next week.
 

-  Robin Johnson
Developer Advocate, Europe



Comments

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: