开发者

How to implement Memcache in my application

Given the following scenario, please suggest me a way to implement memcache in my application.

Currently, I have 10 w开发者_如何学运维ebservers in which the same application is being run and a load balancer to decide upon to which web server the request be sent. On each webserver, I am maintaining a local cache i.e. there is some class XYZ which controls the MySQL table xyz and this class has initialize method which will warm up the local cache.

Now, suppose the webservers are X,Y,Z. The load balancer sends a request to X and this request adds some values to db & updates the cache. Again the same request was sent by the load balancer to Y. But since server Y doesnot have the value in the cache, it hits the database.

So, given this scenario, how should I implement memcache in my application so that I could minimize db hits. Should I have a separate memcache server and all the other 10 webservers will get the cached data from this memcacher server?


One work around (not ideal though), would be to implement sticky session on the load balancer so that request from one user always go through to the same server (for the duration of their session). This doesn't help much if a server dies or you need cached data shared between sessions (but it is easy and quick to do if your load balancer supports it).

Otherwise the better solution is to use something like memcached (or membase if your feeling adventurous). Memcached can either be deployed on each or your servers or on separate servers (use multiple servers to avoid the problem of one servers dying and taking your cache with it). Then on each of your application servers specify in your memcached client connection details for all of the memcached servers (put them in the same order on each server and use a consistent hashing algorithm in the memcached client options to determine on which server(s) the cache key will go). In short - you now have a working memcached set-up (as long as you use it sensibly inside your application)

There are plenty of memcached tutorials out there that can help with the finer points of doing all this but hopefully my post will give you some general direction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