2

A really common pattern that I need in multi instance web applications is invalidating MemoryCaches over all instances - and waiting for a confirmation that this has been done. (Because a user might otherwise after a refresh suddenly see old data on another instance)

We can make this with a combination of:

  • AzureServicebus,
  • Sending message to a topic
  • other instances send message back with ReplyTo to the original instance
  • have a wait loop for waiting on the messages back,
  • be aware of how many other instances are there in the first place.
  • probably some timeout because what happens if an instance crashes in between?

I think working out all these little edge cases might be a lot of work - so before we reinvent the wheel - is there already a common pattern or library for this?

(of course one solution would be using a shared cache like Redis, but for some situations a memorycache is a lot faster)

Dirk Boer
  • 8,522
  • 13
  • 63
  • 111

3 Answers3

2

Have a look at Azure Durable Functions, e.g. Fan-In/Fan-Out scenario. They use Azure Storage Queues underneath, but provide higher-level abstractions.

Note that Durable Functions are still in early preview (as of August 2017), so not suitable for production use yet.

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
2

I think working out all these little edge cases might be a lot of work - so before we reinvent the wheel - is there already a common pattern or library for this?

Indeed. This sounds like a candidate for a middleware framework such as NServiceBus or MassTransit.

AzureServicebus

Both NServiceBus and MassTransit support Azure Service Bus as the transport.

Sending message to a topic

Both NServiceBus and MassTransit can Publish messages (events) to topics.

other instances send message back with ReplyTo to the original instance

Both NServiceBus and MassTransit can send messages to specific destination. NServiceBus also can Reply to the originator of an incoming message using a request/reply pattern.

have a wait loop for waiting on the messages back

Both NServiceBus and MassTransit support Sagas, also known as Process Coordinator pattern.

be aware of how many other instances are there in the first place.

Not sure about this requirement. When you scale out, you're running with a competing consumer and shouldn't care about number of instances of an endpoint.

probably some timeout because what happens if an instance crashes in between?

If you refer to retries and recovery, then both NServiceBus and MassTransit support retries.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80
0

You can use Azure Redis cache pub/sub model to do this. 1) Subscribe to Redis multiplexer

connectionMultiplexer.GetSubscriber().Subscribe(
                        "SubscribeChannelName",
                        (channel, message) => { 

invalidate cache here and publish the confirmation using below publish method
connectionMultiplexer.GetSubscriber().PublishAsync("PublishChannelName", "Cache invalidated for instance").Wait();
});

2) Publish the cache invalidation and subscribe for confirmation from instances

var connection = ConnectionMultiplexer.Connect("redis connection string");
var redisSubscriber = connection.GetSubscriber();
redisSubscriber.Subscribe(
                        "PublishChannelName",
                        (channel, message) => {

// write logic to verify if all instances notified about cache invalidation.
});
redisSubscriber.PublishAsync("SubscribeChannelName","invalidate cache")).Wait();
Pratik Mehta
  • 1,310
  • 4
  • 15
  • 37