Dragonfly Cloud is now available in the AWS Marketplace - learn more

Native JSON in Dragonfly: Simplified Document Storage

Discover how Dragonfly's native JSON support simplifies document storage, eliminates module dependencies, and unlocks powerful use cases like structured data access and real-time configuration manipulation.

November 27, 2024

Native JSON in Dragonfly: Simplified Document Storage

Introduction

What does your backend API return? It's highly likely JSON. As a lightweight, human-readable format, JSON (JavaScript Object Notation) has become one of the most popular standards for data exchange between systems. From REST APIs to configuration files, JSON is everywhere.

But JSON isn't just for transmission—it's also widely used for data storage, with many modern databases supporting it as a native data type (or column type). This allows you to store and query JSON documents directly, making it an essential tool for managing complex, hierarchical data structures in applications.

JSON as String vs. JSON as a Native Data Type

When we store JSON documents in a data store, we normally have two options. The first is to store them as strings (or byte arrays, or blobs), which is the traditional approach. By this way, we basically keep JSON documents as unstructured data, which is convenient for storage but comes with some limitations. Most notably, we can't query, index, or manipulate the JSON data directly, and we have to parse the strings in our application code to access the data. If the use case is a simple key/value-based caching layer, this approach could be sufficient since we always access the data by the key.

The second option is to store JSON documents as a native data type, which is more powerful and flexible. By doing so, the data store can understand the structure of the JSON documents and provide advanced query capabilities, such as indexing, filtering, and aggregation. This approach is particularly useful when the JSON data is complex and hierarchical, and we need to perform complex operations on it. With native JSON support, your data store becomes a powerful NoSQL document store, seamlessly combining flexibility and performance. As you'll see in this blog post, Dragonfly brings this capability to the forefront with native JSON support built-in.


Dragonfly's Native JSON Support

When it comes to working with JSON, Redis and Dragonfly take fundamentally different approaches. Redis supports the JSON data type and its related operations through the RedisJSON module, which allows us to store, query, and manipulate JSON data. However, there's a catch:

  • No Native Integration: RedisJSON is not part of the Redis core engine. We must install and manage the module separately, adding complexity to the setup and maintenance process.
  • Licensing Considerations: RedisJSON may come with specific licensing restrictions, which makes it less accessible on certain cloud platforms or environments.

While RedisJSON provides powerful functionality, the need for additional configuration and its separation from the core engine can create friction for users seeking simplicity.

Dragonfly, on the other hand, offers native JSON support baked directly into the core engine. Here's what sets Dragonfly apart:

  • No Modules, No Hassle: JSON functionality is part of Dragonfly's core, eliminating the need to manage external modules or deal with compatibility issues.
  • Included in Dragonfly Cloud: For users of Dragonfly Cloud, JSON support is bundled without extra costs or licensing fees, making it an attractive and cost-effective option. And since Dragonfly Cloud is available on all major cloud platforms, you can leverage native JSON support without restrictions.

Dragonfly's native support not only simplifies setup but also ensures high performance for JSON operations. For users familiar with Valkey, the Redis fork due to Redis license changes earlier this year, it's worth noting that it currently lacks JSON-related commands at the time of writing. Dragonfly bridges this gap, providing an easy way to work with JSON data natively.


Use Cases and Considerations

The native JSON support in Dragonfly unlocks powerful capabilities for a variety of use cases. Let's explore some example use cases when it makes sense to leverage the JSON data type and where simpler alternatives like strings might suffice.

1. Simple Caching

Even if your payload is JSON, using the string data type might be the better choice for straightforward caching scenarios. For example, consider caching user profile data:

{
  "id": 123,
  "firstName": "Joe",
  "lastName": "Zhou",
  "email": "joe.zhou@example.com"
}

If your application frequently retrieves the entire profile without needing to update individual fields, storing it as a string is simpler and requires no additional commands to parse or update the data. Dragonfly's flexibility lets you choose the most efficient approach for your workload.

# Cache user profile as a string in Dragonfly.
dragonfly$> SET user:123 '{"id":123,"firstName":"Joe","lastName":"Zhou","email":"joe.zhou@example.com"}'
OK

dragonfly$> GET user:123
"{\"id\":123,\"firstName\":\"Joe\",\"lastName\":\"Zhou\",\"email\":\"joe.zhou@example.com\"}"

2. Reading and Updating Structured Data

For more dynamic use cases, the JSON data type excels. Imagine you're managing a set of configurations in Dragonfly for individual users, each with different settings:

{
  "theme": "dark",
  "notifications": {
    "email": true,
    "push": true,
    "sms": false
  }
}

With native JSON support, you can organize and nest fields as needed, then retrieve specific values using their JSON path. For example, if you want to check if push notification is enabled for a specific user:

# Store user settings as a JSON document in Dragonfly.
dragonfly$> JSON.SET user:123:settings . '{"theme":"dark","notifications":{"email":true,"push":true,"sms":false}}'
OK

# Retrieve the value of the 'notifications.push' field using JSON path for the user.
dragonfly$> JSON.GET user:123:settings .notifications.push
"true"

Similarly, you can update individual fields within the JSON document without needing to replace the entire string:

# Retrieve the value of the 'notifications.sms' field using JSON path.
dragonfly$> JSON.GET user:123:settings .notifications.sms
"false"

# Update the value of the 'notifications.sms' field using JSON path.
dragonfly$> JSON.SET user:123:settings .notifications.sms true
OK

3. Manipulating Frequently Updated Fields

When your application frequently modifies specific fields within a JSON document, Dragonfly's native JSON commands eliminate unnecessary overhead. Instead of retrieving a JSON string, deserializing it, updating the value, serializing it back, and saving it into the data store, you can directly manipulate the data.

For instance, in a social media app, imagine many users liking a post at the same time. The JSON representation of the post might look like this:

{
  "id": 12345,
  "title": "Native JSON in Dragonfly",
  "content": "Check out our latest blog post!",
  "likes": 42
}

To increment the likes count, you can use the JSON.NUMINCRBY command:

# Store social media post data as a JSON document in Dragonfly.
dragonfly$> JSON.SET post:12345 . '{"id":12345,"title":"Native JSON in Dragonfly","content":"Check out our latest blog post!","likes":42}'

# Increment the 'likes' field by 1 using JSON path.
dragonfly$> JSON.NUMINCRBY post:12345 .likes 1
"43"

This direct operation is efficient and keeps your application logic simple, allowing Dragonfly to handle the heavy lifting.

4. Combining JSON with Search and Indexing

While we've highlighted a few key use cases, the possibilities with Dragonfly go far beyond these examples. Dragonfly not only supports native JSON but also incorporates features from other popular Redis modules, such as Dragonfly Search and Bloom filters.

By combining JSON with search capabilities, Dragonfly can serve as a comprehensive in-memory data store, allowing you to store JSON documents and perform powerful, real-time search queries on your data—all operations natively supported. This versatility makes Dragonfly an exceptional choice for modern, performance-driven applications.


Conclusion

Dragonfly's native JSON support transforms it into a powerful NoSQL document store, enabling you to handle complex, dynamic data structures with ease. Whether you're managing simple caches, accessing structured data, or manipulating JSON fields directly, Dragonfly offers the simplicity, flexibility, and performance you need to streamline your workflows—all without the hassle of external modules or additional costs. Ready to experience it? Try Dragonfly locally or consider Dragonfly Cloud for a fully managed solution.

Stay up to date on all things Dragonfly

Join our community for unparalleled support and insights

Join

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost