Stream vs polling comparison in DynamoDB - Performance Comparison
When working with DynamoDB, we often need to get updates from the database. Two common ways are streams and polling.
We want to understand how the time it takes to get updates changes as the amount of data grows.
Analyze the time complexity of these two approaches.
// Polling example
while (true) {
let results = dynamodb.scan({ TableName: 'MyTable' });
process(results.Items);
wait(5000); // wait 5 seconds
}
// Stream example
// DynamoDB Streams sends only new changes as events
onStreamEvent(event) {
process(event.Records);
}
The polling code repeatedly scans the whole table every few seconds. The stream code processes only new changes as they happen.
- Polling primary operation: Scanning the entire table repeatedly.
- Polling how many times: Every fixed interval, regardless of changes.
- Stream primary operation: Processing only new change records as they arrive.
- Stream how many times: Only when data changes happen.
Compare how work grows as the table size or changes increase.
| Input Size (n) | Polling Approx. Operations | Stream Approx. Operations |
|---|---|---|
| 10 items | Scan 10 items every interval | Process only changed items |
| 100 items | Scan 100 items every interval | Process only changed items |
| 1000 items | Scan 1000 items every interval | Process only changed items |
Pattern observation: Polling work grows with total table size each time. Stream work grows only with actual changes.
Time Complexity: O(n) for polling, O(k) for streams where k is number of changes
Polling checks all data every time, so work grows with table size. Streams only handle new changes, so work grows with changes, not total data.
[X] Wrong: "Polling is always faster because it's simpler and doesn't need setup."
[OK] Correct: Polling does more work as data grows, causing delays and higher cost. Streams handle only new data, making them more efficient for large or active tables.
Understanding how data retrieval methods scale helps you design efficient systems. This skill shows you can think about real-world costs and performance, which is valuable in many jobs.
"What if the polling interval is increased to scan less often? How would that affect the time complexity and system responsiveness?"