1

I am using https://github.com/redis/go-redis/ package. I am trying to use GT option of zadd. I found a unit test in commands_test.go that uses ZAddArgs method to use the GT option:

// Test only the GT+LT options.
added, err := client.ZAddArgs(ctx, "zset", redis.ZAddArgs{
   GT:      true,
   Members: []redis.Z{{Score: 1, Member: "one"}},
}).Result()

When I apply the same strategy in my code, I get an error from Redis: syntax error without other explanations. Am I doing something wrong? This is my code:

score := 50
member := "Tester"
members := []redis.Z{{Score: score, Member: member}}
_, err := client.ZAddArgs(ctx, config.Key, redis.ZAddArgs{
    GT: true,
    Members: members,
}).Result()

if err != nil {
    panic(err.Error()) // results in "ERR syntax error"
}
Problem Solver
  • 385
  • 1
  • 4
  • 11

1 Answers1

1

Looks like it's an issue with Redis version. It has to be a minimum of 6.2.0. For anyone looking for a workaround:

  1. Use ZSCORE member to retrieve the current score
  2. Compare the existing score to a new score
  3. If the new score is higher, update
Problem Solver
  • 385
  • 1
  • 4
  • 11
  • Unlike the GT option this way is prone to race conditions. You can use lua script which does the above then it will be transactionanl also – Eldar Mar 16 '23 at 13:29
  • Good point Eldar. I guess it depends on what you're trying to accomplish. I'm not convinced that there will be a race condition between the same user unless this is a user/bot that submits information faster than your code can process the above 3 steps AND your user/bot always submits a higher score than the previous score. If you run into a situation like this, you might have a bigger issue to worry about. – Problem Solver Mar 17 '23 at 12:46