Volume 10 · Chapter 5 — Messaging and Event-Driven Architecture
Streaming with Kinesis
5.1 Introduction
Queues and pub/sub handle discrete messages, but some workloads involve a continuous, high-volume stream of data - clicks, transactions, telemetry, logs - that you want to ingest and process in real time, often by multiple consumers reading the same data, with ordering and the ability to replay. That’s data streaming, and on AWS it’s Kinesis (and the related managed Kafka, MSK). This chapter covers streaming and how it differs from queuing, because knowing when to use a stream versus a queue is a meaningful design and interview distinction.
5.2 What Streaming Is
A data stream is an ordered, continuous sequence of records that producers write to and consumers read from, where the records are retained for a period so consumers can read and re-read them. Kinesis Data Streams ingests large volumes of streaming data in real time and lets one or more consumers process it. The defining characteristics versus a queue: records are retained for a retention window (not deleted when read), so multiple independent consumers can each read the whole stream, and a consumer can replay by re-reading from an earlier point; records are ordered within a partition (shard); and streams are built for high-throughput, continuous data. This suits real-time analytics, processing event streams, aggregating telemetry, and any case where you want ordered, replayable, multi-consumer processing of a continuous data flow.
flowchart LR Prod[Producers: transactions, clicks, telemetry] --> Stream[(Kinesis stream\nordered, retained records)] Stream --> C1[Consumer A: real-time fraud analytics] Stream --> C2[Consumer B: dashboards] Stream --> C3[Consumer C: archive to S3] Note[Each consumer reads the whole stream;\ncan replay from an earlier point] -.-> Stream
5.3 Streams vs Queues
The key distinction: in a queue (SQS), a message is typically consumed and deleted by one consumer - it’s about distributing discrete work items, each handled once. In a stream (Kinesis), records are retained and can be read by multiple consumers independently and replayed - it’s about processing a continuous flow of data, often by several consumers and often needing ordering. So you use a queue when you have discrete tasks to distribute and process once (process each payment), and a stream when you have a continuous high-volume data flow that multiple consumers process in order and may need to replay (real-time analytics over a transaction feed, telemetry processing). The retention-and-replay and multiple-independent-consumers-in-order properties are what make a stream a stream rather than a queue.
5.4 Ordering, Shards, and Scale
Kinesis scales through shards - a stream is divided into shards, each an ordered sequence, and throughput scales with the number of shards. Records are ordered within a shard, and which shard a record goes to is determined by a partition key, so records with the same key (say, the same account) land on the same shard and are processed in order. This gives ordered processing per key while scaling across shards for total throughput. The design consideration is choosing a partition key that both preserves the ordering you need (same-key records together) and distributes load evenly across shards (avoiding a “hot shard” where one key dominates) - the same distribution concern seen with DynamoDB keys. Getting the partition key right is central to both ordering and scale in streaming.
5.5 Streaming Processing and Replay
A major advantage of streams is replay: because records are retained, if a consumer has a bug or you add a new consumer, it can process the stream from an earlier point rather than having lost the data - invaluable for reprocessing after fixing a bug, backfilling a new consumer, or recovering from a processing failure. Consumers process the stream continuously and typically in near-real-time, enabling real-time analytics and reactions. Managed services build on this - Kinesis integrates with processing and delivery (for example, delivering stream data to S3 or a data store), and MSK offers managed Apache Kafka for teams standardized on Kafka. The combination of ordered, retained, replayable, multi-consumer, real-time processing is what makes streaming the right tool for continuous high-volume data pipelines.
5.6 Banking Production Scenario
A bank streams its transaction feed through Kinesis for real-time processing by multiple independent consumers. One consumer runs real-time fraud analytics on the ordered transaction stream, another feeds live dashboards, and another archives records to S3 - all reading the same retained stream independently. Transactions are partitioned by account so a given account’s transactions stay ordered on one shard, while many shards scale the total throughput, with the partition key chosen to avoid hot shards. When the fraud model is updated, the bank replays recent stream data through the new consumer to reprocess it - possible only because records are retained. For discrete “process this payment once” work, the bank uses SQS; for the continuous, ordered, replayable, multi-consumer transaction feed, it uses Kinesis - each tool matched to the workload.
5.7 Troubleshooting: Streaming Problems
Streaming issues cluster around ordering, scale, and tool fit. Records processed out of order when order matters mean the partition key doesn’t group related records onto one shard - choose a key (like account ID) that preserves the needed ordering. A “hot shard” throttling throughput means one partition key dominates - pick a key that distributes load evenly. Needing multiple consumers to each read all the data, or needing to replay, but using a queue (which deletes on consume) means you need a stream instead. Using a stream for simple distribute-once task processing adds needless complexity where a queue fits. Insufficient throughput means too few shards - scale shard count. The discipline is choosing a stream for continuous, ordered, replayable, multi-consumer data and a queue for discrete once-processed work, and designing the partition key for both ordering and even distribution.
When would you use a stream (Kinesis) instead of a queue (SQS)?
I use a stream instead of a queue when I have a continuous, high-volume flow of data that multiple consumers need to process independently, often in order, and possibly replay - as opposed to discrete work items each handled once. The key differences drive the choice. In a queue like SQS, a message is typically consumed and deleted by one consumer from a pool - it’s about distributing discrete tasks so each is processed once, like processing each payment. In a stream like Kinesis, records are retained for a retention window rather than deleted when read, so multiple independent consumers can each read the whole stream, a consumer can replay by re-reading from an earlier point, records are ordered within a partition, and it’s built for high-throughput continuous data. So I reach for a stream when those properties matter: when I have a continuous data flow like a transaction feed, clickstream, or telemetry; when several independent consumers each need to process all the data - for instance one doing real-time fraud analytics, another feeding dashboards, another archiving to S3, all reading the same stream; when I need ordered processing, which a stream provides within a shard by routing records with the same partition key to the same shard; and when I need replay - because records are retained, if a consumer has a bug or I add a new consumer, it can reprocess from an earlier point rather than having lost the data, which is invaluable for reprocessing after a fix or backfilling a new consumer. I use a queue instead when I have discrete tasks to distribute and process exactly once, where I don’t need multiple consumers reading the same messages, ordering across the whole flow, or replay - the competing-consumers model handles those efficiently. Kinesis scales through shards, each an ordered sequence, with throughput scaling as I add shards, and the partition key determines both the ordering (same-key records stay ordered on one shard) and the load distribution (a poorly chosen key creates a hot shard), so choosing it well is central. So the distinction is discrete-work-processed- once versus continuous-data-processed-by-many-in-order-with-replay: a queue distributes and consumes discrete messages, while a stream retains an ordered flow that multiple consumers read independently and can replay - and I match the tool to whether the workload is task distribution or continuous multi-consumer data processing, which for a bank means SQS for processing each payment once and Kinesis for real-time analytics over the ordered, replayable transaction feed.
Chapter Revision Notes
- A stream (Kinesis) is an ordered, retained, continuous sequence of records that multiple consumers read independently and can replay.
- Queue vs stream: a queue distributes discrete messages consumed once; a stream retains an ordered flow read by many consumers, with replay.
- Kinesis scales via shards (ordered each); the partition key sets ordering (same key → same shard) and distribution (avoid hot shards).
- Replay is a key advantage - reprocess after a fix or backfill a new consumer because records are retained.
- Use a stream for continuous, ordered, multi-consumer, replayable data (real-time analytics, telemetry); a queue for discrete once-processed work.