Couchbase – Lets Get Started

Reading Time: 4 minutes

In this blog, we will learn about the basics of Couchbase and some facts that a developer should know about Couchbase.

What is Couchbase?

Couchbase is a NoSQL database having the following features –

  • Distributed:- Couchbase is distributed in nature, data can be distributed in form of multiple copies at different data center.
  • Document-Oriented:- It stores keys and values as JSON documents.
  • Flexible Data Model:- Couchbase allows you to not enforce any type of schema on the document. Each document can have a different structure. 
  • Scalable:- As Couchbase follow a peer to peer architecture in which all the nodes are treated as same(no master/slave concept), and thus allows for easy scaling and high performance.

While dealing with Couchbase, you will come across some new terms like BUCKET, DOCUMENT, DOCUMENT ID. So let’s discuss these important terms.

BUCKET: Bucket is the data container used in Couchbase Server. It is basically a logical collection of all the related Documents. Every Bucket has some associated properties like its size, replication, persistence which can be configured.

DOCUMENT: Document stores data in the form of key and value, and it is uniquely identified by Document Id. Well, then Couchbase does not allow me to create two Documents with the same ID. Its a document on which we can perform CRUD(CREATE, RETRIEVE, UPDATE AND DELETE).

Let’s compare the concept mapping of Couchbase with RDBMS for your better understanding.

MySQL Couchbase

Database

Bucket

Table

Bucket

Row

Document

Column

Field

Before we start, let’s download the couchbase server(for Linux platform).
Start the couchbase as a service by using the following command.
–  sudo service couchbase-server start
For couchbase UI, type localhost:8091 in your browser.
Once you provide the credentials(username and password) to your server, you are good to go and see the following dashboard.Screenshot from 2018-12-27 11-41-31 After clicking on Buckets, you can see all your buckets. Here I have created one bucket – Employee. If you want to view all your documents stored in the bucket, click on Documents and for configurations related to the bucket (RAM, replication, persistence and many more) click on Statistics.
Screenshot from 2018-12-27 12-17-05

Following are the documents stored in the bucket Employee.Screenshot from 2018-12-27 12-11-52

Lets Query !!!

Now for query purpose, click on query link in the dashboard and write your query in the Query Editor.
Example:- Let’s retrieve the Employee on the basis of Document Id.

Output


Example:- Let’s retrieve the employee whose name is Bhawna
.

Oops… instead of the result, you are getting this message.

Output

So, for retrieving employee what you really need is an Index. An index is a data structure that provides quick and efficient means to access data. We have two types of Indexes – Primary and Secondary.
A Primary index of N1QL will allow you to query any field of your JSON. As such, it takes up more space and is less efficient. It is the basic requirement to be able to use the query language on your data.

A secondary index of N1QL is targeted at the specific field(s) and will be more performant for queries involving these fields.

 Create Index for Bucket Employee

Now run again the select query, you will see the expected response.

Example: Insert Student having name Reeta

As Couchbase does not have a fixed schema, you can insert any type of Document having different key and value.

Example: Delete employee having name Bhawna

That’s all about query! For more details, you can dig into  Lets Query
So, let’s see some facts related to Couchbase.

Facts Related to Couchbase

Every database is viewed from two perspectives one from the developer and another from Administrator(DBA). So there are certain things that developer should know about Couchbase like:
1) Start with everything in one bucket if you are migrating from RDBMS.
2) Use 5 or fewer buckets.
3) If you want right Performance, create the right Index.
4) Writes are async in nature by default.

Conclusion

So, in this blog, we covered the brief introduction of Couchbase. We also dig into the basics of Query like how we can perform insertion, deletion, and retrieval from couchbase. We also learned about the types of indexes and some facts related to couchbase.

Hope this is helpful and gives you the basic understanding of Couchbase. Please feel free to provide your suggestions. 🙂

References:
https://blog.couchbase.com/10-things-developers-should-know-about-couchbase/
https://forums.couchbase.com/
https://docs.couchbase.com/home/
https://docs.couchbase.com/server/6.0/install/migrate-mysql.html

blog-footer

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!