I have a quick question to people who are more familiar with Redis internals and perhaps know the answer.
I use socket.io and have a map of sessionID - socket(s) stored. In addition to that I use SET command to store empty value to indicate user is connected (if no sockets are associated with sessionID). To account for page reloads the DEL command is executed with a timeout. This leads to situation where I have no active sockets, but the online status is still set in Redis.
This means if user use a single tab, every page reload means the SET command is executed. What I am wondering is whether SETNX would be more performant here? Basically, would the check performed by SETNX be faster than setting the value again with SET? Assuming that the value will exist in 99.99% of cases.
My guess is that it should be faster, but perhaps there's something I don't know. So could anyone confirm my guess or explain me why SET may still be faster?
Redis SETNX command is used to set some string value in Redis key, if the key does not exist in Redis. Fullform of SETNX is SET if Not eXists.
SETNX is short for "SET if Not eXists".
Generally speaking, SETNX
could be slightly faster as it will not set the value sometimes. However, in your use case, the differences in performance would probably be negligible given the size of value (empty string). Test it to be certain :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With