dot Stop testing, start deploying your AI apps. See how with MIT Technology Review’s latest research.

Download now

Document Databases

Unlock the power of structured documents with a NoSQL document database

What is a document database?

A document database (also called a NoSQL document store) is a non-relational database that stores data as structured documents. A document is typically stored as a JSON file. It is a simpler way to store data in JSON format rather than the complexity of SQL with multiple tables of rows and columns. Document databases are often used in scenarios where data is diverse and changes over time. They are particularly popular in web development and real-time applications due to their ability to handle high traffic and large amounts of data.

Relational vs Document Databases

Relational and document databases represent two fundamentally different approaches to data management. Here’s a more detailed comparison:

Data Structure

Relational databases use a table-based structure where data is organized into rows and columns. Each row represents a record, and each column represents a specific field of the record. The structure is rigid and requires a predefined schema that outlines the structure and type of data to be stored. This flexibility allows for rapid development and iteration, making document databases a popular choice for agile development environments.

On the other hand, document databases use a flexible, JSON-like format where data is stored in documents. These documents have the capacity to hold a diverse range of key-value pairs, key-array pairs, or even encompass nested documents. The structure is flexible and does not require a predefined schema. This means you can store data with different structures in the same database.

Data Integrity and Consistency

Relational databases prioritize data integrity and consistency. They use ACID (Atomicity, Consistency, Isolation, Durability) transactions to ensure that data remains consistent throughout the database. This is particularly important in applications where data consistency is critical, such as financial systems.

Document databases, however, are designed for flexibility and scalability. They typically use a model known as eventual consistency, which allows for some temporary inconsistency in exchange for improved performance and scalability. This makes document databases a good fit for applications where data requirements can change rapidly, and scalability is more important than immediate consistency. They are often used in scenarios where high performance and availability are more important than strict consistency, such as social media platforms, content management systems, and real-time analytics.

Query Language

Relational databases use SQL (Structured Query Language) for querying and manipulating data. SQL is a powerful language that can perform complex queries and data manipulations.

Document databases, however, require different querying languages as the record structure isn’t aligned with the assumptions inherent within SQL. MongoDB, for example, uses a method-based query language that’s built into its JavaScript interface. Amazon DynamoDB uses a proprietary, HTTP-based query language. These querying languages are designed to be flexible and easy to use, allowing developers to query and manipulate data in a way that aligns with the flexible nature of document databases.

Scaling

Relational databases are typically scaled vertically by adding more resources (CPU, RAM, SSD) to a single server. This can become expensive and has its limits.

In contrast, document databases are architected with horizontal scalability in mind. This implies that you can enhance the database’s capacity by incorporating more servers. As such, they are an apt choice for applications that require managing substantial data volumes or catering to a vast user base. The ability to scale horizontally equips document databases to efficiently manage high traffic and extensive data, thereby making them a preferred option for applications dealing with big data.

When to use a document database

Non-Tabular Data

Document databases are an excellent choice when your application needs to manage data that isn’t neatly organized into tables. Traditional relational databases are designed to handle structured, tabular data, but they can struggle with semi-structured or unstructured data. Document databases, on the other hand, are designed to handle a wide variety of data types, including complex nested structures, arrays, and other data types that don’t fit neatly into a table. This makes them an excellent choice for managing diverse data in a single database.

High Volume of Small, Continuous Reads and Writes

If your application needs to handle a high volume of small, continuous reads and writes, a document database can be a good choice. These databases are designed for high performance and can handle large volumes of data. They also typically offer in-memory caching, which can significantly speed up access times for frequently accessed data. This makes them a great fit for applications that need to process a large number of requests in a short amount of time, such as real-time analytics or high-traffic web applications.

CRUD Applications

Document databases are well-suited to applications that require Create, Read, Update, and Delete (CRUD) operations. The flexible, schema-less nature of document databases makes them ideal for applications where the data model may evolve over time. This flexibility allows developers to easily add new fields to documents without requiring a database schema change. This makes them a great fit for agile development environments where requirements can change rapidly.

