Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top