0

The situation might be a bit complex, let's say I have a shopping cart related to items (items has some other relations with other entities) in a relational DB, it does not have any problem of query the shopping cart with all the other related entities by specifying them.

Now I am trying to move the shopping cart to Redis for high performance, if the shopping cart is created, with the items and the other relations will be saved to Redis.

    await client.hSet(`shopping-cart:id:${cartId}`, {shoppingCartObj});
shoppingCartObj:
{
  cartId: string,
  items: [
     item1: {
        maker:{},
     },
     item2: {
        maker:{},
     },
     item3: {
        maker:{},
     },
  ]
}

My question is the items and the items other related entities are still in DBMS like Postgres. They are still editable for users, like the maker. If the shopping cart is read from Redis always, they are out dated. Any strategy to maintain?

I can think of only maintain the shopping cart and the item relation in Redis with id links like cartId with itemId, if needed query again with the itemId.

Or load the items in the Redis as a copy, but will need to update if the user edited it.

Any other suggestions?

Chun Young
  • 83
  • 6
  • Maintaining links from entities to cached aggregates (and probably back, you might need bi-directional links to properly handle cases where the user removes things from a cart) is reinventing the wheel. You will be maintaining referential integrity on the app level, just like RDBMS do but in a less robust way. So the 1st question is _why_ do you need to cache the whole shopping cart? Isn't it a fairly simple aggregate that need just several trivial joins (something DB is good at usually)? What makes its performance so bad in your case - did you find the _real_ bottleneck(s) first? – Konstantin Strukov Nov 24 '22 at 11:11
  • Hi @KonstantinStrukov, thanks so much for your insight. For the reason why we are migrating: reading the shopping cart (maybe some other case) is very frequent operation, and now it is a bottle neck of our app, definitely wanna move it to Redis. The question may not only specific to shopping cart. I have the mindset of RDBMS, everything comes so nature to me. But very new to Redis, not quite sure how we manage the relations. – Chun Young Nov 30 '22 at 03:51

0 Answers0