How to make sure all the replicas of a consumer read the same message in a kinesis queue

Viewed 81

I have few consumers which updates their local cache on reading a message from the kinesis queue. But this leads to inconsistency as message is only processed by only one among all the replicas and any information retrieved from other replicas gives us invalid cache.

How can i make sure i clear the cache among all the replicas when the invalidation message appears in the queue.

1 Answers

The library that you're using assigns consumers to shards, so any message that you put on the stream will only go to one consumer.

The best solution would be to send your invalidation message out-of-band, using another Kinesis stream (or alternative such as SNS). This would add complexity to your listener, as they'd now have to listen for two sources (and you couldn't use that library for the out-of-band messages).

If you want to send in-band cache invalidation messages, then you need to write the validation message multiple times, with different partition keys, so that it goes to all shards in the stream. The way that the partition key works is that it's MD5-hashed, and that hash is used to select a shard (see PutRecord doc).

Assuming that your application can handle multiple cache invalidation messages, I think that the best way to handle this is to write multiple messages with different partition keys (no need to get fancy, just use "A", "B", "C", and so on). Keep doing this until you've written a message to every shard (getting the shard ID for each record from the response to PutRecord).

You can call ListShards with a ShardFilter of AT_LATEST to get the shard IDs of the currently active shards.

Related