0
0
GraphQLquery~5 mins

Subscription syntax in GraphQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Subscription syntax
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As more messages come in, the system sends more updates.

Input Size (n)Approx. Operations
10 messages10 update sends
100 messages100 update sends
1000 messages1000 update sends

Pattern observation: The number of operations grows directly with the number of new messages.

Final Time Complexity

Time Complexity: O(n)

This means the work grows linearly with the number of new messages sent to subscribers.

Common Mistake

[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.

Interview Connect

Understanding how subscriptions scale helps you explain real-time data flow in apps, a useful skill for many projects.

Self-Check

"What if the subscription filtered messages by sender? How would that affect the time complexity?"