I'd like to know what would be a better approach to invalidate/update cache objects.
Prerequisites
- Having remote memcached server (serving as cache for multiple applications)
- All servers are hosted by azure (affinity regions, same data centers)
- Cache object size ranges from 200 bytes up to a 50 kilobytes
Approach 1 (store in cache asap)
- Object A is created -> store in database and store in cache
- Object A requested by client -> check cache for existence, otherwise fetch from database and store in cache
- Object A gets updated -> store in database, store in cache
Approach 1 seems to be more straightforward. If something is created, put in the cache asap. Regardless of someone will need it.
Approach 2 (lazy cache store)
- Object A is created -> store in database
- Object A requested by client -> check cache for existence, otherwise fetch from database and store in cache
- Object A gets updated -> store in database, delete key in cache
Approach 2 seems to be more memory-aware. In this approach only requested items go to cache.
Question 1: In mind of performance, what would be a better approach? Memory nor CPU do not count yet.
Question 2: Are my thoughts a kind of premature optimization?
Question 3: Any other thoughts? Other approaches?
Best Answer