Log groups and log streams in AWS - Time & Space Complexity
When working with AWS logs, it is important to understand how the time to manage logs grows as you add more logs and streams.
We want to know how the number of API calls changes as we create or list log groups and streams.
Analyze the time complexity of the following operation sequence.
# Create a log group
aws logs create-log-group --log-group-name MyLogGroup
# For each application instance, create a log stream
for i in $(seq 1 $n); do
aws logs create-log-stream --log-group-name MyLogGroup --log-stream-name Stream_$i
done
# List all log streams in the log group
aws logs describe-log-streams --log-group-name MyLogGroup
This sequence creates one log group, then creates multiple log streams inside it, and finally lists all streams.
- Primary operation: Creating log streams inside the log group.
- How many times: Once for the log group, then n times for the log streams.
- Listing log streams is a single operation but returns data proportional to n.
As the number of log streams n increases, the number of create calls grows directly with n.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 1 (log group) + 10 (streams) + 1 (list) = 12 |
| 100 | 1 + 100 + 1 = 102 |
| 1000 | 1 + 1000 + 1 = 1002 |
Pattern observation: The total API calls grow linearly as you add more log streams.
Time Complexity: O(n)
This means the time to create and list log streams grows directly in proportion to the number of streams.
[X] Wrong: "Creating many log streams happens in constant time because it is just one command."
[OK] Correct: Each log stream requires a separate API call, so the total time grows with the number of streams.
Understanding how AWS logging operations scale helps you design systems that handle logs efficiently as they grow.
"What if we batch create log streams instead of one by one? How would the time complexity change?"
Practice
log group in AWS CloudWatch Logs?Solution
Step 1: Understand log group role
A log group acts as a container for multiple log streams, grouping logs by application or resource.Step 2: Differentiate from other options
Log events are stored in log streams, not directly in log groups. Access control and billing are unrelated.Final Answer:
To organize multiple log streams into a single container -> Option AQuick Check:
Log group = container for log streams [OK]
- Confusing log groups with log streams
- Thinking logs are stored directly in log groups
- Mixing log groups with access control or billing
appStream inside a log group appGroup using AWS CLI?Solution
Step 1: Identify correct AWS CLI command syntax
The command to create a log stream isaws logs create-log-streamwith parameters--log-group-nameand--log-stream-name.Step 2: Verify aws logs create-log-stream --log-group-name appGroup --log-stream-name appStream matches syntax
aws logs create-log-stream --log-group-name appGroup --log-stream-name appStream correctly uses the command and parameters in the right order and names.Final Answer:
aws logs create-log-stream --log-group-name appGroup --log-stream-name appStream -> Option DQuick Check:
Correct CLI syntax = aws logs create-log-stream --log-group-name appGroup --log-stream-name appStream [OK]
- Using create-log-group instead of create-log-stream
- Swapping parameter names
- Omitting required flags
aws logs create-log-group --log-group-name myAppLogs
aws logs create-log-stream --log-group-name myAppLogs --log-stream-name stream1
aws logs put-log-events --log-group-name myAppLogs --log-stream-name stream1 --log-events '[{"timestamp":1609459200000,"message":"Start"}]'
aws logs describe-log-streams --log-group-name myAppLogsWhat will the
describe-log-streams command output include?Solution
Step 1: Trace commands creating log group and stream
The first two commands create a log group 'myAppLogs' and a log stream 'stream1' inside it.Step 2: Put log event and describe streams
The third command adds a log event to 'stream1'. The last command lists streams in 'myAppLogs', so it will show 'stream1' with events.Final Answer:
A list containing one log stream named 'stream1' with stored events -> Option AQuick Check:
Created stream appears in describe output [OK]
- Expecting empty list before adding streams
- Confusing log groups with streams in output
- Assuming describe lists groups, not streams
aws logs put-log-events --log-group-name myGroup --log-stream-name myStream --log-events '[{"timestamp":1609459200000,"message":"Hello"}]'But you get an error saying the log stream does not exist. What is the most likely fix?
Solution
Step 1: Understand error cause
The error indicates the log stream 'myStream' does not exist in 'myGroup'. You must create the stream before sending logs.Step 2: Identify correct fix
Creating the log stream withaws logs create-log-streamresolves the error. Changing group or removing timestamp won't help.Final Answer:
Create the log stream 'myStream' inside 'myGroup' before putting events -> Option CQuick Check:
Log stream must exist before putting logs [OK]
- Trying to put logs before stream creation
- Ignoring error details about missing stream
- Confusing log group creation with stream creation
Solution
Step 1: Understand grouping and streaming roles
Log groups organize logs for management and retention. Log streams separate logs by source within groups.Step 2: Choose best structure for microservices
Using one log group for all microservices and separate streams per microservice allows easy management and searching.Step 3: Evaluate other options
Create one log stream per microservice without using log groups lacks grouping, C creates many groups complicating retention, A ignores CloudWatch benefits.Final Answer:
Create one log group for all microservices, and create separate log streams for each microservice inside it -> Option BQuick Check:
Group = container; streams = sources [OK]
- Skipping log groups and using only streams
- Creating too many log groups unnecessarily
- Ignoring CloudWatch log management features
Ready to prove it?
Pass the test and this pattern moves unknown
