Skip to content

Pipeline-based script loading and retries - #1499

Merged
luin merged 4 commits into
redis:v5from
marcbachmann:pipelined-evalsha
Feb 20, 2022
Merged

luin merged 4 commits into
redis:v5from
marcbachmann:pipelined-evalsha

Conversation

@marcbachmann

@marcbachmann marcbachmann commented Feb 1, 2022 •

Copy link
Copy Markdown
Collaborator

This PR builds on #1497 (first three comments)
The last two commits are relevant for the new script load behavior.

Changelog

  • 🚀 Scripts are loaded into the pipeline instead in advance
    • It improves performance during first load
    • Improves cluster support with scripts
  • 🐛 Automatically retry failed evalsha commands if the script is missing in redis, this improves the resiliency
  • 🐛 Use the same script caching logic for standalone execution of scripts
  • 🐛 Only send the scripts to a socket exactly once

@marcbachmann marcbachmann changed the title Pipelined evalsha Pipeline-based script loading and retries Feb 1, 2022
Comment thread test/functional/scripting.ts Resolved
Comment thread lib/script.ts Outdated

// Standalone mode (regular)
function usingStandalone(script, redis, args, options, callback) {
if (redis._addedScriptHashes[script.sha]) {

@marcbachmann marcbachmann Feb 1, 2022 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing those decisions in here, we could move it into the command.toWritable function.
But we don't have any stream context there. https://github.com/luin/ioredis/blob/b8177479c348aa4bbd467fa944d61fe9b35aec19/lib/redis/index.ts#L790

If we would pass in the stream to the toWritable function, we could maintain the script cache per cluster node or socket instead on the redis instance.

@marcbachmann

marcbachmann commented Feb 3, 2022 •

Copy link
Copy Markdown
Collaborator Author

@luin what do you think about those changes to make the scripts more reliable?
The script implementation is now much more straight forward.
Just how a socket-like object like the pipeline gets passed to the redis instance could be better at the moment. Because of that the check is a bit more complicated during write.

But in general it completely eliminates all caching issues as we just load the script once with every new connection. Previously it resulted in quite a lot of NOSCRIPT errors. I think this drawback is good enough for most cases.
We could also only use eval when multi was used. To guarantee that the script is always there in that case. For the other cases it will do the fallback.

Comment thread lib/script.ts

// Resend the same custom evalsha command that gets transformed to an eval
// in case it's not loaded yet on the connectionDo an eval as fallback, redis will hash and load it
const resend = new this.Command(

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could remove this fallback if order of delivery needs to be guaranteed.
It will still clear the cache for the next command that gets sent to redis.
https://github.com/luin/ioredis/pull/1499/files#diff-3618aaced6e025d978d2cdde354e756687916385e9024868d0ad3fff887c55c6R24

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking to include this breaking change in the next major version #1504 so we can just retry custom commands in pipeline as well.

Comment thread lib/redis/index.ts Outdated

if (stream) {
if (stream.isPipeline)
stream.write(command.toWritable(stream.destination.redis.stream));

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the pipeline we could move the buffer logic into the Redis class itself.
Maybe even persist the pipeline itself in the queue instead of calling sendCommand that often.

@luin

luin commented Feb 12, 2022

Copy link
Copy Markdown
Collaborator

Hey @marcbachmann 👋,

Sorry for the delay. I'm planning v5 in #1504. So do you mind change the target branch of this PR to v5 and resolve the conflicts?

@marcbachmann
marcbachmann changed the base branch from master to v5 February 12, 2022 15:56
@marcbachmann

Copy link
Copy Markdown
Collaborator Author

@luin I've rebased this.
At least one cluster.disconnect test is missing as I've removed the script based ones:
https://github.com/luin/ioredis/pull/1499/files#diff-25757dbbc9670af28c244c85ae7b36c1224b1963223299f4df48963785141a6fL415

@luin
luin force-pushed the v5 branch 5 times, most recently from 99d9371 to 64a50dc Compare February 20, 2022 01:49
@luin
luin merged commit 332980f into redis:v5 Feb 20, 2022
@luin

luin commented Feb 20, 2022

Copy link
Copy Markdown
Collaborator

Awesome! Nice and clean improvements!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants