Bird
Raised Fist0
Rest APIprogramming~5 mins

Why caching reduces server load in Rest API - Performance Analysis

Choose your learning style10 modes available
Time Complexity: Why caching reduces server load
O(1)
Understanding Time Complexity

We want to see how caching affects the work a server does when handling requests.

How does caching change the number of steps the server takes as requests grow?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


// Pseudocode for handling API requests with caching
function handleRequest(request) {
  if (cache.has(request.key)) {
    return cache.get(request.key);  // Fast return from cache
  } else {
    let data = fetchFromDatabase(request.key);  // Slow database call
    cache.set(request.key, data);  // Store in cache
    return data;
  }
}
    

This code checks if the requested data is in cache. If yes, it returns quickly. If not, it fetches from the database and saves it in cache.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Checking cache and possibly fetching from database.
  • How many times: Once per request, repeated for each incoming request.
How Execution Grows With Input

As the number of requests grows, the server work depends on cache hits or misses.

Input Size (requests)Approx. Operations
10Mostly quick cache checks, few database fetches
100Many cache checks, some database fetches
1000Mostly cache hits, very few database fetches

Pattern observation: With caching, most requests are fast checks, so work grows slowly even as requests increase.

Final Time Complexity

Time Complexity: O(1)

This means each request takes about the same small amount of time, so the server handles more requests efficiently.

Common Mistake

[X] Wrong: "Caching makes the server do less work only sometimes, so it doesn't really help much."

[OK] Correct: Actually, caching means most requests skip slow database calls, so the server workload stays low even with many requests.

Interview Connect

Understanding how caching reduces repeated work shows you can improve server speed and handle more users smoothly.

Self-Check

"What if the cache size is very small and often clears? How would the time complexity change?"

Practice

(1/5)
1. Why does caching reduce the load on a server when handling REST API requests?
easy
A. Because it forces the server to recalculate data every time
B. Because it increases the number of requests the server must process
C. Because it deletes data from the server to free up space
D. Because it stores copies of responses to serve repeated requests faster

Solution

  1. Step 1: Understand caching purpose

    Caching saves copies of data or responses so the server doesn't have to create them again for repeated requests.
  2. Step 2: Effect on server load

    By serving stored copies, the server avoids extra processing, reducing its workload.
  3. Final Answer:

    Because it stores copies of responses to serve repeated requests faster -> Option D
  4. Quick Check:

    Caching reduces server work = D [OK]
Hint: Caching saves repeated work by storing data copies [OK]
Common Mistakes:
  • Thinking caching increases server calculations
  • Confusing caching with deleting data
  • Believing caching forces recalculation every time
2. Which HTTP header is commonly used to control caching behavior in REST APIs?
easy
A. Cache-Control
B. Authorization
C. Content-Type
D. User-Agent

Solution

  1. Step 1: Identify caching headers

    The HTTP header that controls caching rules is named Cache-Control.
  2. Step 2: Verify other headers

    Authorization is for security, Content-Type defines data format, and User-Agent identifies the client.
  3. Final Answer:

    Cache-Control -> Option A
  4. Quick Check:

    Cache header = Cache-Control [OK]
Hint: Cache-Control header manages caching rules [OK]
Common Mistakes:
  • Choosing Authorization for caching
  • Confusing Content-Type with caching control
  • Selecting User-Agent instead of cache header
3. Consider this REST API server with caching enabled. What will be the output of the following sequence?
1. Client requests resource A (not cached)
2. Server processes and caches response for A
3. Client requests resource A again
4. Server returns cached response
What happens at step 4?
medium
A. Server returns the cached response instantly without processing
B. Server processes resource A again and returns new response
C. Server returns an error because cache is empty
D. Server delays response to check for updates

Solution

  1. Step 1: Understand caching steps

    At first request, server processes and caches the response. On the second request, the cached response is available.
  2. Step 2: Behavior at repeated request

    Server returns the cached response immediately without reprocessing, speeding up response time.
  3. Final Answer:

    Server returns the cached response instantly without processing -> Option A
  4. Quick Check:

    Cached response served = A [OK]
Hint: Cached data skips processing on repeat requests [OK]
Common Mistakes:
  • Thinking server recalculates every time
  • Assuming cache is empty on second request
  • Believing server delays to check updates
4. A developer added caching to their REST API but notices stale data is served too long. What is the likely cause?
medium
A. Cache is disabled in the server
B. Server is not processing requests
C. Cache expiration time is set too high
D. Client is not sending requests

Solution

  1. Step 1: Identify stale data cause

    If cached data stays too long, it means the cache expiration or max-age is set too high, so data is not refreshed often.
  2. Step 2: Check other options

    Cache disabled means no caching, so no stale data. Server not processing or client not sending requests would cause no responses, not stale data.
  3. Final Answer:

    Cache expiration time is set too high -> Option C
  4. Quick Check:

    Long cache time = stale data = B [OK]
Hint: Long cache expiration causes stale data [OK]
Common Mistakes:
  • Assuming cache is off when stale data appears
  • Blaming server or client inactivity
  • Ignoring cache expiration settings
5. You want to reduce server load by caching REST API responses but also ensure data freshness. Which strategy best balances these needs?
hard
A. Cache responses indefinitely without validation
B. Set a short cache expiration and use conditional requests with ETag headers
C. Disable caching completely to always get fresh data
D. Only cache error responses to reduce load

Solution

  1. Step 1: Understand caching and freshness tradeoff

    Short cache expiration limits stale data time. Conditional requests with ETag let server confirm if data changed before sending full response.
  2. Step 2: Evaluate other options

    Disabling caching increases load. Indefinite caching causes stale data. Caching only errors doesn't reduce load effectively.
  3. Final Answer:

    Set a short cache expiration and use conditional requests with ETag headers -> Option B
  4. Quick Check:

    Short cache + ETag = freshness + load reduction = C [OK]
Hint: Use short cache and ETag for freshness and efficiency [OK]
Common Mistakes:
  • Disabling caching to fix freshness
  • Caching forever without validation
  • Caching only error responses
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard