Course contentsShow
← Node.js
Lesson 1360 of 1,98012. Caching Strategies and Application SpeedFree lesson

Efficient Cache Storage of JSON Data

You will choose between structured and flat storage formats for optimal retrieval speed.

Efficient Cache Storage of JSON Data

In this lesson, you will learn how to choose between structured and flat storage formats to optimize the retrieval speed of your cached JSON objects.

When caching data in Node.js (often using Redis), developers frequently store entire complex JSON objects as single strings. While convenient, this forces the application to deserialize the entire object even if you only need one specific property. As your cache grows, this creates unnecessary CPU overhead.

Structured vs. Flat Storage

Structured Storage involves keeping the JSON nested as it exists in your application logic. You store the entire object under a single key. This is ideal when your application almost always reads the full document at once, as it minimizes the number of round-trips to the cache.

Flat Storage (often using Hashes in Redis) decomposes the JSON into individual key-value pairs at the top level or within a hash map. For example, instead of storing user:101 = '{"name": "Alice", "role": "admin", "lastLogin": "..."}', you store it as a hash:

// Using HSET to store flattened data
await redis.hset('user:101', {
  name: 'Alice',
  role: 'admin',
  lastLogin: '2023-10-27'
});

// Retrieving only the specific field needed
const role = await redis.hget('user:101', 'role');

Which should you choose?

  • Use Structured Storage when the data is small, cohesive, and consistently accessed in its entirety. It is simpler to manage and requires only one GET request.
  • Use Flat Storage when your objects are large or when your application frequently performs "partial reads." By flattening the data, you avoid the cost of deserializing large JSON blobs and allow the cache engine to return only the specific field requested. This significantly reduces memory throughput and CPU cycles on your Node.js event loop during high-concurrency periods.

If you find that your service is spending significant time running JSON.parse() on large cached objects just to check one boolean flag, it is time to pivot to a flatter storage strategy.

Key Takeaway: Choose structured storage for atomic retrieval of complete objects, and transition to flat, field-level storage to avoid unnecessary deserialization costs when accessing subsets of large data sets.