gabegorelick / yarnl Goto Github PK
View Code? Open in Web Editor NEWYet Another Redis NodeJS Lock
License: MIT License
Yet Another Redis NodeJS Lock
License: MIT License
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.
Replace maxAttempts
and retryDelay
with backoff
. Lock can accept a backoff instance and call reset()
after every .lock()
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.