Subscription syntax in GraphQL - Time & Space Complexity
When using GraphQL subscriptions, it's important to understand how the time to deliver updates grows as more events happen.
We want to know how the system handles repeated data updates over time.
Analyze the time complexity of the following subscription syntax.
subscription OnNewMessage {
newMessage {
id
content
sender {
name
}
}
}
This subscription listens for new messages and fetches their details as they arrive.
Look at what repeats when new data arrives.
- Primary operation: Sending each new message update to all subscribed clients.
- How many times: Once per new message event, repeated indefinitely as messages arrive.
As more messages come in, the system sends more updates.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 messages | 10 update sends |
| 100 messages | 100 update sends |
| 1000 messages | 1000 update sends |
Pattern observation: The number of operations grows directly with the number of new messages.
Time Complexity: O(n)
This means the work grows linearly with the number of new messages sent to subscribers.
[X] Wrong: "The subscription sends all past messages every time a new message arrives."
[OK] Correct: Subscriptions only send new data as it happens, not the entire history each time.
Understanding how subscriptions scale helps you explain real-time data flow in apps, a useful skill for many projects.
"What if the subscription filtered messages by sender? How would that affect the time complexity?"