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 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,
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.
The value can also contain json with data like
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.
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."
In MySQL, the tables store data in a fixed schema. Let's see an example.
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?
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:
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.
Here's a comparison table between relational and non-relational databases:
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?
Subscribe to Programiz PRO Blog!
Be the first to receive the latest tutorial from Programiz by signing up to our email subscription. Also more bonus like inside looks on the latest feature and many more.