Refetching and polling in GraphQL - Time & Space Complexity
When using refetching and polling in GraphQL, we want to understand how the time it takes to get data changes as we ask for more updates.
We ask: How does the number of operations grow when we repeatedly fetch data?
Analyze the time complexity of the following GraphQL polling query.
query GetMessages {
messages {
id
content
timestamp
}
}
# Polling every 5 seconds to get new messages
This query fetches a list of messages repeatedly at fixed intervals to keep data fresh.
Look for repeated actions that affect time.
- Primary operation: Fetching the entire list of messages each poll.
- How many times: Once every polling interval (e.g., every 5 seconds).
As the number of messages grows, each poll fetches more data, so the work grows with the list size.
| Input Size (n messages) | Approx. Operations per Poll |
|---|---|
| 10 | 10 fetch operations |
| 100 | 100 fetch operations |
| 1000 | 1000 fetch operations |
Pattern observation: The work grows linearly as the number of messages increases.
Time Complexity: O(n)
This means the time to fetch data grows directly with the number of messages each time we poll.
[X] Wrong: "Polling only fetches new messages, so time stays the same no matter how many messages exist."
[OK] Correct: Polling as shown fetches the whole list every time, so more messages mean more work each poll.
Understanding how repeated data fetching scales helps you explain real-world app behavior and design efficient updates.
What if we changed polling to only fetch messages newer than the last one received? How would the time complexity change?