Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis SET vs SETNX performance

Tags:

node.js

redis

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?

like image 407
pzaj Avatar asked May 12 '17 11:05

pzaj


People also ask

What is Setnx in Redis?

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.

What is set NX in Redis?

SETNX is short for "SET if Not eXists".


1 Answers

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 :)

like image 63
Itamar Haber Avatar answered Oct 04 '22 13:10

Itamar Haber