Wide Variety of Access Patterns and Data Types

If your application needs to operate over a wide variety of access patterns and data types, a document database could be a good fit. These databases are designed to be flexible and perform well under a variety of conditions. They can handle structured and unstructured data, and they can scale horizontally to handle large volumes of data. This makes them a great choice for applications that need to handle diverse data and a wide range of access patterns, such as content management systems or e-commerce platforms.

Rapid Development and Iteration

Document databases are a great fit for agile development environments where requirements can change rapidly. The absence of a fixed schema in document databases enables developers to modify the data model in real-time as the application progresses, eliminating the need for lengthy database migrations. This allows for faster development and iteration, making document databases a popular choice for modern, agile development teams.

Complex Data Structures

If your application needs to store complex data structures, such as nested objects or arrays, a document database can be a good choice. Document databases store data in a format similar to JSON, which can naturally represent complex data structures. This makes them a great choice for applications that need to handle complex data, such as social media platforms or real-time analytics applications. This makes them a great choice for applications that need to handle large volumes of data or serve a large number of users, such as big data applications or high-traffic web applications. 

Scalability

When your application needs to scale, either to handle larger volumes of data or to serve a growing number of users, a document database can be a good choice. Document databases are designed to scale horizontally, meaning you can add more servers to handle more data as your needs grow.

Polyglot Persistence

In scenarios where different data storage technologies are used for different data storage needs within the same application (polyglot persistence), document databases can be an excellent choice for storing semi-structured data. This allows for a more flexible and adaptable data architecture, making document databases a great choice for modern, complex applications.

Remember, the choice of database should always be dictated by the specific requirements of your application. Document databases offer many advantages, but they are not the best choice for every situation. Always consider your application’s data access patterns, performance requirements, and scalability needs when choosing a database.

Use cases for document databases

Content Management Systems (CMS)

Document databases are ideal for content management systems. These systems often need to store a variety of content types, including blog posts, user comments, and multimedia content. The flexible, schema-less nature of document databases makes it easy to store and retrieve this diverse content. This flexibility allows CMS to easily adapt to changing content requirements, making document databases a popular choice for these systems.

User Profiles

Applications that manage user profiles can benefit from document databases. User profiles often contain a mix of standard and custom attributes. With a document database, you can easily add new attributes as your application evolves without having to modify a database schema. This makes document databases a great fit for applications that need to handle diverse user data, such as social media platforms or customer relationship management (CRM) systems.

E-Commerce Websites

Document databases align well with the needs of e-commerce platforms. Such websites have to handle a diverse range of data, encompassing product catalogs, customer profiles, and transaction records. The adaptability of document databases facilitates the effortless addition or modification of product attributes. Hence, they are a favored choice for e-commerce sites that require managing varied product information and swiftly fluctuating stock levels.

Middleware Applications that Use JSON

Middleware applications that use JSON for data interchange can benefit from using document databases. Since document databases typically store data in a format similar to JSON, they can store and retrieve data without the need for much data mapping or transformation. This makes them a great fit for modern web applications that use JSON for data interchange.

RedisJSON

JSON is a very popular format for exchanging data in web applications. Redis Enterprise provides in-memory manipulation of JSON documents at high velocity and volume. With Redis JSON, you can natively store document data in a hierarchical, tree-like format to scale and query documents efficiently, significantly improving performance over storing and manipulating JSON with Lua and core Redis data structures. The native data type it applies is the Standard ECMA-404, using the Standard JSON Data Interchange Syntax. Redis JSON outperforms any other technique for storing JSON objects in Redis, such as using Lua scripting for manipulating JSON or MessagePack objects.

https://www.youtube.com/embed/2mFakgHKme4

Next section  ►  TimeSeries databases