RedisJSON

7 questions found

What is RedisJSON, and how does it improve working with JSON data compared to storing it as a plain string?

Beginner
RedisJSON is a module that adds native support for storing, retrieving, and modifying JSON documents directly within Redis, letting you access and update specific fields deep within a document without needing to retrieve, parse, modify, and rewrite the entire JSON string, which is required when JSON is simply stored as a plain text value.
JSON.SET user:100 $ '{"name":"Sarah","age":29,"address":{"city":"Boston"}}'
JSON.GET user:100 $.address.city
Real-world example A user profile system stores complete user documents as JSON using RedisJSON, letting the application directly update just a user's city field without needing to retrieve and rewrite the entire profile document.

Common follow-ups: How much more efficient is this compared to storing JSON as a plain string?;What happens if you try to use JSON.SET on a key that already holds a non JSON value?

Data Types;Hashes in Redis

How do you set and retrieve a specific nested field within a JSON document using RedisJSON's path syntax?

Beginner
You use JSON.SET or JSON.GET along with a JSONPath expression that identifies the specific field you want to target, using a dollar sign to represent the document's root and dot notation to navigate into nested objects, letting you work with just the specific piece of data you actually need.
JSON.SET user:100 $.address.zipCode '"02108"'
JSON.GET user:100 $.address.zipCode
Real-world example An address update feature modifies just a user's zip code field directly within their stored JSON document, without touching any of the other unrelated fields in that same document.

Common follow-ups: What is the JSONPath syntax for accessing an item within an array?;What happens if the specified path does not exist within the document?

Data Types;Redis Modules Overview

How do you work with arrays within a JSON document stored in Redis, such as adding a new item to an existing array?

Intermediate
You use JSON.ARRAPPEND to add one or more new items to the end of an existing array at a specified path within your JSON document, letting you efficiently grow an array, such as a list of tags or order items, without needing to retrieve, modify, and rewrite the entire containing document.
JSON.ARRAPPEND product:55 $.tags '"electronics"'
Real-world example A product catalog system appends a new tag to a specific product's existing tags array directly within its stored JSON document, without needing to fetch and rewrite the entire product record just to add one tag.

Common follow-ups: What other array manipulation commands does RedisJSON provide besides append?;How do you remove a specific item from an array by its position?

Data Types;Lists in Redis

How can you combine RedisJSON with RedisSearch to enable searching across specific fields within stored JSON documents?

Intermediate
You create a search index using RedisSearch that specifically defines certain fields within your JSON documents as searchable, and RedisSearch then automatically indexes those fields as JSON documents are added or updated, letting you run rich search queries directly against structured data stored using RedisJSON.
FT.CREATE product_idx ON JSON PREFIX 1 product: SCHEMA $.name AS name TEXT $.price AS price NUMERIC
FT.SEARCH product_idx '@name:laptop'
Real-world example An e-commerce platform combines RedisJSON for storing detailed product documents with RedisSearch for building a fast, flexible product search feature across specific fields like name and price within those documents.

Common follow-ups: Does every field in a JSON document need to be indexed, or can you choose specific ones?;What happens to the search index when a JSON document is updated?

RedisSearch (Full Text Search);Redis Modules Overview

How would you design a document based data model using RedisJSON for an application that would traditionally use a document database like MongoDB?

Advanced
You would structure your data as nested JSON documents that closely match how your application naturally thinks about its entities, use RedisJSON's path based commands to efficiently update specific fields without rewriting entire documents, combine it with RedisSearch if you need rich querying capabilities across document fields, and take advantage of Redis's overall speed for read heavy access patterns.
JSON.SET order:1001 $ '{"customerId":123,"items":[{"product":"laptop","qty":1}],"status":"pending"}'
Real-world example A team building a fast order management system uses RedisJSON to store complete order documents with nested item arrays, taking advantage of Redis's speed for their read heavy dashboard while still working with a natural, document oriented data model.

Common follow-ups: What are the tradeoffs of using RedisJSON versus a dedicated document database for this kind of application?;How does RedisJSON handle very deeply nested or very large documents?

RedisSearch (Full Text Search);Data Types

What are the performance and memory considerations when storing very large or deeply nested JSON documents using RedisJSON?

Advanced
Very large JSON documents require more memory to store and can take longer to parse and serialize during read and write operations, and while RedisJSON's path based updates avoid needing to rewrite an entire document for small changes, extremely deep nesting or very large arrays within a document can still add noticeable overhead, making it worth considering whether some data would be better split across multiple smaller, related documents instead.
-- Consider splitting a very large document
-- into smaller, related documents if it grows too large
JSON.SET order:1001:items $ '[{...}, {...}]'
Real-world example A team notices performance degrading for a specific type of document that had grown to contain thousands of nested array items, and improves things by splitting that large array out into a separate, more manageably sized document.

Common follow-ups: At what document size does performance typically start to noticeably degrade?;How do you decide when to split a large document versus keeping it together?

Redis Memory Optimization;Data Types

How do you atomically increment a numeric value nested within a JSON document using RedisJSON?

Intermediate
You use the JSON.NUMINCRBY command, specifying the document key, the path to the specific numeric field, and the amount to increase it by, which atomically updates that specific nested value, making it safe even if multiple clients are trying to update the same field simultaneously.
JSON.NUMINCRBY product:55 $.stock -1
Real-world example An inventory system atomically decreases a product's stock count, which is nested within a larger JSON document, using JSON.NUMINCRBY, safely handling many simultaneous purchases without any risk of an incorrect final count.

Common follow-ups: What happens if the target field is not actually a numeric value?;Can JSON.NUMINCRBY target multiple matching fields at once using a wildcard path?

Data Types;Redis CLI & Basic Commands