简体   繁体   中英

Node.js + Redis multiple lookups

I'm new to key value stores, but I'd like to learn. As a personal project, I'm trying to build an inventory management system with Node.js and Redis. Let's assume this is the correct technology to use for the moment.

If I had a simple system, that needs to track the number of widgets at a particular location, with the ability to look up details by widget or by location, my understanding according to https://matt.sh/thinking-in-redis-part-one is to store separate "custom indexes" to look up by location and by item.

In node.js to save a new entry then, we would create the entry with hmset, add the entry to the 2 indexes with sadd:

redis.hmset([
    key,
    'attr1', entry.attr1,
    'attr2', entry.attr2,
    'attr3', entry.attr3,
  ],
  function(err) {        
    // add entry to location set
    var locationKey = 'location:' + entry.location;
    redis.sadd(locationKey, key, function(err) {
      redis.expire(locationKey, 900);
    });

    // add entry to widget set
    var widgetKey = 'widget:' + widget.id;
    redis.sadd(widgetKey, key, function(err) {
      redis.expire(widgetKey, 900);
    });

    redis.expire(key, 900);
  }
);

Now if we wanted to move all widgets from one location to another, we'd need to get all entries from the widget set, add the entry to the new location index, and remove it from the old index:

// move all widgets to another location
redis.smembers('widget:' + widget.id, function(err, entryKeys) {
  entryKeys.forEach(function(entryKey) {
    // get entry for rebroadcast
    redis.hgetall(entryKey, function(err, result) {
      if (result) {
        // add entry to new location's index
        var locationKey = 'location:' + location;
        redis.sadd(locationKey, entryKey, function(err) {
          redis.expire(locationKey, 900);
        });
        // remove entry from old location's index
        redis.srem('location:' + result.location, entryKey);
      }
    });
  });
});

My concern is the number of requests that need to be made for each command. Adding an entry, will cost 3 inserts for the data itself, and 3 more assuming we want to expire the data. Moving all widgets will require 1+n inserts, n reads, and n deletes.

If this were for a real time game with hundreds or thousands of requests a second, is it ok for each command to require this many calls? Is this normal for a redis implementation?

Yes.

Redis is that fast. But do a benchmark on your machine, or similar production machine, that will run redis. It is included in redis itself. . (Post it back here, I'd be interested as well.)

Redis has a lot of commands at its disposal, and your data organization might allow cheaper calls, or calls-less-often. That will depend how you lay out the data model. There is not really a "query language" like SQL that can do lots of stuff in the query or combine queries into a single one. You are meant to hit redis a lot, which is a different philosophy than SQL (to some extent).

This personal project will allow you to see what works and what could be made better, so kudos on the effort. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM