Why subscriptions enable real-time data in GraphQL - Performance Analysis
We want to understand how the work done by GraphQL subscriptions changes as more data or users connect.
How does the system handle updates in real-time as input grows?
Analyze the time complexity of this GraphQL subscription snippet.
subscription OnNewMessage {
messageAdded {
id
content
sender
}
}
This subscription listens for new messages and sends updates to all subscribed clients.
Look for repeated actions that happen as new data arrives or more clients subscribe.
- Primary operation: Sending new message data to each subscribed client.
- How many times: Once per new message, repeated for every subscribed client.
As more clients subscribe, the server sends the new message to each one.
| Input Size (n = number of clients) | Approx. Operations (sending updates) |
|---|---|
| 10 | 10 sends per new message |
| 100 | 100 sends per new message |
| 1000 | 1000 sends per new message |
Pattern observation: The work grows directly with the number of subscribed clients.
Time Complexity: O(n)
This means the time to send updates grows linearly with the number of subscribed clients.
[X] Wrong: "Subscriptions send updates instantly without extra work as clients increase."
[OK] Correct: Each new client adds more work because the server must send the update to every subscriber.
Understanding how subscriptions scale helps you explain real-time data handling clearly and confidently.
What if the subscription filtered messages so only some clients get updates? How would that affect the time complexity?