AWS Kinesis Streaming: Producer and Consumer

Moving from nightly batches to real-time means a stream. With AWS Kinesis a producer writes records to a stream and a consumer reads them per shard. Here's the producer/consumer pair and the concepts that matter.

Data Engineerawskinesisstreaming

Batch vs stream

Batch ETL processes data on a schedule (nightly), so insights lag a day. A stream processes events as they arrive. Kinesis is the managed stream: producers append records, consumers read them in near real-time, ordered within a shard.

The producer

import json, boto3
k = boto3.client("kinesis")
STREAM = "events"

def produce(events):
    for ev in events:
        k.put_record(
            StreamName=STREAM,
            Data=json.dumps(ev).encode(),
            PartitionKey=ev["user_id"],   # routes the record to a shard
        )

The partition key decides which shard a record lands on. Records with the same key go to the same shard and stay ordered - pick a key (e.g. user id) that groups events you need in order. For high throughput, put_records (plural) batches up to 500 records per call.

The consumer

Kinesis is pull-based per shard: get a shard iterator, then loop get_records:

def consume():
    shard_id = k.describe_stream(StreamName=STREAM)["StreamDescription"]["Shards"][0]["ShardId"]
    it = k.get_shard_iterator(StreamName=STREAM, ShardId=shard_id,
                              ShardIteratorType="TRIM_HORIZON")["ShardIterator"]
    while it:
        resp = k.get_records(ShardIterator=it, Limit=100)
        for rec in resp["Records"]:
            handle(json.loads(rec["Data"]))
        it = resp.get("NextShardIterator")

The concepts that matter

Want to try it hands-on? HeyDevJob gives you this exact setup in a live cloud workspace in your browser - edit it, run it, and see it work. Free, nothing to install.

Try it in a workspace →

What you'll practice

FAQ

How do I write data to a Kinesis stream?

Call put_record (or put_records for batches) with the stream name, the record Data as bytes, and a PartitionKey. The partition key decides which shard the record goes to and keeps same-key records ordered.

How does a Kinesis consumer read records?

Get a shard iterator (get_shard_iterator with a type like TRIM_HORIZON or LATEST), then loop get_records, advancing with the returned NextShardIterator each time. In production the Kinesis Client Library or a Lambda trigger handles this plus checkpointing.

What is a shard in Kinesis?

A shard is a unit of throughput and the ordering boundary - records are ordered within a shard but not across shards. You add shards to scale, and the partition key determines which shard a record lands on.

What is an Amazon Kinesis stream?

A Kinesis data stream is a managed service for real-time streaming data. Producers put records into shards and consumers read them in order per shard, buffering high-throughput event data for processing within seconds.

Is Kinesis better than Kafka?

Neither is strictly better. Kinesis is fully managed and integrates with AWS with no cluster to run; Kafka is open-source, higher-throughput, and portable but you operate it. Choose Kinesis for low-ops on AWS, Kafka for control and scale.

Keep learning

Make an ETL Pipeline IdempotentData projectSpeed Up a Slow SQL Report QueryData projectMake a CSV Importer Resilient to Bad DataData projectData roadmapStep by step to hiredData interview questionsSTAR answersAll Data projectsProjects hub

Learn it by doing. Open this in a live cloud workspace, make the change yourself, and keep a record of the work you can share.

Open the workspace →