Introduction to NoSQL Databases and Apache Cassandra

Traditional databases work well when data is structured and small, but they start to struggle at large scale and in distributed systems. This is where NoSQL databases come in. They are designed for flexibility, speed, and horizontal scalability across multiple machines. In this blog, we’ll understand the basics of NoSQL and explore Apache Cassandra, a popular distributed database built for handling massive amounts of data efficiently.

Most traditional databases (like SQL databases) store data in fixed tables with strict rules. This works well for many apps, but it becomes difficult when data grows very large or comes from many different sources.

To solve this, we use NoSQL databases.


What is a NoSQL database?

A NoSQL database is a type of database that is designed to:

  • Handle large amounts of data

  • Work well in distributed systems (multiple machines)

  • Be flexible with data structure

  • Scale easily as data grows

Instead of strict tables and joins, NoSQL focuses on speed and scalability.


What is Apache Cassandra?

Apache Cassandra is a NoSQL database built for handling huge amounts of data across many servers.

Think of it like:

A system where data is spread across multiple machines, but still behaves like one database.

It is used when:

  • You need very fast writes

  • You cannot afford downtime

  • Data keeps growing continuously (logs, events, time-series data)


Simple Cassandra example

Let’s create a basic table to store user activity.

CREATE TABLE user_activity (
      user_id UUID, 
      activity_time TIMESTAMP, 
      activity_type TEXT, 
      page TEXT, 
      PRIMARY KEY (user_id, activity_time));
CREATE TABLE user_activity (
      user_id UUID, 
      activity_time TIMESTAMP, 
      activity_type TEXT, 
      page TEXT, 
      PRIMARY KEY (user_id, activity_time));
CREATE TABLE user_activity (
      user_id UUID, 
      activity_time TIMESTAMP, 
      activity_type TEXT, 
      page TEXT, 
      PRIMARY KEY (user_id, activity_time));


What this means

🔑 Primary Key

(user_id, activity_time)
(user_id, activity_time)
(user_id, activity_time)

This has 2 parts:

  • Partition Key → user_id

    • Decides where data is stored in the cluster

    • All data for a user goes to the same node

  • Clustering Key → activity_time

    • Sorts data inside that partition

    • Helps retrieve data in order (latest activity first, etc.)


How data is stored

For each user:

user_1 all their activities grouped together
user_2 all their activities grouped together
user_1 all their activities grouped together
user_2 all their activities grouped together
user_1 all their activities grouped together
user_2 all their activities grouped together

Inside each user group:

sorted by activity_time
sorted by activity_time
sorted by activity_time


In Cassandra (and most NoSQL systems), you don’t design tables first. You design them based on the query you want to run.


In Cassandra, this query tells us:

“I need fast lookups by year AND artist_name”

So we must design the table so these fields become part of the primary key.

SELECT * 
FROM songs 
WHERE year = 1970 AND artist_name = "The Beatles";
SELECT * 
FROM songs 
WHERE year = 1970 AND artist_name = "The Beatles";
SELECT * 
FROM songs 
WHERE year = 1970 AND artist_name = "The Beatles";


📦 Table design in Cassandra

CREATE TABLE songs_by_year_artist (   
  year INT,    
  artist_name TEXT,    
  song_id UUID,    
  song_title TEXT,    
  album TEXT,    
  PRIMARY KEY ((year, artist_name), song_id));
CREATE TABLE songs_by_year_artist (   
  year INT,    
  artist_name TEXT,    
  song_id UUID,    
  song_title TEXT,    
  album TEXT,    
  PRIMARY KEY ((year, artist_name), song_id));
CREATE TABLE songs_by_year_artist (   
  year INT,    
  artist_name TEXT,    
  song_id UUID,    
  song_title TEXT,    
  album TEXT,    
  PRIMARY KEY ((year, artist_name), song_id));


🧠 What’s happening here

🔑 Partition key

(year, artist_name)
(year, artist_name)
(year, artist_name)

This means:

  • Data is grouped by year + artist

  • Your query can directly jump to the right partition


🔢 Clustering key

song_id
song_id
song_id

This helps:

  • store multiple songs by the same artist in the same year

  • keep rows unique and organized


Why Cassandra is different from SQL

  • No joins

  • Data is stored based on how you query it

  • Designed for speed and scale, not complex relationships

So instead of asking:

“How do I join tables?”

You design tables based on:

“How will I read this data?”


One-line summary

Apache Cassandra is a NoSQL database that stores data across multiple machines and is designed for fast, scalable, always-on applications.

hiker in nature

Subscribe to my Newsletter

Sign up to stay updated about my latest work and adventures. No Spam, No BS. Promise!

hiker in nature

Subscribe to my Newsletter

Sign up to stay updated about my latest work and adventures. No Spam, No BS. Promise!