409 Conflict in Rest API - Time & Space Complexity
When handling a 409 Conflict response in a REST API, it's important to understand how the server checks for conflicts before processing requests.
We want to know how the time to detect conflicts grows as the number of resources increases.
Analyze the time complexity of the following REST API conflict check.
// Pseudocode for handling a PUT request
function updateResource(id, newData) {
existing = findResourceById(id);
if (existing.version != newData.version) {
return "409 Conflict";
}
save(newData);
return "200 OK";
}
This code checks if the resource version matches before updating to avoid conflicts.
Look for operations that repeat or scale with input size.
- Primary operation: Searching for the resource by ID.
- How many times: Once per request, but the search may scan through many resources.
As the number of resources grows, finding the resource may take longer if no fast lookup is used.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The time to find the resource grows linearly with the number of resources.
Time Complexity: O(n)
This means the time to detect a conflict grows in direct proportion to the number of resources.
[X] Wrong: "Checking for conflicts is always instant regardless of data size."
[OK] Correct: Without efficient lookup, the server may need to scan many resources, making conflict detection slower as data grows.
Understanding how conflict detection scales helps you design APIs that stay fast and reliable as data grows, a key skill in real-world development.
"What if we used a hash map to store resources by ID? How would the time complexity change?"