How can I optimize category data caching in a NestJS e-commerce app using Redis?

I’m building an online shop with NestJS, Prisma, and PostgreSQL. I need to cache category info globally for faster access. I’m thinking about using Redis and setting up a daily cache refresh at night.

Is this a good way to handle global category caching? Are there better methods for big e-commerce sites?

I’m worried that if one user updates the cache, it might affect others. So I made a central cache updater that runs daily. This way, everyone gets the same cached category data. Is this smart, or are there better ways to manage global caching in an online store?

Here’s a basic example of what I’m thinking:

class ProductManager {
  private cacheKey = 'product_categories';
  private cacheTTL = 86400; // 1 day

  async fetchCategories() {
    let categories = await this.redisClient.get(this.cacheKey);
    if (!categories) {
      categories = await this.dbClient.getCategories();
      // Don't set cache here, let the scheduler handle it
    }
    return categories;
  }

  async updateCacheDaily() {
    const freshCategories = await this.dbClient.getCategories();
    await this.redisClient.set(this.cacheKey, JSON.stringify(freshCategories), this.cacheTTL);
  }
}

// Scheduler
class CacheUpdater {
  @DailyAt('00:00')
  async refreshCategories() {
    await productManager.updateCacheDaily();
  }
}

Any thoughts on this setup?

Hey there Charlie31! Your caching strategy sounds pretty solid for a start. I’m curious though - have you considered any real-time update scenarios? Like, what if a new category gets added during peak hours?

Maybe we could tweak your approach a bit. What if we kept the daily refresh but also added a way to update the cache right after any category changes? That way, you’d get the best of both worlds - consistent updates plus immediate reflection of changes.

Also, I’ve been tinkering with cache invalidation lately. Ever thought about using cache versioning? It’s been a game-changer for me in managing cache consistency across different parts of my apps.

Oh, and one more thing - how are you handling cache misses? It might be worth looking into cache warming strategies to minimize those initial slow responses after a cache clear.

What do you think? Have you run into any specific challenges with your current setup?

hey charlie31, ur approach sounds solid! have u thought bout using a pub/sub system with redis? it could help with real-time updates when categories change. also, maybe implement a fallback mechanism for cache misses? that way, if the cache is empty, u can fetch from the db and update redis on the fly. just some ideas to consider for ur e-commerce setup!

Your approach is fundamentally sound, especially if the product categories do not change frequently. Using a daily cache refresh during off-peak hours can effectively improve performance. From personal experience, enhancing this setup with dynamic cache invalidation mechanisms can offer even better results over time. For example, incorporating cache versioning or warming techniques might prevent potential issues when data updates occur unexpectedly. Additionally, having a strategy to immediately update the cache for critical changes can minimize inconsistencies, and a reliable fallback to the database ensures system resiliency.