Should cache keys be hashed?

3.4k Views Asked by At

I am working on an existing system that using NCache. it is a distributed system with large caching requirements, so there is no question that caching is the correct answer, but...

For some reason, in the existing code, all cache keys are hashed before storing in the cache.

My argument is that we should NOT hash the key, as the caching library may have some super optimized way of storing it's dictionary and hashing everything means we may actually be slowing down lookups if we do this.

The guy who originally wrote the code has left, and the knowledge of why the keys are cached has been lost.

Can anyone suggest if hashing is the correct thing to do, or should it be removed.

2

There are 2 best solutions below

1
On BEST ANSWER

Okay so your question is

  1. Should we hash the keys before storing?
  2. If you yourself do hashing, will it slow down anything

Well, the cache API works on strings as keys. In the background NCache automatically generates hashes against these keys which help it to identify where the object should be stored. And by where I mean in which node.

When you say that your application Hashes keys before handing it over to NCahe, then it is simple an unnecessary step. NCache API was meant to take this headache from you.

BUT if those hashes were generated because of some internal Logic within your application then that's another case. Please check carefully.

Needless to say, if you're doing something again and again then it will definitely have a performance degradation. The Hash strings that you provide will be used again to generate another hash value (int).

6
On

Whether you should or shouldn't hash keys depends on your system requirements.

NCache identifies object by it's key, and considers objects with equal keys to be equal. Below is a definition of a hash function from Wikipedia:

A hash function is any function that can be used to map data of arbitrary size to data of fixed size.

If you stop hash keys, then cache may behave differently. For example, some objects that NCache considered equal, now NCache may consider not equal. And instead of one cache entry you will get two.

NCache doesn't require you to hash keys. NCache key is just a string that is unique for each object. Relevant excerpt from NCache 4.6 Programmer’s Guide:

NCache uses a “key” and “value” structure for objects. Every object must have a unique string key associated with it. Every key has an atomic occurrence in the cache whether it is local or clustered. Cached keys are case sensitive in nature, and if you try to add another key with same value, an OperationFailedException is thrown by the cache.