skip to Main Content

Let’s suppose that we have simplified online store with following entities: products, related products and categories. Relations are pretty simple:

  • Products are linked to categories
  • Products have related products
  • Products prices are region specific

All entities are stored as json strings in Redis, something like:

Products (and related products): {"productId":1, "title": "Product", "relatedProductIds":[4,5,6], "categoryIds":[1,2,3]}

Product prices: {"price": 555.5, "region": "eu"}

Category: {"categoryId": 12, "title": "Category"}

Our task is to fetch whole tree from redis for some product ids: products, products categories, products prices, product related products, categories of related products, related products prices. Doing this from client is okay, but requires multiple requests to redis. Whole procedure could be described in this way:

  • Fetch all required products and prices and json decode them. Now we know list of primary products categories and related product ids.
  • Fetch categories, related products and prices of related products and json decode them. Now we know list of related products categories.
  • Fetch related products categories.

It works and it works really fast, but could this be done using redis lua scripting? I’m planning to do this job via lua, combine one big json (or multiple small jsons) and return to client. Is it worth doing and why?

Thank you and sorry for my english.

2

Answers


  1. Yes, Lua script should probably get you much better latency than couple of hopes.
    Especially if you don’t need to retrieve all the fields, this way you should reduce data load on network and return only the relevant fields.

    BTW, you might want to check https://redisgears.io and https://redisjson.io in case you have more complex use cases, that might require supporting Redis Cluster or more complex JSONs.

    Login or Signup to reply.
  2. Lua script performance depends on what you did inside script. As Redis uses single thread command executor, if your script execute for a long time, it will block other commands and make Redis slower for response.

    For your case I would recommend to query the different data directly from Redis and handle their relations in your application. In this way, Redis is only used for data storage not for logic, and make it pure and faster.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search