Kinesis Streaming Data
Complete the full lesson to earn 25 points
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks and see fewer ads — read each lesson on a single page with Pro
Mastering Data Ingestion with Amazon Kinesis
In the landscape of modern data engineering and machine learning, the ability to process information as it arrives is often the difference between a reactive system and a proactive one. Traditionally, data pipelines relied on batch processing, where records were gathered over hours or days and then moved to a warehouse for analysis. However, as business requirements evolve toward real-time decision-making, we must shift our focus toward streaming architectures. Amazon Kinesis serves as the backbone for these architectures, acting as a managed service that allows you to collect, process, and analyze streaming data in real time.
Understanding Kinesis is not just about learning a specific tool; it is about mastering the flow of information. Whether you are tracking user clicks on a website, monitoring telemetry from industrial sensors, or processing financial transaction logs, Kinesis provides the infrastructure to ingest these events reliably. By decoupling the data producers from the data consumers, Kinesis enables your machine learning models to ingest live features, allowing them to make predictions based on the most current state of your system rather than stale, historical data.
The Core Architecture of Kinesis
To understand how Kinesis works, we must first look at its fundamental building block: the Kinesis Data Stream. A stream is essentially an ordered sequence of data records meant to be written to and read from in real time. Unlike a traditional database table where you query specific rows, a stream is a continuous flow of data that is partitioned to allow for parallel processing.
Understanding Shards
The primary unit of throughput in a Kinesis stream is the "shard." A shard is a base throughput unit that provides a fixed capacity: 1 MB/second of input and 2 MB/second of output per second, with a limit of 1,000 records per second for writes. If your application exceeds these limits, you will encounter throttling errors, which can lead to data loss if your producers do not implement proper retry logic.
When you create a stream, you must specify the number of shards. This decision is critical because it dictates the maximum performance of your ingestion pipeline. If your data volume grows, you can "reshard" (split or merge shards) to adjust capacity, but this is an operational task that should be planned for rather than performed as an emergency fix.
The Role of Partition Keys
Data records in Kinesis consist of a sequence number, a partition key, and a data blob. The partition key is the most important element for developers because it determines which shard a record is sent to. When you send a record, Kinesis hashes the partition key and uses that value to map the record to a specific shard.
Callout: Partitioning Strategy Choosing the right partition key is essential for maintaining a balanced stream. If you choose a key with low cardinality—for example, using "region" when 90% of your traffic comes from one region—you will create "hot shards," where one shard handles almost all the load while others sit idle. Aim for high cardinality keys, such as User IDs or Device IDs, to ensure data is distributed evenly across all shards.
Setting Up Your First Data Stream
Before we dive into the code, let’s walk through the steps of setting up a stream via the AWS CLI. While the console is fine for exploration, the CLI is the preferred method for maintaining infrastructure-as-code.
Step 1: Create the Stream
You start by defining the stream name and the number of shards.
aws kinesis create-stream \
--stream-name user-activity-stream \
--shard-count 2
This command initializes a stream with two shards. Remember, each shard provides 1 MB/s of ingestion. If you expect higher traffic, you would increase the --shard-count parameter accordingly.
Step 2: Verify the Stream Status
Streams are not created instantly. You must wait for the stream to reach an "ACTIVE" state before you can begin pushing data to it.
aws kinesis describe-stream --stream-name user-activity-stream
Look for the StreamStatus field in the JSON output. Once it reads "ACTIVE," you are ready to begin sending records.
Producing Data: Sending Records to Kinesis
A "producer" is any application that puts data into your Kinesis stream. This could be a web server, a mobile application, or a background worker process. The most common way to interact with Kinesis is through the AWS SDKs, such as Boto3 for Python.
Example: A Simple Python Producer
The following script demonstrates how to send a mock user event to a Kinesis stream.
import boto3
import json
import random
import time
# Initialize the Kinesis client
client = boto3.client('kinesis', region_name='us-east-1')
def send_event():
while True:
# Create a mock user event
event = {
'user_id': random.randint(1000, 9999),
'action': random.choice(['click', 'login', 'logout']),
'timestamp': time.time()
}
# Put the record into the stream
response = client.put_record(
StreamName='user-activity-stream',
Data=json.dumps(event),
PartitionKey=str(event['user_id'])
)
print(f"Sent record with sequence number: {response['SequenceNumber']}")
time.sleep(1)
if __name__ == "__main__":
send_event()
Key Considerations for Producers
- Batching: Sending records one by one is inefficient due to network overhead. Always use
put_recordsinstead ofput_recordto send multiple events in a single HTTP request. This significantly reduces latency and lowers your costs. - Error Handling: Network fluctuations occur. Your producer must be capable of catching
ProvisionedThroughputExceededExceptionand implementing an exponential backoff strategy to retry the failed records. - Data Serialization: While you can send raw bytes, it is best practice to use a structured format like JSON or Avro. This makes downstream processing significantly easier when you begin training machine learning models.
Note: If you are using the Kinesis Producer Library (KPL), it handles batching, retries, and asynchronous delivery for you. While it adds a bit of complexity, it is highly recommended for high-volume production applications.
Consuming Data: Reading from the Stream
Once data is in the stream, you need a "consumer" to extract it. There are several ways to consume Kinesis data: Kinesis Data Firehose, AWS Lambda, or a custom application using the Kinesis Client Library (KCL).
Using AWS Lambda as a Consumer
Lambda is the most popular choice for serverless stream processing. When you trigger a Lambda function from Kinesis, the service automatically polls the stream, batches records, and invokes your function.
- Batch Size: You can configure the
BatchSize(e.g., 100 records) andBatchWindow(e.g., 60 seconds). - Checkpointing: Lambda manages the shard iterator for you. If your function fails, Lambda will retry the batch based on the configuration, ensuring you don't lose data.
Example: Processing Events in Lambda
import json
import base64
def lambda_handler(event, context):
for record in event['Records']:
# Kinesis data is base64 encoded
payload = base64.b64decode(record['kinesis']['data']).decode('utf-8')
data = json.loads(payload)
# Business logic for ML feature extraction
print(f"Processing user {data['user_id']} action: {data['action']}")
return {'statusCode': 200}
Comparison: Kinesis Data Streams vs. Kinesis Data Firehose
A common point of confusion for beginners is the difference between Kinesis Data Streams (KDS) and Kinesis Data Firehose. While both are part of the Kinesis family, they serve different purposes.
| Feature | Kinesis Data Streams | Kinesis Data Firehose |
|---|---|---|
| Latency | Real-time (milliseconds) | Near real-time (minutes) |
| Management | You manage shards/scaling | Fully managed, auto-scaling |
| Consumption | Custom consumers (KCL, Lambda) | Directly writes to S3, Redshift, OpenSearch |
| Use Case | Complex logic, real-time ML | Log archival, batch loading |
If your goal is to feed a machine learning model that requires sub-second latency, use Kinesis Data Streams. If your goal is to store raw events in an S3 data lake for later batch training, Kinesis Data Firehose is the more cost-effective and simpler choice.
Best Practices for Production Systems
Successfully managing a streaming pipeline requires more than just functional code; it requires operational rigor. Below are the industry standards for keeping your Kinesis environment healthy.
1. Monitoring with CloudWatch
Never run a stream in production without monitoring. You must watch the following metrics in Amazon CloudWatch:
- IncomingBytes / IncomingRecords: To understand traffic patterns.
- ReadProvisionedThroughputExceeded: Indicates that your consumers are not keeping up with the data volume.
- WriteProvisionedThroughputExceeded: Indicates that your producers are sending data faster than your shards can handle.
2. Implementing Retries
Always implement exponential backoff in your code. If a request to Kinesis fails, wait a short period before retrying, and increase that wait time with each successive failure. This prevents your application from overwhelming the service during periods of high contention.
3. Handling Data Evolution
When working with machine learning, your feature schema will inevitably change. If you are using JSON, add versioning fields to your records. If you are using Avro or Protobuf, use a schema registry. Never assume the data structure will remain static for the lifetime of your pipeline.
Warning: Do not store sensitive information like PII (Personally Identifiable Information) in cleartext within your Kinesis stream. If you must pass it, encrypt the data at the application level before sending it to the stream.
4. Security and Access Control
Use AWS Identity and Access Management (IAM) to follow the principle of least privilege. Your producers should have kinesis:PutRecord permissions only, while your consumers should have kinesis:GetRecords, kinesis:GetShardIterator, and kinesis:DescribeStream permissions. Avoid using root or administrator credentials for your pipeline applications.
Common Pitfalls and How to Avoid Them
Even experienced engineers run into trouble when setting up streaming systems. Here are the most frequent mistakes:
The "Hot Shard" Problem
As mentioned earlier, poor partition key selection leads to hot shards. If you notice one shard has 90% of the traffic while others are idle, your processing will bottleneck at that single shard regardless of how many shards you have overall.
- The Fix: Review your partition key. If you are using a fixed value like "sensor_id" and one sensor is faulty and sending millions of events, implement a strategy to salt the key (e.g.,
sensor_id + random_int) to spread the load.
Ignoring Throughput Limits
Developers often assume Kinesis is "infinite." It is not. If you hit your shard limit, your producers will start failing.
- The Fix: Implement automated scaling. You can use AWS Auto Scaling to adjust the shard count of your stream based on the
IncomingBytesmetric from CloudWatch.
Lack of Dead Letter Queues (DLQ)
What happens when your consumer fails to process a record? If you don't have a plan, that record might be lost or stuck in a retry loop.
- The Fix: Always implement a DLQ. If a record fails processing after a defined number of retries, write that record to a separate S3 bucket or a "failed-events" queue so you can inspect it manually and debug the issue.
Integrating Kinesis with Machine Learning
For machine learning practitioners, Kinesis is the gateway to "Online Learning" or "Online Inference." When a model is deployed to an endpoint, it needs a continuous flow of data to provide predictions.
Real-Time Feature Engineering
In many ML systems, you need to calculate features like "number of clicks in the last 10 minutes." Kinesis enables this by streaming events to a stateful processing engine like Apache Flink (via Amazon Managed Service for Apache Flink). Flink can maintain a window of the last 10 minutes of activity, update that feature in real time, and push it to a feature store like Amazon SageMaker Feature Store.
Step-by-Step for ML Ingestion:
- Producer: Instrument your application code to send event logs to Kinesis.
- Stream: Configure the stream to handle the expected peak throughput.
- Consumer: Use a Flink application to perform windowed aggregations.
- Feature Store: Sink the processed features into an online feature store.
- Inference: Your model endpoint queries the feature store to retrieve the latest feature values, allowing it to make accurate predictions based on recent user behavior.
Advanced Data Handling: Compression and Encryption
When dealing with large volumes of data, costs can escalate quickly. Optimizing your payload size is a key engineering task.
Payload Compression
If you are sending large JSON blobs, consider compressing the data at the producer level using GZIP or Snappy. This reduces the number of bytes sent, which can help stay within the 1 MB/s shard limit and reduce your overall AWS bill. However, remember that this adds CPU overhead to both the producer and the consumer.
Encryption at Rest
Kinesis supports server-side encryption using AWS KMS (Key Management Service). This is a best practice for compliance and security. Enabling this is a simple toggle in the AWS console or a single parameter in your CloudFormation template. It ensures that data written to the stream is encrypted before being stored on disk, protecting your data in the event of a storage-layer compromise.
Summary: A Checklist for Success
To wrap up this module, keep this checklist handy whenever you are building a Kinesis-based ingestion pipeline:
- Capacity Planning: Have you calculated your peak expected throughput in MB/s? Does your shard count support this?
- Partitioning: Does your partition key provide high cardinality? Will it distribute data evenly?
- Producer Resilience: Does your producer code have exponential backoff and retry logic?
- Consumer Monitoring: Are you tracking
ReadProvisionedThroughputExceededto ensure your consumers are keeping up? - Security: Is your stream encrypted with KMS? Are your IAM roles scoped to the minimum required permissions?
- Error Handling: Do you have a plan for handling malformed records or failed processing attempts (e.g., a DLQ)?
- Scalability: If your traffic spikes, do you have an auto-scaling strategy or a manual process to increase shards?
Frequently Asked Questions
Q: Can I change the shard count of a stream that is currently receiving data? A: Yes, you can. You can split shards to increase capacity or merge shards to decrease capacity. However, be aware that this operation takes time, and you should monitor the stream carefully during the transition.
Q: How long is data stored in Kinesis? A: By default, data is stored for 24 hours. You can extend this retention period up to 365 days, but keep in mind that you are charged for the additional storage time.
Q: What is the maximum size of a single data record? A: The maximum size of a data blob (the actual payload) is 1 MB. If you have records larger than this, you must compress them or split them into multiple records before sending.
Q: Is Kinesis suitable for transactional data? A: Kinesis is an event-streaming platform, not a database. It is excellent for capturing the history of transactions for audit or analysis, but it should not be used as the primary source of truth for the state of a transaction. Always ensure your database (e.g., RDS or DynamoDB) is the primary record of truth.
Final Thoughts
The transition from batch processing to streaming data is a fundamental shift in how we approach machine learning. By utilizing Amazon Kinesis, you are not just moving data; you are creating a live nervous system for your applications. This allows your models to learn from the present, adapt to changing patterns, and deliver value in the moment.
Focus on the fundamentals: keep your shards balanced, monitor your throughput, and ensure your consumers are robust enough to handle the inevitable irregularities of real-world data. Start small, test your throughput assumptions with load tests, and build your pipeline with failure in mind. As you master these ingestion patterns, you will find that the complexity of real-time data becomes a manageable—and powerful—tool in your engineering toolkit.
Key Takeaways
- Decoupling: Kinesis decouples data producers from consumers, allowing you to scale each independently and manage data ingestion as a continuous flow rather than a batch operation.
- Shards are Everything: Performance is dictated by shard count. You must monitor your throughput and scale shards proactively to avoid throttling and data loss.
- Partitioning Strategy: The choice of partition key is critical. High-cardinality keys are necessary to avoid "hot shards" and ensure even data distribution.
- Resilience is Non-Negotiable: Always implement exponential backoff in your producers and ensure your consumers have a dead-letter queue strategy for handling failed records.
- Operational Monitoring: Use CloudWatch metrics to watch for throughput exceptions; these are your primary indicators of a struggling pipeline.
- ML Integration: For real-time machine learning, Kinesis is the bridge between raw event logs and the low-latency feature stores required for accurate, modern inference.
- Security First: Always use encryption (KMS) and follow the principle of least privilege for IAM access, ensuring your data pipeline is secure from the moment it is created.
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles your daily points limit so you progress twice as fast, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× daily points limit
- ✓ Distraction-free lessons