NO_SQL_DB
NO_SQL_DB
15 font size =
13 ( best to read)
NoSQL databases (AKA "not only SQL") store data differently than relational
tables. NoSQL databases come in a variety of types based on their data model.
The main types are document, key-value, wide-column, and graph. They
provide flexible schemas and scale easily with large amounts of big data and
high user loads.
In this article, you'll learn what a NoSQL database is, why (and when!) you
should use one, and how to get started.
Table of contents
• What is a NoSQL Database?
• Types of NoSQL Database
• Brief History of NoSQL Databases
• NoSQL Database Features
• Relational database vs NoSQL database example
• Differences between RDBMS and NoSQL databases
• NoSQL use cases
• When should NoSQL be Used?
• NoSQL Database Misconceptions
• NoSQL Query Tutorial
• Summary
• FAQs
Document-oriented databases
A document-oriented database stores data in documents similar to JSON
(JavaScript Object Notation) objects. Each document contains pairs of fields and
values. The values can typically be a variety of types, including things like
strings, numbers, booleans, arrays, or even other objects. A document
database offers a flexible data model, much suited for semi-structured and
typically unstructured data sets. They also support nested structures, making it
easy to represent complex relationships or hierarchical data.
Examples of document databases are MongoDB and Couchbase. A typical
document will look like the following:
{
"_id": "12345",
"name": "foo bar",
"email": "[email protected]",
"address": {
"street": "123 foo street",
"city": "some city",
"state": "some state",
"zip": "123456"
},
"hobbies": ["music", "guitar", "reading"]
}
Key-value databases
A key-value store is a simpler type of database where each item contains keys
and values. Each key is unique and associated with a single value. They are
used for caching and session management and provide high performance in
reads and writes because they tend to store things in memory. Examples are
Amazon DynamoDB and Redis. A simple view of data stored in a key-value
database is given below:
Key: user:12345
Value: {"name": "foo bar", "email": "[email protected]", "designation": "software
developer"}
Wide-column stores
Wide-column stores store data in tables, rows, and dynamic columns. The data
is stored in tables. However, unlike traditional SQL databases, wide-column
stores are flexible, where different rows can have different sets of columns.
These databases can employ column compression techniques to reduce the
storage space and enhance performance. The wide rows and columns enable
efficient retrieval of sparse and wide data. Some examples of wide-column
stores are Apache Cassandra and HBase. A typical example of how data is
stored in a wide-column is as follows:
Graph databases
A graph database stores data in the form of nodes and edges. Nodes typically
store information about people, places, and things (like nouns), while edges
store information about the relationships between the nodes. They work well for
highly connected data, where the relationships or patterns may not be very
obvious initially. Examples of graph databases are Neo4J and Amazon Neptune.
MongoDB also provides graph traversal capabilities using the $graphLookup
stage of the aggregation pipeline. Below is an example of how data is stored:
Multi-model databases
In-memory databases
In-memory databases store data in memory in order to provide ultra-low
latency for real-time applications. Redis and Valkey are examples of in-memory
NoSQL databases. In-memory databases are most commonly used for caching,
messaging, streaming, and real-time analytics.
Quick comparison of types of databases — NoSQL
Each of the NoSQL databases offers different features. For example, graph
databases could be more suited for analyzing complex relationships and
patterns between entities, while document databases provide a more flexible,
natural way of storing and retrieving large data volumes of similar types as
documents. The choice of database depends on the use case you want to
develop.
BASE compliance
NoSQL databases are BASE compliant, i.e., basic availability soft state eventual
consistency. Basic availability refers to the ability of the system to tolerate a
partial failure (like a loss of a node). Soft state means that the system allows
temporary inconsistencies before eventually achieving consistency
automatically over time. BASE compliance ensures high availability, faster data
processing, scalability, and flexibility. However, MongoDB can also be
configured to provide multi-document ACID compliance.
Learn more about the advantages of NoSQL databases.
https://www.mongodb.com/developer/products/mongodb/map-terms-concepts-
sql-mongodb/
Differences between RDBMS and NoSQL databases
There are a variety of differences between relational database management
systems and non-relational databases. One of the key differences is the way
data is modeled in the database. Some key differences of each feature is listed
below:
Data modeling
NoSQL: Data models vary based on the type of NoSQL database used — for
example, key-value, document, graph, and wide-column — making the model
suitable for semi-structured and unstructured data.
RDBMS: RDBMS uses a tabular data structure, with data represented as a set
of rows and columns, making the model suitable for structured data.
Schema
NoSQL: It provides a flexible schema where each set of documents/row-
column/key-value pairs can contain different types of data. It’s easier to change
schema, if required, due to the flexibility.
RDBMS: This is a fixed schema where every row should contain the same
predefined column types. It is difficult to change the schema once data is
stored.
Query language
NoSQL: It varies based on the type of NoSQL database used. For example,
MongoDB has MQL, and Neo4J uses Cypher.
RDBMS: This uses structured query language (SQL).
Scalability
NoSQL: NoSQL is designed for vertical and horizontal scaling.
RDBMS: RDBMS is designed for vertical scaling. However, it can extend limited
capabilities for horizontal scaling.
Data relationships
NoSQL: Relationships can be nested, explicit, or implicit.
RDBMS: Relationships are defined through foreign keys and accessed using
joins.
Transaction type
NoSQL: Transactions are either ACID- or BASE-compliant.
RDBMS: Transactions are ACID-compliant.
Performance
NoSQL: NoSQL is suitable for real-time processing, big data analytics, and
distributed environments.
RDBMS: RDBMS is suitable for read-heavy and transaction workloads.
Data consistency
NoSQL: This offers eventual consistency, in most cases.
RDBMS: This offers high data consistency.
Distributed computing
NoSQL: One of the main reasons to introduce NoSQL was for distributed
computing, and NoSQL databases support distributed data storage, vertical and
horizontal scaling through sharding, replication, and clustering.
RDBMS: RDBMS supports distributed computing through clustering and
replication. However, it’s less scalable and flexible as it’s not traditionally
designed to support distributed architecture.
Fault tolerance
NoSQL: NoSQL has built-in fault tolerance and high availability due to data
replication.
RDBMS: RDBMS uses replication, backup, and recovery mechanisms. However,
as they are designed for these, additional measures like disaster recovery
mechanisms may need to be implemented during application development.
Data partitioning
NoSQL: It’s done through sharding and replication.
RDBMS: It supports table-based partitioning and partition pruning.
Learn more about data partitioning here.
Summary
NoSQL databases provide a variety of benefits, including flexible data models,
horizontal scaling, lightning-fast queries, and ease of use for developers. NoSQL
databases come in a variety of types, including document stores, key-values
databases, wide-column stores, graph databases, and multi-model databases.
MongoDB is the world's most popular NoSQL database. Learn more about
MongoDB Atlas, and give the free tier a try.
Excited to learn more now that you have your own Atlas account? Head over to
MongoDB University where you can get free online training from MongoDB
engineers and earn a MongoDB certification. The Quickstarts are another great
place to begin; they will get you up and running quickly with your favorite
programming language.
Advantages of NoSQL
Agile development
The flexibility of NoSQL complements agile app development. A NoSQL
database can store many types of data in its native format and allows the data
model to be defined and adapted as you go, so developers can get going fast,
spend less time on data transformation, and iterate quickly.
Scalability
Unlike relational databases, NoSQL databases make it easy to increase capacity
as data and traffic grows—in most cases, with zero downtime. Cloud-based
databases are even easier to scale on demand, offering autoscaling features
and flexible pricing models.
High availability
NoSQL data architectures are distributed by design and have no single point of
failure. They also provide easy replication, making them more resistant to
unplanned outages and disruptions.
Faster queries
Unlike relational databases, which are normalized to reduce data duplication,
NoSQL is optimized for fast querying. It typically does not require complex joins,
meaning that database queries return results more quickly.
Disadvantages of NoSQL
While NoSQL databases have gained huge popularity in recent years, there are
some drawbacks to using them over relational databases. They are still
relatively new and may lack the maturity of relational databases. Overall,
NoSQL comes with less developer expertise, fewer available tools and products,
and less support should undocumented issues arise.
In addition, NoSQL has no lingua franca like SQL—each database may have its
own language for querying and managing data. These languages are often
similar but not fully compatible with the SQL standard.
In many cases, NoSQL databases lack the data integrity safeguards and high
level of data consistency that are standard in SQL databases. However, there
are some, like Firestore and MongoDB Atlas, that do support ACID transactions.
NoSQL databases usually aren’t a good choice for applications that run complex
queries and joins. Managing indexes and queries across multiple nodes would
be slow and may not return consistent results.
Still, eventual consistency models are sufficient for most NoSQL use cases,
where a small millisecond delay doesn’t matter. For many applications, high
availability and speed far outweighs the need for strong global consistency.
Optimal Relational databases are designed NoSQL databases are designed for
workloa for transactional and strongly a number of data access patterns
ds consistent online transaction that include low-latency
processing (OLTP) applications. applications. NoSQL search
They are also good for online databases are designed for
analytical processing (OLAP). analytics over semi-structured
data.
APIs Requests to store and retrieve data Object-based APIs allow app
are communicated using queries developers to easily store and
that conform to a structured query retrieve data structures. Partition
language (SQL). These queries are keys let apps look up key-value
parsed and executed by the pairs, column sets, or semi-
relational database. structured documents that contain
serialized app objects and
attributes.
When should you choose NoSQL databases over SQL databases
A NoSQL database is best for handling indeterminate, unrelated, or rapidly
changing data. It is intuitive to use for developers when the application dictates
the database schema. You can use it for applications that:
• Need flexible schemas that enable faster and more iterative development.
• Prioritize performance over strong data consistency and maintaining
relationships between data tables (referential integrity).
• Require horizontal scaling by sharding across servers.
• Support for semi-structured and unstructured data.
You don't always have to choose between a non-relational and relational
database schema. You can employ a combination of SQL and NoSQL databases
in your applications. This hybrid approach is quite common and ensures each
workload is mapped to the right database for optimal price performance.
Nesting data
With document databases like MongoDB it is common to put more data in a
smaller number of collections. For example, in a blogging application, one
might choose to store comments within the blog post document, so that with a
single retrieval one gets all the comments. Thus in this approach a single
document contains all the data needed for a specific task.