Kinesis Data Streams and Iterator Age
Posted: | Tags: aws cloudAlarming on Kinesis Data Streams’ GetRecords.IteratorAgeMilliseconds metric should come with a little more explanation in my opinion. Maybe it’s just me that needs it, nontheless, here we go.
Kinesis Data Stream is a resource that allows you to stream events, much like its name suggests. Consumers read from a shard by calling GetRecords with a shard iterator. Each call returns with a batch of records and the next iterator to pick up from. The consumer processes the records and calls GetRecords with the next iterator.
The iterator age, measured in milliseconds, is the current time minus the time the last record from your fetched batch was written. In practical terms, it tells you how old the data returned by your iterator is. The stream-level metric rolls up every call across each shard, so by setting the statistic of the Metric in CloudWatch to Maximum you can see the age of whichever shard is furthest behind. The older the data the more likely one or more of your consumers are lagging behind in processing the stream. By default, data streams have a retention period of 24 hours, this is also the minimum amount of time. For workloads that are not realtime or for consumers that process events in large batches you can increase the data retention to ensure you aren’t dropping events.
Setting an alarm based on GetRecords.IteratorAgeMilliseconds is recommended by AWS, and in general seems like a good idea. The guidance specifically recommends setting the statistic to Maximum, and a threshold dependant on your data rentention and the expected consumer behaviour, 50% of the rention is a good place to start. The alarm’s period should be 60 seconds, with 15 out of 15 datapoints to alarm. That means every datapoint in a 15-minute window has to breach the threshold, one after the other, before triggering the alarm. That’s important!
Here’s the explanation I wish I had. Let’s say, to make things simple, a Kinesis data stream only has one consumer, an Amazon Data Firehose, which delivers the events to an S3 bucket. In this setup there’s two possible scenarios in a healthy system that could cause a spike in iterator age. The first I found occurs in data streams with few events or intermittement events, and the second on stream reshards.
Sporadic events
In a data stream with sporadic events, Amazon Data Firehose calls on the shards every hour. This is coming purely from my observation, I couldn’t find any documentation making this claim. If there aren’t any events flowing through you can expect to see a spike in the GetRecords.IteratorAgeMilliseconds every hour that slowly grows. Each time Firehose reads from that shard, the metric reports the time since the last record, so the spike grows until a new record arrives to reset it.

If you have deep pockets you can enable per-shard monitoring and see the individual shards and their interator age whenever Firehose reads the last event. Here’s the same graph but looking at the maximum statistic per-shard.

You can then compare the age with the time of the last event in each shard using this command:
$ ITERATOR=$(aws kinesis get-shard-iterator --stream-name <STREAM> --shard-id <SHARD_ID> \
--shard-iterator-type AT_TIMESTAMP --query 'ShardIterator' --output text)
$ aws kinesis get-records --shard-iterator "$ITERATOR" --limit 1 \
--query 'Records[0].ApproximateArrivalTimestamp'
Resharding
In the event of resharding, triggered automatically in on-demand streams based on the flow of events or manually in provisioned streams by you, dear administrator, you can also see spikes in iterator age. A shard can be split resulting in two shards. The parent shard, which was split, will be closed so no further events can land there but it still remains readable. This shard will be closed until the last record in that shard passes the retention period, after which the shard will move to an expired state.
Amazon Data Firehose will continue to query a closed shared, in my experience, once every 30 minutes. With each of those requests the parent shard that is now clsoed returns its last record, written right before the split. So, the metric reports the tiem since the time since the split. Every spike should be 30 minutes older than the one before, until the parent shard expires. I was able to align the time of each spike with when Firehose calls ListShards based on CloudTrail events, like the previous timing and behaviour this is undocumented and based on my observations.

Once again, with shard-level metrics enabled you can confirm that this growing half-hourly spike belongs to the closed shard.

You can find ID of the closed shard with the following command:
$ aws kinesis list-shards --stream-name <STREAM> \
--query 'Shards[?SequenceNumberRange.EndingSequenceNumber].ShardId'
We’re looking for an ending sequence number in this case, since for open shards this will be null.
Alarming on iterator age
Coming back to the guidance outlined by AWS, in both scenarios the alarm wouldn’t fire. On the resharded stream the iterator age passed 12 hours, which is over 50% of the 24-hour data retention period, but they were not 15 consecutive data points. Firehose kept working on the active shards inbetween the list call.
I initially paid no mind mind the period and evluation thresholds, and I suspect other might not have as well. Hopefully this helped illustrate why a single or small number of breaching data points shouldn’t be a cause for concern.
Consumer metrics
In order to judge how far beind consumers are, I’d say looking at the consumer applications metrics is probably your best bet. Firehose, for example emits KinesisMillisBehindLatest which tells you how far behind the last read record is compared to the newest read in the stream. This metric is zero on low-traffic or sporadic stream. Other consumers have their own versions of this, IteratorAge for Lambda, MillisBehindLatest for KCl and SubscribeToShardEvent.MillisBehindLatest for a enhanced fan-out consumer.
As for the data stream itself, there are still useful metrics like throttling on puts and gets, and failed operations which make a good base for alarms. The AWS documentation has no shortage of those.