Relational (SQL) vs non-relational (NoSQL) databases
Tags: database
Published: 2023-07-12 13:00:00

A comparison of relational databases and the different types of non-relational databases

## ⚡ Tl;dr

- Relational databases, sometimes called SQL databases, store data in tables with rows and are good at maintaining reference integrity and data consistency.
- NoSQL databases are designed to have flexible schemas, making them great for modern applications where data structures evolve quickly. There are a few types of NoSQL databases, as listed below.
  - Document store
  - In–memory cache
  - Graph database
  - Search engine database
  - Time series database
  - Key-value database
  - Wide-column database

## 🚀 Let’s kick-off

Relational and non-relational are the two popular types of databases that are most widely used. They are very different in their features and trade-offs and appropriate for different use cases.

In this article, we’ll discuss the differences between relational and non-relational databases and then dive into the various non-relational database types you can choose.

## Relational databases

Relational databases, sometimes called SQL databases or Relational Database Management Systems (RDBMS), store data in tables with columns and rows.

The main advantage of relational databases is the integrity of references which means the data is always accurate and consistent. Using primary/foreign keys ensures that when a row is added or deleted, any references to other tables must also be valid and exist. This prevents orphaned records where the reference points to a row that does not exist, which can cause errors in your application.

Querying relational databases is done by using [Structured Query Language (SQL)](https://www.w3schools.com/sql), which is a robust language designed to create, retrieve, update and delete rows of a table.

Example relational databases:
- [MySQL](https://www.mysql.com)
- [PostgreSQL](https://www.postgresql.org)
- [Amazon RDS](https://aws.amazon.com/rds)
- [Google Cloud SQL](https://cloud.google.com/sql)
- [Azure SQL](https://azure.microsoft.com/en-ca/products/azure-sql)

## Non-relational databases

NoSQL databases are designed to have flexible schemas, making them great for modern applications where data structures might evolve quickly. They typically offer an easy development experience due to the JSON-like format and lack of structure imposed by tables, rows and columns.

NoSQL databases scale horizontally, allowing them to operate at a much larger scale and performance than relational databases. Not worrying about the accuracy of relations makes it much easier to shard non-relational data sets.

There are various types of NoSQL databases which we’ll cover next.

### Document store

Document databases store and query documents in a key-value JSON-like structure, making them easy to work with. The flexible and hierarchical nature of these documents provides straightforward mutation of the data structure as it evolves over time. Document databases typically scale horizontally, allowing them to scale to large datasets.

Examples of document stores:
- [MongoDB](https://www.mongodb.com)
- [Amazon DocumentDB](https://aws.amazon.com/documentdb)
- [Google Cloud Firestore](https://cloud.google.com/firestore)
- [Azure CosmosDB](https://azure.microsoft.com/en-ca/products/cosmos-db)

### In–memory cache

In-memory caches are key-value stores designed to minimize latency by storing the data in the main memory, removing the need for slower disks or SSDs. They are typically used as a caching mechanism and can persist data on disks by periodically taking memory snapshots.

Examples of in-memory cache:
- [Redis](https://redis.com)
- [Amazon MemoryDB](https://aws.amazon.com/memorydb)
- [Google Cloud Memorystore](https://cloud.google.com/memorystore)
- [Azure Cache](https://azure.microsoft.com/en-ca/products/cache)

### Graph database

Graph databases are often the most complex type of database as they’re designed to store and query relations between objects. They store data in a graph-like structure allowing efficient querying of related and inter-connected data. Relationships between nodes are persisted in the database, making it very fast to traverse the graph.

Examples of graph databases:
- [Neo4j](https://neo4j.com)
- [AWS Neptune](https://aws.amazon.com/neptune)
- [Apache TinkerPop](https://tinkerpop.apache.org/index.html)

### Search engine database

Search engine databases are specialized databases for indexing and searching large amounts of data for similar characteristics. They are optimized for operations such as full-text search, ranking based on similarity and complex search expressions. They are often used as secondary databases, indexing data from a primary database and making it searchable.

Examples of search engine databases:
- [Elastic Search](https://www.elastic.co)
- [Amazon OpenSearch Service](https://aws.amazon.com/opensearch-service)
- [Azure Cognitive Search](https://azure.microsoft.com/products/search)

### Time series database

Time series databases (TSBD) are optimized for storing sequential data collected over time, usually associated with a timestamp. They are efficient at collecting, storing and querying large and high-velocity data sets and are great for analyzing how data changes over time. Time series databases are usually append-only.

Examples of time series databases:
- [Prometheus](https://prometheus.io)
- [Amazon Timestream](https://aws.amazon.com/timestream)

### Key-value database

Key-value databases store a collection of key-value pairs that can hold simple or complex object types such as strings, numbers or objects. Due to its simple and optimized key-based structure, it’s highly performant and can reach scales that other database types cannot.

Examples of key-value databases:
- [Amazon DynamoDB](https://aws.amazon.com/dynamodb)
- [Google Cloud Bigtable](https://aws.amazon.com/dynamodb)
- [Azure Table Storage](https://azure.microsoft.com/en-ca/products/storage/tables)

### Wide-column database

Wide-column stores are conceptually similar to relational databases but organize data by columns instead of rows. This allows them to store tables with many columns and handle massive data sets.

Examples of wide-column databases:
- [Apache Cassandra](https://cassandra.apache.org)
- [Amazon Keyspaces](https://aws.amazon.com/keyspaces)
- [Google Cloud Bigtable](https://cloud.google.com/bigtable)

## 🏁 To wrap up

There are many factors and trade-offs involved when making a decision to choose a specific type of database. It’s essential to research and experiment with different technologies to find the right solution for your use case.

Let us know if we missed your favourite database!

Stay chill 🧊