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?"
Practice
Solution
Step 1: Understand DynamoDB Streams behavior
DynamoDB Streams send data change events as they happen, pushing updates immediately.Step 2: Understand polling behavior
Polling requires the client to ask for updates at fixed intervals, not instantly.Final Answer:
Streams push data changes immediately, polling checks periodically. -> Option CQuick Check:
Streams = push, Polling = periodic check [OK]
- Confusing which method pushes data
- Thinking polling is real-time
- Assuming streams require manual checks
Solution
Step 1: Identify correct AWS CLI command for streams
The correct command to enable streams is 'update-table' with '--stream-specification' option.Step 2: Verify syntax correctness
aws dynamodb update-table --table-name MyTable --stream-specification StreamEnabled=true,StreamViewType=NEW_IMAGE uses correct flags and parameters: StreamEnabled=true and StreamViewType=NEW_IMAGE.Final Answer:
aws dynamodb update-table --table-name MyTable --stream-specification StreamEnabled=true,StreamViewType=NEW_IMAGE -> Option AQuick Check:
Enable streams with update-table and stream-specification [OK]
- Using non-existent commands like create-stream
- Incorrect flag names
- Missing StreamEnabled parameter
Solution
Step 1: Understand polling frequency impact
Polling every 10 minutes means updates are checked only at those intervals, not instantly.Step 2: Compare with streams behavior
Streams push changes immediately, so polling delays updates and may miss real-time events.Final Answer:
You receive updates only every 10 minutes, possibly missing immediate changes. -> Option DQuick Check:
Polling = delayed updates, not real-time [OK]
- Assuming polling is real-time
- Thinking polling triggers streams
- Believing polling disables streams
Solution
Step 1: Analyze polling frequency effects
Polling very frequently can cause throttling or API limits, increasing latency.Step 2: Understand polling limitations
Polling does not guarantee no missed updates and can overload the system if too frequent.Final Answer:
Polling too frequently causes throttling and delays. -> Option BQuick Check:
Too frequent polling = throttling and latency [OK]
- Thinking streams must be off to poll
- Believing polling disables writes
- Assuming frequent polling prevents missed updates
Solution
Step 1: Identify real-time update requirement
The dashboard needs instant updates when data changes, requiring immediate notification.Step 2: Compare streams and polling for real-time needs
Streams push changes immediately, ideal for real-time dashboards; polling delays updates and is less efficient.Final Answer:
Use DynamoDB Streams to push changes and update dashboard in real-time. -> Option AQuick Check:
Real-time dashboard = use streams [OK]
- Choosing polling despite real-time need
- Ignoring cost vs latency trade-offs
- Assuming polling can be truly real-time
Ready to prove it?
Pass the test and this pattern moves unknown
