Event-driven architecture patterns in DynamoDB - Time & Space Complexity
When using event-driven patterns with DynamoDB, it is important to understand how the time to process events grows as more events occur.
We want to know how the number of operations changes as the event load increases.
Analyze the time complexity of the following DynamoDB event processing snippet.
// Assume events is a list of event records
for event in events:
# Extract key from event
key = event['key']
# Query DynamoDB for item with key
response = dynamodb.get_item(TableName='MyTable', Key={'id': {'S': key}})
# Process the item
process(response['Item'])
This code loops over each event and fetches the related item from DynamoDB to process it.
Look at what repeats as the input grows.
- Primary operation: Looping over each event and performing a get_item call.
- How many times: Once per event in the events list.
As the number of events increases, the number of get_item calls grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 get_item calls |
| 100 | 100 get_item calls |
| 1000 | 1000 get_item calls |
Pattern observation: The work grows directly with the number of events.
Time Complexity: O(n)
This means the time to process events grows linearly as more events come in.
[X] Wrong: "Fetching one item from DynamoDB is slow, so processing many events is always slow regardless of approach."
[OK] Correct: Each get_item call is fast and independent, so total time depends mostly on how many events you process, not on hidden loops inside DynamoDB.
Understanding how event-driven processing scales helps you design systems that handle growing workloads smoothly.
"What if we batch multiple keys in one DynamoDB request instead of one get_item per event? How would the time complexity change?"
Practice
Solution
Step 1: Understand DynamoDB Streams role
DynamoDB Streams capture data modification events like inserts, updates, and deletes.Step 2: Identify the purpose in event-driven architecture
These captured events trigger other processes automatically, enabling real-time reactions.Final Answer:
To capture and record changes in the DynamoDB table as events -> Option AQuick Check:
DynamoDB Streams = capture changes [OK]
- Confusing Streams with backups
- Thinking Streams increase capacity
- Assuming Streams encrypt data
Solution
Step 1: Recall AWS CLI command for enabling streams
The correct command uses 'update-table' with '--stream-specification' to enable streams.Step 2: Verify syntax and parameters
aws dynamodb update-table --table-name MyTable --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES correctly sets StreamEnabled=true and specifies StreamViewType as NEW_AND_OLD_IMAGES.Final Answer:
aws dynamodb update-table --table-name MyTable --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES -> Option DQuick Check:
Enable streams = update-table + stream-specification [OK]
- Using non-existent commands like enable-stream
- Incorrect parameter names
- Confusing create-stream with enabling streams
Solution
Step 1: Understand StreamViewType NEW_IMAGE meaning
NEW_IMAGE sends only the new version of the item after the change.Step 2: Determine what Lambda receives on update
Lambda gets the updated item data, not the old version or just keys.Final Answer:
Only the new version of the item after the update -> Option CQuick Check:
NEW_IMAGE = new item data only [OK]
- Confusing NEW_IMAGE with NEW_AND_OLD_IMAGES
- Assuming keys only are sent
- Thinking Lambda gets no data
Solution
Step 1: Check if Streams are enabled
If Streams were not enabled, no events would be generated, but question implies setup.Step 2: Verify Lambda subscription to the stream
Without subscribing Lambda to the stream, it won't trigger on events.Final Answer:
The Lambda function is not subscribed to the stream -> Option BQuick Check:
Lambda must be subscribed to stream to trigger [OK]
- Assuming syntax errors stop invocation
- Thinking empty table stops Lambda trigger
- Ignoring subscription setup
Solution
Step 1: Identify real-time event-driven pattern
Real-time notifications require immediate reaction to data changes.Step 2: Match pattern with DynamoDB Streams and Lambda
Streams capture changes as events; Lambda processes them instantly to notify users.Final Answer:
Use DynamoDB Streams to capture changes and trigger Lambda to send notifications -> Option AQuick Check:
Streams + Lambda = real-time notifications [OK]
- Choosing polling which is not real-time
- Confusing replication with notifications
- Using backups which are not event triggers
Ready to prove it?
Pass the test and this pattern moves unknown
