Coder Social home page Coder Social logo

yarnl's People

Contributors

gabegorelick avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

yarnl's Issues

Am I using it right?

First thing, I love your work. But I have a question as I'm new to Redis and how this actually works.

I'm cross posting from Stackoverflow.

I'm trying to use redlock for concurrent requests as I don't want the same request to fetch data from database multiple times. If the data is available then save it to Redis and it can be retrieved from Redis.

Here's my code

var AWS = require('aws-sdk');
var redis = require('redis');
var bluebird = require('bluebird');
var s3 = new AWS.S3();
var Lock = require('yarnl');
var request = require('request');

bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);

var client = redis.createClient({
  host: "localhost",
  port: 6379
});

module.exports = {
  getFromRedis: function(req, res) {
    client.getAsync('hash').then(function(data) {
      if(data) {
        console.log('fetch from cache');
        return res.send(data);
      } else {
        new Lock('url').lock(function (err, unlock) {
          if (err){
            throw err
          }

          if (!unlock) {
            console.log('first lock');
            return res.send('first lock');
          } else {
            console.log('fetch crawl');
            request('/fetch_data_from_db', function (err, response, body) {
              client.set('hash', body);
              unlock(function (err) {
                if(err) {
                  throw err
                }

                console.log('releasing lock');
                return res.send(body);
              });
            });
          }
        });
      }
    });
  }
}

I'm using this library to do the locking

https://github.com/gabegorelick/yarnl

And I test this by running ab -n 10 -c 5 http://localhost:8081/fetch

This is the result I get

fetch crawl
releasing lock
fetch from cache
fetch from cache
fetch from cache
fetch from cache
fetch crawl
releasing lock
fetch crawl
releasing lock
fetch crawl
releasing lock
...

What I get from that result is that the first request locks the process and the subsequent requests fetch from what's in Redis. However, there's 4-5 concurrent requests trying to get the lock and fetch the data from database. My question is is there anything I could do to reduce the concurrent requests to fetch the data to just one request. If the next request sees the lock then just return empty response until the data is ready in Redis.

Backoff

Replace maxAttempts and retryDelay with backoff. Lock can accept a backoff instance and call reset() after every .lock().

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.