developer747
developer747

Reputation: 15938

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?

Upvotes: 3

Views: 1669

Answers (2)

Leon
Leon

Reputation: 3401

Whatever mechanism, will need to know about all of the servers which need their caches cleared.

Instead of each server being aware of all other servers, have a centralized process (service/app/anything) which queues up cache-clear requests, and then sends it to each server in the farm.

This way you have the logic in one place, avoid possibly infinite loop (each server updating other servers, which update original, etc.) and can scale up with minimal changes.

Web Servers aren't responsible for managing the queue - just passing cache-clear events to some centralized process.

Upvotes: 0

TLS
TLS

Reputation: 3150

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.

Upvotes: 1

Related Questions