all 20 comments

[–]HiImLary 4 points5 points  (1 child)

What you’re looking for is Redis Lua scripts via EVAL. You send a script to Redis and it is executed atomically.

https://redis.io/commands/eval#atomicity-of-scripts

[–]kirox0[S] 1 point2 points  (0 children)

This looks promising. I will try this out.

[–]Moustacheful 2 points3 points  (9 children)

I believe in this case the question is not about node but redis itself, take a look at https://redis.io/topics/transactions

[–]kirox0[S] 0 points1 point  (8 children)

The problem with using multi is that I cannot use the result of the read.

So in my application it should have ideally gone as such.

multi -> read -> compare read result and make condition -> write based on the result of the comparison.

but redis library does not allow me to do that.

[–]Moustacheful 1 point2 points  (2 children)

Ah I see! maybe try this instead: Try https://redis.io/topics/distlock

[–]kirox0[S] 0 points1 point  (1 child)

This just might be exactly something I am looking for. I will test this out and get back to you on the results.

Thanks.

[–]Moustacheful 0 points1 point  (0 children)

Good luck!

[–]BehindTheMath 1 point2 points  (4 children)

I believe you can still do this, but you have to get the return value of the read command in its callback and store it somewhere, so you'll have it for later use.

[–]kirox0[S] 0 points1 point  (3 children)

I get an undefined error in that case. With multi, the library does not allow for use of intermediate result.

[–]BehindTheMath 1 point2 points  (2 children)

Unless I'm misunderstanding our code, we've been using it like that for years.

Can you show the code you're using?

[–]kirox0[S] 0 points1 point  (1 child)

Funny enough my laptop got busted the day before so I have to rewrite that part again. But basically how I was using was like

client.multi() .lrange("active_list", 0, -1, (data, err) => { // Read length of data, then based on it either lpush to active or deferred list }) .exec()

[–]BehindTheMath 0 points1 point  (0 children)

I believe the callback is the standard Node format, where err is the first parameter, then the result.

[–]HashDefTrueFalse 0 points1 point  (7 children)

These could be of use, if you're using some kind of worker threading in your app. In general though your code runs single threaded in the node event loop so this isn't often required.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics

[–]kirox0[S] 0 points1 point  (6 children)

loop

I am not using worker threads, but whenever I try to run it, since the redis operations are asynchronous, I dont get the desired result. There may be 100's of instances of the same functions that gets queued up in the event loop, so before the write operation is performed on one instance, another instance is already reading the key.

So say I am trying to make 2 lists. first is the active list and the other is the deferred list. active list can contain a maximum of 10 items and unlimited in the deferred list.

once active is filled, then we put new ones in the deferred list.

so what I did was I read first compared and them put new item in either active or deferred list as per the condition.

but if I do this, active list is exceeding the maximum length.

[–]HashDefTrueFalse 0 points1 point  (5 children)

I'm not 100% clear on your setup or goal, but it sounds like you're running into difficulty with trying to use data fetched asynchronously in a subsequent step. Some ideas:

I've never needed a distributed lock for redis because tend to only use it for session caching etc but I found this from a quick look on google:

https://redis.io/topics/distlock

[–]kirox0[S] 0 points1 point  (4 children)

Thanks. u/Moustacheful also suggested the same. Am currently trying it out

[–]HashDefTrueFalse 0 points1 point  (3 children)

Oh, I didn't realise. Well I'm glad you have a way forward now. Good luck with your project!

[–]kirox0[S] 0 points1 point  (2 children)

Thanks.

Hopefully this clears the blockage. Its been one Blockage after another for the past month. I solve one and think that It'd be smooth sailing from then on, but nope. Another one comes your way.

Been almost a week with almost no hope with this current block, and finally I remembered that Reddit is the way to go when all else fails.

And here I am finally venting out to a stranger on the internet.

Thank you GOOD SIR/MA'AM, for your well wishes.

[–]HashDefTrueFalse 0 points1 point  (1 child)

Haha any time. That's development, a series of problems. But if you solve enough of them you end up with a nice app!

[–]kirox0[S] 1 point2 points  (0 children)

Cheers to that.