Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do delete cache by key on a load balanced application?

I have a load balanced application of 3 web servers. I have a requirement where on the click of a button I need to delete cache by key, simultaneously on all three webservers. For example, there is a cache named currentTemperature. So when I click a button, I want just cache["currentTemperature"] deleted on all three web servers. I don't want any other cache items to get deleted in the process.

Any ideas?

like image 830
developer747 Avatar asked Dec 29 '25 01:12

developer747


1 Answers

When you run a load-balanced web farm, each of the servers has its own cache. One possibility would be to implement a mechanism that causes the web server that responded to the original "delete cache" request to make the same request to the other servers. This can get pretty complicated and difficult to manage, though. Each server would need to "know" about the other servers in the farm to know which other servers to contact.

If you put something in a config file (or a database) to store the complete list of servers (perhaps by direct internal IP address) and write your cache-clearing code to automatically discover the current server, then the same code can be deployed to all servers.

Alternatively, you could simply use a brute-force approach. Keep a list of the internal IPs, and when a certain request is processed by a web server, have it send that same request to all internal IPs. The code that processes the request would need to know whether it's an internal or external request to avoid an infinite loop of requests from each server. This could be done with a name/value pair in the querystring that would indicate that it's an internal request - something that would not be included in the request from a browser.

This is a pretty theoretical discussion, but does it sound like something that might work for you?

NOTE: This assumes that your caching is done within the IIS application on each web server and not in a separate custom service layer.

like image 151
TLS Avatar answered Dec 31 '25 16:12

TLS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!