AWS
Kinesis
ShardIteratorType
TRIM_HORIZON
Data Streaming

Expected behavior for AWS Kinesis ShardIteratorType TRIM_HORIZON

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Amazon Web Services (AWS) Kinesis Data Streams, shard iterators provide the means for accessing data records within a stream. One critical shard iterator type is TRIM_HORIZON. This iterator type allows consumers to process data from the very beginning of the shard, offering the advantage of replaying all available records. This article explores the expected behavior, implications, and technical aspects of TRIM_HORIZON.

Understanding ShardIteratorType: TRIM_HORIZON

Basics of Shard Iterators

In Kinesis, each shard in a data stream can be thought of as a continuous sequence of data records, where shard iterators act as pointers to these records. When you retrieve records from a shard, the iterator provides the current location in the shard and allows you to read records starting from that point.

TRIM_HORIZON Overview

The TRIM_HORIZON iterator type provides an essential capability: it starts reading at the oldest available record in a shard. This behavior enables comprehensive data processing, suitable for scenarios where consumers need to process or review all historical data.

Practical Use Cases

  • Log Analysis: Applications that need to replay all logs for analyses or debugging benefit from using TRIM_HORIZON.
  • Data Replication: For creating backups or replicating data across regions or services, starting from the earliest possible point ensures completeness.
  • Migration Scenarios: During the migration of data processing workflows, opting for TRIM_HORIZON can help ensure that no historical data is skipped.

Technical Explanation

How It Works

When a consumer application requests a shard iterator with the TRIM_HORIZON type:

  1. The iterator is positioned at the oldest data record in the shard's current retention period.
  2. The consumer uses this iterator to start reading data sequentially.

Data Retention Implications

By default, Kinesis maintains data for 24 hours, adjustable to up to 365 days with extended retention. Consequently, the TRIM_HORIZON iterator will only access records within the current retention window.

Example

Suppose a Kinesis stream's earliest record is timestamped at 2023-09-01T00:00:00Z. A request for a TRIM_HORIZON iterator on 2023-09-02T00:00:00Z will provide a stream reading access from the start time.

Example Code

A typical implementation in AWS SDK for Python (Boto3) might look like this:

  • Latency: Reading from TRIM_HORIZON may incur extra latency due to extensive data processing from the stream's initial point.
  • Throughput: Ensure sufficient read throughput is configured to accommodate the potentially high volume of historical records, especially in long-retention streams.
  • LATEST: Only reads new data as it arrives in the stream, suitable for real-time processing.
  • AT_SEQUENCE_NUMBER and AFTER_SEQUENCE_NUMBER: Read data based on specific sequence numbers, useful for pinpointing exact data records.
  • AT_TIMESTAMP: Starts reading at a specified timestamp, allowing for temporal alignment in data processing tasks.

Course illustration
Course illustration

All Rights Reserved.