What is a Non-Relational Database? A Beginner's Guide

What is a Non-Relational Database? A Beginner's Guide

Suppose you're organizing an extensive library with thousands of books. In a traditional relational database, you would categorize each book by author, title, and publisher.

As you acquire more books, you realize some don't fit your existing categories. For example, a cookbook that includes both recipes and personal stories.

This is where non-relational databases come in. Instead of fitting every book into a predefined structure, you can use a more flexible approach. You can group books by tags and add/remove them without conforming to a rigid schema.

A non-relational database allows you to store data without defining strict relationships between tables. This makes handling large amounts of data easier and adaptable to changing needs.

This blog provides an overview of non-relational databases and explains scenarios in which they outperform relational databases.

Let's get started!


Non-Relational Database

Non-relational databases, or NoSQL, depart from the traditional tabular format of relational databases.

No relations, no tables with rows and columns, just plain unstructured (or semi-structured) data in a document, like json. For example,

{
"book_id": 1,
"title": "Attack on Titan",
"author": "Hajime Isayama",
"price": $50
}
Non-Relatonal Database in JSON

This is typical json data in a non-relational database.

Examples of common non-relational databases are:

  • MongoDB: a document-oriented database that stores data in a flexible JSON-like format, known for its easy scaling and querying of data.
  • Cassandra: a distributed database that handles large amounts of data across multiple nodes.
  • Redis: an in-memory data store widely used for caching, messaging, and real-time data processing.
  • Amazon DynamoDB: a fully-managed service offering fast, scalable, and flexible data storage.

Why Use a Non-Relational Database?

Let's start with the problem with relational databases.

Relational databases use tables to store data efficiently. For example, MySQL enforces relationships between tables and requires a fixed schema to store data — like a predefined set of rows and columns.

Adding new data to this database requires defining each row and column's data type and size.

While this structure is beneficial for organizing data, it's an intensive process requiring significant memory and computing power.

As the application grows, relational databases become complicated and may fail to handle the load. Hence, the introduction of non-relational databases.

Non-relational databases allow us to store data in a single document, making handling data on large applications easier.


How Does Non-Relational Database Work?

Non-relational databases work by using key-value pairs to store and retrieve data.

For example, to store information about a book, you can use the book's barcode as the key and name as the value.

Key

Value

0938279193

Harry Potter and the Philosopher's Stone

8576382930

Lord of the Rings

8937268374

Attack on Titan

The value can also contain json with data like

{
name: "Harry Potter and the Philosopher's Stone"
price: $25
description: "The first book of the Harry Potter book series"
}
Json value in a Non-Relational Database

With thousands of books stored in your database, keeping track of each item's location is challenging. This is where the primary key comes in.

The primary key serves as a unique identifier for each item in the database.

They use a hash function to convert each item's primary key into a number that falls into a fixed range.

Key

Hash

Value

0938279193

101

Harry Potter and the Philosopher's Stone

8576382930

32

Lord of the Rings

8937268374

89

Attack on Titan


Here, the database can quickly locate the data based on its primary key, allowing for efficient data storage and retrieval. This ensures fast and reliable access to data, even with large datasets.


Applications That Use Non-relational Databases

Some applications using non-relational databases are:

  • Facebook, Twitter - to store and manage user-generated content
  • Amazon, eBay - to store product catalogs and customer data
  • Xbox Live, Steam - to store player data and game state information

The reason why such big applications use non-relational databases is because of their scaling capability. Let's understand scaling with the analogy of buildings.

Vertical scaling means adding more floors to an existing building, while horizontal scaling means adding more buildings.

Non-relational databases can scale both vertically and horizontally. But relational databases can only scale vertically. This is why non-relational databases are much more powerful.


Non-Relational Database Vs. Relational Database

The previous section discussed why we need non-relational databases over relational databases. But there is more to explore.

Suppose we have a database for a bookstore with two tables: "books" and "authors."

MySQL Database

In MySQL, the tables store data in a fixed schema. Let's see an example.

Books Table:

book_id

title

author_id

price

1

Harry Potter

1

25

2

Lord of the Rings

2

19


Authors Table:

author_id

name

1

J.K. Rowling

2

J.R.R. Tolkien


In this example, the data is stored in multiple tables, with relationships between the tables based on the author ID.

To learn more, visit What is relational database?

MongoDB Database

As MongoDB uses a flexible, document-oriented data model, we can store data more fluidly.

Here's how the same data is stored in MongoDB:

{
"book_id": 1,
"title": "Harry Potter",
"author": {
"author_id": 1,
"name": "J.K. Rowling"
},
"price": 25
},
{
"book_id": 2,
"title": "Lord of the Rings",
"author": {
"author_id": 2,
"name": "J.R.R. Tolkien"
},
"price": 19
}
MongoDB Database


Here, the data is stored as JSON documents with nested fields for the author's information.

The data is not separated into multiple tables, and there are no predefined relationships between the data.

This non-relational approach allows for greater flexibility and querying of the data.

Comparison Table

Here's a comparison table between relational and non-relational databases:


Relational Database

Non-relational Database

Data consistency

Yes

No

Scaling

No

Yes

Resource intensive

No

Yes


To learn more, visit Relational Vs. Non-Relational Database.


In short, non-relational databases are a powerful tool for managing large amounts of data. They offer flexibility, scalability, and performance advantages over relational databases.

Understanding the differences between relational and non-relational databases helps you choose the right database for your project. To learn more, refer to the guide SQL vs. NoSQL: Which Database Should You Use?