Caching your data in your application code is generally a good idea for many reasons. We have being doing this for quiet some time in our shared environment which includes ColdFusion, .NET, and PHP. However, because we share this environment with many other development groups in our organization, there is significantly more downtime then we (or our customers) appreciate.
Therefore, our web admins are moving to implement a new environment. In this environment they are adding a QA level between the current Dev and Prod environments. Also, to increase uptime, they are clustering machines at both the QA and Prod level.
This is all great for many reasons. The one area where I see a problem is with caching. There will be two (or more depending on the number of nodes) sets of cache. As you can imagine, this creates the following potential problem. Cache is the same on node A and B. User 1 updates data and thus cache is updated on node A. User 2 comes to query data but is on node B and therefore get’s the old data.
Are their any best practices on how to handle this?
Is there any type of change I can make in my code?
Are there server settings that can be implemented?
The basic two approaches for content caching are 1) centralization and 2) replication.
Each can be implemented in various ways, and to various levels of complexity.
If you’re talking about just a small group of web servers, then a simple centralization setup is what you want. I would recommend a memcached server per environment (which PHP supports). So in your model, both nodes A and B would use cached data from a new node: node C.
Replication is the more scalable solution, but it’s also significantly more complicated to implement. But you need to hit a vast traffic volume to go this route (think facebook, youtube, wikipedia) so I doubt you need to worry about it.