I am currently developing a system optimized for read. The architecture is a MySQL as the authoritative, persistent storage, and a memcache layer between the MySQL db and clients. Writes happen from time to time but much less frequently than reads.
The db is running on a relatively weak host so I have the incentive to minimize accesses to the db. The currently protocol in my mind is like this:
[Both DB and memcache entries are associated with versioning sequences for optimistic locking]
- At boot time, a dedicated process loads all currently entries from db to memcache.
- When a write happens, it will:
- a. load the entry version from memcache (memcache version).
- b. in a db transaction, try to update db if (db entry version < loaded memcache version + 1), the db entry version will be updated to (memcache version + 1) upon success.
- c. if b succeeded, proceed to update memcache with the new entry and version = (memcache version + 1)
- Meanwhile, the dedicated process periodically loads all data from the db and put them into memcache, just in case some writes are not propagated to memcache. May cause some race condition here with 2 but I estimate that to be acceptable.
My crurrent assumption is that for an entry at any time, (memcache version) <= (db version), and hence multiple concurrent writes from 2 doesn't cause race condition.
Could anyone find any problem out of this design? Thanks!