0

I have a program that wants to read data from an API and compare that data with data stored in the database. if some conditions exist, then remove data from the database and send data to a function. Database and API are not important for me in this example but if you want to know, I'm working with "node-binance-api" and Redis database and Node.js.

REDIS_PORT = process.env.PORT || 6379;
redisClient = createClient(this.REDIS_PORT);
binance.futuresMiniTickerStream((miniTicker) => {
  miniTicker.forEach((_array) => {
    _array.forEach((selectedPair) => {
      redis.ZRANG(`binance-future-${selectedPair.symbol}`, (obj) => {
        obj.forEach(item => {
          item = JSON.parse(item);
          if (item.targetPrice > item.firstPrice && selectedPair.close >= item.targetPrice || item.targetPrice < item.firstPrice && selectedPair.close <= item.targetPrice) {
            redisClient.ZREMRANGEBYSCORE(`binance-future-${selectedPair.symbol}`, item.targetPrice, item.targetPrice, () => {
              console.log("Item Deleted");
            })
          }
        });
      });
    });
  });
});
   

how can I optimize this piece of code? also for some optimization operations, I wanna use RabbitMQ to distribute my code to some workers, I used RabbitMQ before in this project, but any idea, I'm happy to read.

Arsham Arya
  • 1,461
  • 3
  • 17
  • 25
  • 1
    You can considering using [redis-pipeline](https://redis.io/topics/pipelining) to combine multiple single redis operations. – spike 王建 Oct 11 '20 at 13:08
  • 1
    Before you starting using rabbitmq, you need to benchmark the program to find out if comparing with redis is bottleneck.Or the rabbitmq will not bring performance improvement, but increased complexity. – spike 王建 Oct 11 '20 at 13:13
  • 1
    Your additional RabbitMQ question sounds too broad on its own, and certainly shouldn't be tacked onto this question - the format of this site doesn't really work if you ask two questions on one page. – IMSoP Oct 11 '20 at 20:04
  • Please read about callback hell and use promise – Dipak May 12 '21 at 15:13
  • You are at Callback hell, some clean-code principles can help you. You can also separate your callbacks to different functions. –  May 12 '21 at 15:03

0 Answers0