fire
fire

Reputation: 21531

Using crc32 over md5 for hashing memcached keys

We currently use MD5 to hash keys that we want to lookup in memcached.

A basic example being:

$sql = "SELECT * FROM articles WHERE id = 1";
$key = md5($sql);

if (!$results = $memcache->get($key)) {
    $results = $db->query($sql);
    $memcache->set($key, $results);
}

The key sizes are all 32 bytes as it uses MD5 to hash the key.

We are considering using crc32 instead to hash the key to save memory e.g:

$key = hash('crc32', $sql);

This generates a key of only 8 bytes.

Is this a good enough solution to replace MD5 as the key hash? Is there an increase in potential collision's with keys?

Upvotes: 1

Views: 1876

Answers (1)

robertklep
robertklep

Reputation: 203304

Read http://bretm.home.comcast.net/~bretm/hash/8.html (TL;DR: "CRC32 was never intended for hash table use. There is really no good reason to use it for this purpose, and I recommend that you avoid doing so").

Do you have that many unique queries that it warrants switching to something else besides MD5? If so, consider something more suitable besides CRC32, like MurmurHash or CityHash.

Upvotes: 1

Related Questions