Safe methods vs unsafe methods in Rest API - Performance Comparison
We want to understand how the time it takes to handle safe and unsafe REST API methods changes as the number of requests grows.
How does the server's work increase when processing these different methods?
Analyze the time complexity of the following code snippet.
// Example REST API handler
function handleRequest(request) {
if (request.method === 'GET' || request.method === 'HEAD') {
return readData(request);
} else {
return modifyData(request);
}
}
function readData(request) {
// Reads data from database
}
function modifyData(request) {
// Updates database and triggers side effects
}
This code handles safe methods (GET, HEAD) by reading data and unsafe methods by modifying data.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading or modifying data per request.
- How many times: Once per request, but modifyData may trigger extra operations like database writes or side effects.
As the number of requests increases, the server does more work for each request.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads or writes |
| 100 | 100 reads or writes |
| 1000 | 1000 reads or writes |
Pattern observation: The work grows directly with the number of requests, whether safe or unsafe.
Time Complexity: O(n)
This means the time to handle requests grows linearly with how many requests come in.
[X] Wrong: "Safe methods like GET take no time or less time than unsafe methods."
[OK] Correct: Both safe and unsafe methods require processing; unsafe methods may do more work, but safe methods still take time to read and respond.
Understanding how different REST methods affect server work helps you explain API performance and design choices clearly in interviews.
"What if modifyData triggered multiple database writes per request? How would the time complexity change?"
Practice
Solution
Step 1: Understand safe methods
Safe methods do not change data on the server; they only read or retrieve data.Step 2: Identify the safe method among options
GET is the method used to request data without modifying it, making it safe.Final Answer:
GET -> Option DQuick Check:
Safe method = GET [OK]
- Confusing POST as safe
- Thinking PUT is safe
- Assuming DELETE is safe
Solution
Step 1: Identify method for creating resources
POST is used to send data to the server to create new resources.Step 2: Check syntax correctness
POST /resource HTTP/1.1 is the correct syntax to send data for creation.Final Answer:
POST /resource HTTP/1.1 -> Option AQuick Check:
Creating resource = POST [OK]
- Using GET to send data
- Confusing DELETE with POST
- Using PUT for creation without context
PUT /items/123 HTTP/1.1
Content-Type: application/json
{"name": "New Item"}
Choose the correct description.Solution
Step 1: Understand the PUT method purpose
PUT is used to update or replace an existing resource at a specific URL.Step 2: Analyze the request details
The request targets /items/123 with JSON data, meaning it updates item 123.Final Answer:
It updates or replaces the item with ID 123 with new data. -> Option BQuick Check:
PUT updates resource = C [OK]
- Thinking PUT retrieves data
- Confusing PUT with DELETE
- Assuming PUT creates without ID
POST /users HTTP/1.1 Host: example.com
Solution
Step 1: Understand the goal and method used
The goal is to safely retrieve data, but POST is used which changes data.Step 2: Identify correct method for safe retrieval
GET is the safe method to retrieve data without side effects.Final Answer:
POST is unsafe and changes data; GET should be used to retrieve data. -> Option CQuick Check:
Safe retrieval = GET, not POST [OK]
- Ignoring method safety
- Thinking Host header is missing
- Confusing HTTP versions
Solution
Step 1: Identify the action needed
The goal is to remove (delete) a user account from the server.Step 2: Choose the correct HTTP method for deletion
DELETE is the method designed to remove resources safely and clearly.Final Answer:
DELETE, because it explicitly removes the resource on the server. -> Option AQuick Check:
Deleting resource = DELETE method [OK]
- Using GET for deletion
- Confusing POST with DELETE
- Using PUT instead of DELETE
Ready to prove it?
Pass the test and this pattern moves unknown
