Repository navigation
Description
SET and variants
SET is one of the most used Redis commands, and has many variants:
SETNX-- Set if not existsSETEX-- Set with expirePSETEX-- Set with milliseconds expire
There is demand for additional variants such as SETEXNX, becuase it is a pretty common pattern in caching and sessions handling to set an object only if not already inside the cache, and setting it with an expire. This is also useful for poor's man locking, and so forth.
There is the argument that this need can be addressed with scripting, however probably this is enough common that a very high performance solution is expected, and also we can exploit the need in order to make our SET story more clear for the users.
Trivia: the reason currently we have this mess of commands is because of an early limitation of the Redis protocol in the early stages. The binary-safe argument was only possible as last argument, so it was not easy to extend already existing commands, and I kept introducing new ones. Finally the protocol was fixed and now our hands are free.
Proposal
This proposal extends the SET command to cover all the already existing variants in form of options, so that later all the SET variants will be deprecated.
At the same time the proposal adds a new functionality in form of an option that only executes the SET command if the key already exists.
The new form of the command will be:
SET key value option option option ...
The available options will be:
- nx -- Execute the SET only if the key does not already exists
- xx -- Execute the SET only if the key already exists
- ex -- Set an expire if the SET will be executed.
- px -- Set a milliseconds expire if the SET will be executed.
Examples:
SET foo bar xx # Set foo to bar only if key foo already exists.
SET foo value ex 1000 nx # Set foo to value with an expire of 1000 seconds, if not exists.
SET foo bar ex 10 # Same as SETEX foo 10 bar
The new command will allow more variants in the future if needed and is simple to remember. The option names were chosen to be short to write and easy to remember just remembering the first character:
- Non existing
- eXisting
- Eexpire
- Pprecision expire
Feedbacks welcomed.
Activity
mgravell commentedon Feb 4, 2013
I think it would be a positive change, and would make taking locks a breeze - just:
My concern, however, would be the return value and the impact on older clients;
sethas historically returned+ok; however now it would presumably be taking on the return signature ofsetnx, i.e.:0or:1. This could negatively impact older clients that are checking for a+okresponse, with false-negative failures when using regularset.Unless, of course, the intent is to return
+okifnx/xxare not used, and:0/:1ifnx/xxare used.Thoughts?
antirez commentedon Feb 4, 2013
Good point Marc, my reasoning was to always return just :0 / :1 actually because in the vanilla form
SETcan't fail, so probably nobody checks the return value, but yep if there is an explicit check, this will break existing clients. There is to evaluate if this is the case or not.The backward compatible way of returning
OKwith no options works for sure, but given that we are going for fixing it, maybe it's better to try hard to return a consistent return value. However there is also an alternative of returning "+OK" if the command works as expected and "+NOOP" or alike in case nx or xx prevented the command from being executed. In that case we have a single return value, it is backward compatible, but it introduces something new to know for the users.Ok there is to think a bit about this :-) thanks for the hint!
melo commentedon Feb 4, 2013
@mgravell I don't see the return value as an issue.
If you plan to use the new command, you will probably check the client library to see if already supports it or not, and update it if not.
As long as the old SET version in the new code returns the same old +OK return code, old code (code that uses an old client lib) will only use the current syntax, and will keep returning the current response code.
@antirez Do we need EX and PX? Why not always precision expire? Also, why the short codes (EX instead of EXPIRE)?
mgravell commentedon Feb 4, 2013
@melo it is the same command; the proposal (see also antirez's comment) would potentially change the result of the existing
setcommand; there is no "new command" here. Old clients, continuing to use the command that they always have, but against a new server version, may suddenly get a different response.antirez commentedon Feb 4, 2013
@melo unfortunately the new version would NOT return OK even in the old form... But :1
About ex/px, given that we have options, it is probably not too bad to also allow people to use seconds that is what 99% of people do with expires, so my reason is to add two options solely based on the frequency of usage.
About the short options, two reasons:
About point 2, because the initial character is different for every option so far, checking this will be very very fast just with a switch or alike for the first char, and only later we'll check the rest of the string, that must just be 'x' + nullterm.
mgravell commentedon Feb 4, 2013
@antirez "
setcan't fail" is incorrect; it might be true in the "happy path", but it can fail in many other ways:What I have not done is look at all the available client-library implementations to see which disregard the result completely; which just check for
-message(for error conditions), and which positively check for+ok.I've checked my own library, and I have an
ExpectOk()there; obviously that is totally trivial for me to fix; my point there is : it is not beyond reason that other clients have their equivalent.antirez commentedon Feb 4, 2013
True but in that case a -ERR exception will be raised exactly like in the new SET case, that's why I don't think people check for the 'OK' string itself but for exceptions (errors).
mgravell commentedon Feb 4, 2013
I chose another library at random and looked at the "set", and indeed in that library it just checked for
-messagefailures. I guess it comes down to the confidence level of libraries being overly conservative in what they accept.melo commentedon Feb 4, 2013
I may be missing something obvious, but I see no reason that the new code must return a different result from the one we have today if the simple SET k v format is used.
Only if options are used does a new return code makes sense, IMVHO.
antirez commentedon Feb 4, 2013
@mgravell yep I think we'll break almost nothing at all in the practice, and anyway this is for 2.8 so we'll make sure to put a big warning in the release notes :-)
@melo I want to have a single return value because often people do something like that:
Basically in complex code, often inside library clients implementation, you don't know the form it is used, and to check explicitly if the form is a "pure" one or not will make the check more complex.
dragonsinth commentedon Feb 4, 2013
This seems awesome, love the proposal. Don't have any brilliant suggestions on the return value tho.
antirez commentedon Feb 4, 2013
Dudes, quick poll on Twitter revealed with this change we'll break a lot of code.
Currently the best option we have as an exit strategy is +OK on success, +NOOP on no op performed.
But +NOOP kind of sucks, a better more clear to understand string would be much better.
antirez commentedon Feb 5, 2013
I reconsidered the return value issue, basically OK / NOOP will not help us further, and will create an asymmetry as all the other commands use the :1 / :0 pattern.
The reason why it does not help is that in the initial time after 2.8 introduction clients will not be always updated, so people will check for explicit NOOP returned by the command.
Later probably most clients will update to return true/false to allow for the
if (redis.set(...))pattern, and this will break the code checking for NOOP.So probably to return :0 and :1 is our best bet, with information about the change.
Similarly I would like to rename HMSET to HSET (variadic version by default) and deprecate HMSET.
So practically speaking, what this will break?
OKreturned by set.OKIn some way or the other we should try to move forward and fix the API by providing changes and deprecating the old way of doing things. There isn't a lot to fix in the Redis API, but this time I don't think there are better options.
e-oz commentedon Feb 5, 2013
In languages with dynamic typing it doesn't matter, will this function return +OK or 1.
if (redis.set(...will be possible and correct anyway. But not with +NOOP. Dynamic typed languages are widely used in web-dev, so I guess it will not be a big problem.For compatibility it will be easy to create wrappers for old variants of
SETin clients. Because client library can't afford to declare support only one (last) version of Redis :)14 remaining items
Extended SET command implemented (issue #931).
antirez commentedon Apr 2, 2013
Implemented, back ported into 2.6, 2.6.12 released, and documented at redis.io. Closing :-)
Thank you for all your feedbacks!
mgravell commentedon Apr 3, 2013
The locking guidance on http://redis.io/commands/setnx should probably be updated with this in mind. The most obvious lock now is simply:
with the check: if you get
+OK, you got the lock; if you get*0, you didn't.Edit: I see this is mentioned in "set", but IMO the "setnx" page should strongly suggest using the "set" version, only using the "setnx" implementation for down-level servers.
antirez commentedon Apr 3, 2013
@mgravell agreed, doing it right now. I'm also writing the small Lua script required to safely unlock for reference so that I can add it into the
SETpage. Thanks.Implement extended set options added in Redis 2.6.12.
Extended SET command implemented (issue redis#931).
Squashed 'deps/hiredis/' changes from f8de9a4bd..b6a052fe0
Update README.md file to reference valkey.io (redis#931)
Merge pull request redis#931 from pps83/dev-win32-sheck
Limit Vector Sets JSON Parser Allowed Recursion Depth (redis#931)
Limit Vector Sets JSON Parser Allowed Recursion Depth (redis#931)
Limit Vector Sets JSON Parser Allowed Recursion Depth (redis#931)
Limit Vector Sets JSON Parser Allowed Recursion Depth (redis#931)
Limit Vector Sets JSON Parser Allowed Recursion Depth (redis#931)