3

I have a telegram bot that alerts whenever a price of certain asset reach a certain price.

I have the following table in a DB:

  • AlertId
  • ChatId
  • Price
  • Asset

So each Asset will have several ChatId (users) and each of them can have several AlertId, and AlertID is a unique autonumeric value.

As this data is constantly read, I decided to cache it using Redis.

I ended up with the following Redis design, but I'm not really confortable with it, I feel there's a better way to design it.

I made 2 sets:

Asset:{asset}- return lists of ChatId that have alerts on that Asset

ChatId:{chatId} - return lists of AlertId

and the following key that contains the price

Alerts:{alertId} - returns JSON with price that will trigger the alarm.

To send the alert via Telegram I need the chatId, the asset and the price, so for each price update I make the following algorithm:

  • check if there's any item in Asset:{asset}
  • enter a for loop with previous values for ChatId
  • loop again for each ChatId and check if the condition for the price is reached
  • alert accordingly
  • delete the record from db and from Redis

Is this design ok? or is there a better feature from Redis that I'm missing? The whole "set inside a set" makes some noise inside my head, but maybe this is the way it is suppoused to be used. This is my first time designing a solution using Redis.

Thank you very much,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.