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.
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. 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.
Following are the documents stored in the bucket Employee.
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.
Example:- Let’s retrieve the employee whose name is Bhawna.
Oops… instead of the result, you are getting this message.
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.
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. 🙂