Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ensure consistency between MemCache and Datastore on GAE?

I am writing multiple entites to the datastore using a transaction. I want to keep these entities in MemCache, also. How do I ensure that the copy of the entity in MemCache actually equals the copy in Datastore?

E.g. I can do:

tx.begin()
datastore.put(entity)
if (memcache.putIfUntoched(key, entity))
  tx.commit()

But then if the transaction fails the entity will possibly end up in the MemCache but not in the Datastore. On the other hand, if I do:

tx.begin()
datastore.put(entity)
tx.commit()
memcache.putIfUntoched(key, entity))

then the Datastore transaction might succeed but the MemCache update could fail. How can I ensure consistency?

like image 522
Michael Avatar asked Jan 23 '26 17:01

Michael


1 Answers

From my experience, it may not be that helpful if you write to the DB and the cache at the same time. In general, mixing DB transactions with other stuffs (e.g. file system) is difficult to do it right.

I suggest you change your program logic, so that

  1. When you create a new record, write only to DB
  2. When you update an existing record, write to DB, and invalidate corresponding slots in cache
  3. When you're looking for a record, just check the cache. If it's not there, load from DB and fill in the cache
like image 146
Xiao Jia Avatar answered Jan 25 '26 20:01

Xiao Jia