Saying *NO* to stale data

Jacques Caron jc at oxado.com
Wed Jul 5 13:18:54 UTC 2006


Hi,

At 14:30 05/07/2006, Michael Carter wrote:
>1. A: user = memcached.get("user_1")
>2. B: user = memcached.get("user_1")
>3. B: user.email = "newemail", user.lastAction = now
>4. B: SQL update on user id=1
>5. B: memcached.set("user_1", user)
>6. A: user.lastAction = now
>7. A: SQL update on user id=1
>8. A: memcached.set("user_1", user) *** Here lies the problem
>
>Now the memcached version of user_1 has stale data for the email address.
>
>Is there a built-in or standard way of addressing this?

Options:
- store various bits of information is separate keys (may or may not 
be a good idea depending on the circumstances)

- when you change data, just delete the key in the cache (after DB 
update). The next one who will need it will fetch from DB, get 
something consistent, and save it to memcache

- when you change data, re-fetch the whole thing from DB after DB 
update and then save to memcache (the end result is similar to the previous)

- use memcached's "add" function to create a lock:
while (!memcached.add("lock_user_1")) sleep (1);
... fetch user 1 from memcache or DB, update user 1, save to DB and 
memcache ...
memcached.delete("lock_user_1");

>The final solution is to just expire the data whenever its updated, 
>but that seems awfully wasteful.

How so?

Jacques.




More information about the memcached mailing list