The key you are talking about using is actually not so long.
The sample key that you specify for the set sets the O (1) search methods. More complex set operations (SDIFF, SUNION, SINTER) - O (N). Most likely, filling $userId was more expensive than using a longer key.
Redis comes with a test utility called redis-benchmark , if you change the GET test to src / redis-benchmark.c so that they are just "foo", you can run the test with short keys after make install :
diff --git a/src/redis-benchmark.cb/src/redis-benchmark.c --- a/src/redis-benchmark.c +++ b/src/redis-benchmark.c @@ -475,11 +475,11 @@ benchmark("MSET (10 keys)",cmd,len); free(cmd); - len = redisFormatCommand(&cmd,"SET foo:rand:000000000000 %s",data); + len = redisFormatCommand(&cmd,"SET foo %s",data); benchmark("SET",cmd,len); free(cmd); - len = redisFormatCommand(&cmd,"GET foo:rand:000000000000"); + len = redisFormatCommand(&cmd,"GET foo"); benchmark("GET",cmd,len); free(cmd);
Here's the GET test speed for the 3 subsequent runs of the foo shortcut:
59880.24 requests per second 58139.53 requests per second 58479.53 requests per second
Here's the speed of the GET test after changing the source again and changing the key to "set-allBooksBelongToUser: 1234567890":
60240.96 requests per second 60606.06 requests per second 58479.53 requests per second
Changing the key once again to the "ipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumloreipsumlorem: 1234567890" gives the following:
58479.53 requests per second 58139.53 requests per second 56179.77 requests per second
Thus, even really very long keys do not have much effect on redis speed. And this is on GET, operation O (1). More complex operations will be even less sensitive to this.
I think that having keys that clearly define what values โโthey have significantly outweighs any insignificant speed that you choose from shortened keys.
If you want to take this additionally, there is also the -r [keyspacelen] parameter in the redis-benchmark utility that allows you to create random keys (if they have ": rand:"), you can simply increase the size of the prefix in the test code to any desired length .