How to store the key size more than 250 characters in memcached using java code?

Memcached is a popular cahing engine used by many companies like twitter, linkedin etc. It has one peculiar restriction which put limit on the length of cache keys to 250 characters. Long keys are usually a sign of misuse of memcached, though, so it often helps to take a step back and evaluate what you actually need to put into your key.

How to get pass this limit

1. It is a limitation of the memcached server, so you have to change the ‘KEY_MAX_LENGTH’ value and re-compile your memcached server.But this is a tedious job

2. What you can do is use md5 or sha1 to hash your string and use that as a key.. Disadvantage of this approach is that you can have key collisions and your application can get inconsistent values fro given keys, though chances of key collisions are very very low. This is java code to hash a string using SHA-256 algorithm :

9) ? 'A'+l-10 : '0'+l)) ;
        return sb.toString() ;

Uday Ogra

Connect with me at and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *