I want to update multiple fields on redis with RedisJSON feature, by using Jedis java client;
Suppose we have a key:value like below in redis:
PERSON_1:{name=ali2, phone=1234, address=Tehran, education={uniName=Amirkabir}}
then we want to update phone and address fields.
But it seems we can just update only one path or multiple paths with the same field name at a time.
Like:JSON.GET PERSON_1 $phone 9876
If we accept that we can only update one field at a time, than we have to hit the redis twice for two fields.
Is there any way to update multiple fields at a time to keep atomicity and have better performance?
I tried to use Jedis like:
client.jsonSet("PERSIN_1", Path.of("phone"), "9876");
and
client.jsonSet("PERSIN_1", Path.of("address"), "Shiraz");
In this way, we hit the Redis sever twice.
There is a JSON.MSET command in the works. Until then you have a few options.
If you just want atomicity, you can use MULTI and EXEC to do it all in a transaction. You'll want to WATCH the key first to make sure that no one else has changed it. Like this:
If someone changes
foobeforeEXECis called, you'll get an error and then can try again.You can also do these changes using pipelining to avoid multiple round trips. This just basically shoves all the commands down a socket without waiting for a response.
Jedis supports this but—full disclosure—I've never used pipelining from Jedis before. The docs make it look pretty straightforward though:
If you want to get extra fancy—and who doesn't wanna get extra fancy—you can combine these and use pipelining with transactions.
Hope this helps and good luck!