I am using the Jedis pipeline to add multiple records in Redis at once. But when I am debugging the code I can see the records appear in Redis even before calling jedis.sync()
method. Aren't all commands in the pipeline expected to be executed only after that? Or maybe it's just batching them on some chunks with a fixed size?
var pipeline = jedis.pipelined();
all.forEach(value -> pipeline.sadd(allPrefix, value));
grouped.forEach((key, value) -> pipeline.hset(groupedPrefix, String.valueOf(key), value));
pipeline.sync();
Am I doing it the right way and what is the reason for this behavior?
Jedis pipeline writes the commands in the socket buffer. If the size of all commands exceeds the size of that buffer, it is flushed to make space for more commands. In the mean time, those flushed commands reach to Redis server and the server may start processing those commands; even though pipeline.sync()
is not called.
pipeline.sync()
ensures that all commands would be sent to server. It does not ensure all commands would be kept in buffer until sync()
is called.
If you want something where none of your commands would be executed before a trigger, consider (any variant of) Transaction. All the commands in a transaction gets executed only after exec()
is called.