Showing posts with label bigdata. Show all posts
Showing posts with label bigdata. Show all posts

Thursday, November 01, 2018

Consistency levels in Apache Cassandra explained

Cassandra is scalable column-oriented open source NoSQL database. It is the right choice for managing large amounts of structured, semi-structured, and unstructured data across multiple data centers when you need scalability and high availability without compromising performance. In this article, we are going to discuss how the read/write operations are maintained in a cluster and various consistency levels in Cassandra & how can they be applied to our business applications.

According to CAP theorem, it is impossible for a distributed system to simultaneously provide all three guarantees:
  • Consistency -Every node contains same data at the same time
  • Availability- At least one node must be available to serve data every time
  • Partition tolerance -Failure of the system is very rare
"Cassandra is typically classified as an AP system, meaning that availability and partition tolerance are generally considered to be more important than consistency in Cassandra. But Cassandra can be tuned with replication factor and consistency level to also meet C.So Cassandra is eventually consistent."

Replication factor:

Before deep diving into the consistency levels its necessary to understand the term replication factor. It describes how many copies of your data exist. Based on the RF & the consistency levels it is easy to design a very good stable architecture in Cassandra.
The below terms explains how the write/read transactions serve its purpose:
Commit log − The commit log is a crash-recovery mechanism in Cassandra. Every write operation is written to the commit log.
Mem-table − A mem-table is a memory-resident data structure. After commit log, the data will be written to the mem-table. Sometimes, for a single-column family, there will be multiple mem-tables.
SSTable − It is a disk file to which the data is flushed from the mem-table when its contents reach a threshold value

Write path in Cassandra:


When a write is initiated its first captured by the commit logs. Later the data will be captured and stored in the mem-table. Whenever the mem-table is full, data will be written into the SStable data file. All writes are automatically partitioned and replicated throughout the cluster. Cassandra periodically consolidates the SSTables, discarding unnecessary data.

Read path in Cassandra:

For any read operations first, the values are fetched from the mem table and then Cassandra checks the bloom filter(cache) to find the appropriate SSTable that holds the required data.

Consistency Levels :

Consistency levels are used to manage the data consistency versus data availability. Below are the various levels of consistency that can be set to achieve the data consistency in the DB:
ALL- Writes/Reads must be written to the commit log and memtable on all in the cluster.
EACH_QUORUM- Writes/Reads must be written to the commit log and memtable on each quorum of nodes. Quorum is 51% of the nodes in a cluster.
QUORUM- Writes/Reads must be written to the commit log and memtable on a quorum of nodes across all data centers.
LOCAL_QUORUM- Writes/Reads must be written to the commit log and memtable on a quorum of nodes in the same datacenter as the coordinator.
ONE- Writes must/Reads be written to the commit log and memtable of at least one node.
TWO- Writes/Reads must be written to the commit log and memtable of at least two nodes.
THREE- Writes/Reads must be written to the commit log and memtable of at least three nodes.
LOCAL_ONE- Writes/Reads must be sent to and successfully acknowledged by, at least one node in the local datacenter.
ANY- Writes/Reads must be written to at least one node.

How to calculate the DB impact based on these parameters?

Its very easy to calculate the DB impacts for any given RF & read, write Consistency levels. For example, say let us set up a 5 node cluster with 3 RF, Read & Write Consistency level as quorum then the impact would be as below:
1.     Your reads are consistent
2.     You can survive the loss of 1 node without impacting the application.
3.     You can survive the loss of 1 node without data loss.
4.     You are really reading from 2 nodes every time.
5.     You are really writing to 2 nodes every time.
6.     Each node holds 60% of your data.
The same cluster scenario with Read & Write Consistency level as ONE will have the below impact.
1.     Your reads are eventually consistent
2.     You can survive the loss of 2 nodes without impacting the application.
3.     You can survive the loss of no nodes without data loss.
4.     You are really reading from 1 node every time.
5.     You are really writing to 1 node every time.
6.     Each node holds 60% of your data.
Thus the Cassandra cluster architecture can be defined according to our own business need with the optimal use of the resources to yield high performance.

Credits: You can use this Cassandra Parameters for Dummies to find out the impact: https://www.ecyrd.com/cassandracalculator/

Wednesday, July 01, 2015

MySQL to MongoDB Migration in Linux

                   
In the present scenario due to the influence, Bigdata people started to move on from RDBMS to NoSQL for a better excellence. So its necessary to have a database that is good enough in performance and handling the big data. MongoDB a document-oriented database would fit into it which has high availability and scaling. In this post, we are going to see how to migrate all the data from a MySQL instance to MongoDB instance. It can be performed without downtime and the data loss. The tool used to perform the migration task is the Mongify.
                  Mongify tool was developed by Andrew Kalek from Anlek Consulting. This tool helps for migrating data from MySQL to mongodb.No need to worry about the IDs or foreign IDs.Allows embedding data into documents, including polymorphic associations. The problem is it can't do anything to an embedded table. Some packages are to be necessary before installing mongify in Linux so install them as below:
yum install gem
gem install bundler
bundle install
gem install json_pure
To install mongify issue the below command:

           gem install mongify
Setup:
To perform the migration task it needs some requirements in the system:
  • There must be MySQL client running as rpm in because the tool listens only for the rpm connection
  • Also, have a binary or rpm instance of mongodb running in the machine
  • Make sure that both mysql & MongoDB servers are running
Configuration file:
It involves mainly a two files database.config and ruby file translation.rb
  • The database.config file contains the credentials of the mysql such as host,username,password,database and for mongodb credentials like host,database
  • The translation.rb is a ruby file that should contain the details of mysql tables and column values with the proper datatype mentioned
Mongify Check:
             It is used to connect the both mysql and mongodb instances & checks whether connection works.It checks the configuration file database.config whether the database credentials present are correct.Once the connection is established it throws a message “SQL connection works NoSQL connection works”.This command can be performed as below:
mongify check database.config
Mongify Translation:
            The is a ruby file used to translate SQL data before saving it into MongoDB.In order for Mongify to know with the data provide a translation file(translation.rb).It contains the tables and column values with the datatypes defined.This command can be performed as below:
mongify translation database.config > translation.rb
Mongify Process:
              Once the translation file is setup, it tells mongify to move the data by issuing the process command. This command initiates the migration process from the MySQL to MongoDB . This command can be performed as below:
mongify process database.config translation.rb

Its very simple to migrate the data to MongoDB from MySQL.Have a great time guys meet you in another good post.