Idempotency of methods in Rest API - Time & Space Complexity
When working with REST APIs, some methods can be called multiple times without changing the result beyond the first call.
We want to understand how repeated calls affect the work the server does.
Analyze the time complexity of repeated calls to an idempotent API method.
// Example: PUT /resource/123
// Request body: { "name": "New Name" }
// Server updates resource 123 with new name
// Returns updated resource
// This call can be repeated many times
// without changing the resource after first update
This code updates a resource with new data, and calling it multiple times has the same effect as calling it once.
Look at what happens when the method is called multiple times.
- Primary operation: Updating the resource in the database.
- How many times: Each call repeats the update operation.
Each call does the same amount of work regardless of previous calls.
| Number of Calls (n) | Approx. Operations |
|---|---|
| 1 | 1 update operation |
| 10 | 10 update operations |
| 100 | 100 update operations |
Pattern observation: The work grows linearly with the number of calls.
Time Complexity: O(n)
This means the total work grows directly with how many times the method is called.
[X] Wrong: "Calling an idempotent method multiple times costs no extra work after the first call."
[OK] Correct: Each call still makes the server do the update operation, so work adds up with each call.
Understanding idempotency helps you design APIs that are safe to retry and scale well, a useful skill in real projects.
"What if the method was not idempotent, like POST creating new items each time? How would the time complexity change with repeated calls?"