dot Redis LangCache and the next era of fast, accurate AI are here.

Get the details

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

Frequently asked questions

More questions? See our Docs page

What is the difference between SQL and a document-oriented database? plus-white minus-white

SQL databases use a structured data schema that stores data in tables, rows, and columns, and use Structure Query Language (SQL) to query the database. SQL databases follow ACID (Atomicity, Consistency, Isolation, Durability) compliance to ensure data consistency. Document-oriented databases (also known as document databases) store data in documents – often JSON format – and don’t require a rigid schema. Document-oriented databases typically have their own specific query language or API to query data, and they are typically not ACID-compliant.

When should you use NoSQL databases? plus-white minus-white

A NoSQL database, like a document-oriented database, is a better choice when data flexibility and rapid scalability are paramount. This is because NoSQL databases excel for data that is unstructured and does not have a defined schema (content management, IoT, analytics, etc.)

Are document-oriented databases a type of relational database? plus-white minus-white

No, document-oriented databases are not a type of relational database. They are considered NoSQL (non-relational) databases because they don’t store data in relational structures like tables, rows, and columns.

What are the advantages of using a document-oriented database? plus-white minus-white

The top advantages of using a document-oriented database are data flexibility (each document can have a different structure), scalability (excellent with horizontal scaling), and powerful query capabilities (enable advanced searches and aggregations without complex SQL).

What are the best practices for indexing in document-oriented databases? plus-white minus-white

Index only the fields that matter. Like those used in frequent queries, filters, and sorts to improve read performance and write efficiency. Use compound indexes for common query patterns, choose the right index time (text, geospatial, vector), and regularly monitor performance to keep indexes lean and effective as data grows.

How do document-oriented databases handle schema changes? plus-white minus-white

Document-oriented databases are typically schemaless. This means that they don’t require or enforce rigid schemas like relational databases, and each document can have a different structure. However, if there has been a major schema change that introduces or changes an indexed field (something you want to search on), the index would need to be created or updated.

Additionally, it is a best practice to version schemas in your document-oriented database so that changes to data structures being stored or queried can be easily managed over time.

How do document-oriented databases handle data consistency and transactions? plus-white minus-white

Document-oriented databases handle consistency through configurable write concerns, allowing users to choose between strong or eventual consistency. Some use quorum-based writes to ensure durability, while modern implementations support multi-document ACID transactions for operations that require strict consistency.

How is Redis Enterprise used as a document-oriented database in real-world applications? plus-white minus-white

Because of Redis’s ability to store data as JSON documents, create indexes, and search/query data in real time, Redis can be used as a document database.

Common document-oriented database use cases for Redis are store session and user profile data, IoT data from various sensors and connected devices, and storing product catalog data as JSON documents.

How does Redis Enterprise ensure high performance and scalability for document-oriented databases with large datasets? plus-white minus-white

Redis ensures high performance by storing data in RAM, which allows for reads and writes with sub-millisecond latency even at extremely high throughputs. Redis’s ability to shard (or split datasets into smaller pieces called “shards”) allows for highly efficient horizontal scaling across multiple machines or containers for improved performance. And clustering capabilities handle the distribution of data that is sharded across multiple nodes.

How does Redis Enterprise handle document storage, and what data structures are supported for document-oriented use cases? plus-white minus-white

Redis supports a variety of data types that make it suitable for document-oriented use cases. Primarily, Redis’s JSON and hash data types are suited for document-oriented database use cases as they allow data to be stored in a hierarchical, tree-like format. Redis also supports string, lists, sets, and sorted sets, which can also support document-oriented database use cases.

Next section  ►  TimeSeries databases