trlogic Silver Logo

Cassandra with Spring Framework

trlogic Silver Logo

Cassandra is a distributed NoSQL database. Cassandra is developed with java. Cassandra is highly popular because of some technical reasons such as scalability, its distributed structure that can support multi datacenter, keeping data at very high size, very high reading and writing speed. Companies such as Facebook, Twitter and Cisco use this database.

Basic Features of Cassandra

Horizontal Scalability:

Cassandra allows to increase capacity by adding new servers in the cluster. It has nearly linear scaling performance. Therefore Cassandra guarantees high performance.

No Single Point of Failure:

Because Cassandra is designed to provide continuous accessibility, it is not possible for the conglomerate to fail because of a server error. It does not use “master / slave” or replication structures in classic databases. The data are distributed to the nodes in the cluster. It is copied to the server in the desired number (replication factor). Since all servers are designed to respond to incoming requests, the problem with one of the servers will not affect the whole cluster.

Replication:

Every server in the cluster can respond to reading and writing requests. The first server the request comes in is called the “coordinator” and allows copying for the desired number of servers. The number of the copies (Replication Factor) can be set in the database.

Consistency Levels:

This level determines how many servers will receive the read request and how many requests will be returned to the client after the data is written to the client “completed”.

The level of data consistency in the Cassandra database can be set on a cluster and query basis. High data consistency level negatively affects performance. For this reason, it will be correct to adjust it appropriately.

The codes belong to spring part are given as follows.

Image for post
DevBootStrap

Image for post
Customer Controller

Image for post
Customer Implementation

Image for post
Customer Class

Image for post
Customer Repository Interface

Image for post
Name Getter Service Implementation

Image for post
Name Setter Service Implementation

Image for post
Name Getter Service Interface

Image for post
Name Setter Service Interface

Image for post
Main Class

Image for post
Index.html

Image for post
Application Properties file

Image for post
Project Tree

Before running this spring application, first of all, Cassandra has to be up and running. To do this, open the terminal and write as follows.

docker pull cassandra
Image for post

After that, write

docker run -p 9042:9042 --rm --name cassandra -d cassandra
Image for post

To open cql, write following commands.

docker run -it --link cassandra:cassandra --rm cassandra cqlsh cassandra
Image for post

To create a keyspace,

CREATE KEYSPACE mykeyspace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

To create table and indexes,

Image for post
Image for post

After all steps, run the project and open web browser and write localhost:8080. Here is the example.

Image for post