keepcalculator

06 Numbers and Powers

Make Your API Faster: 7 Fixes to Measure and Try

Card No. 06·31237 · Updated · Standard formula, shown below

This article gives you seven concrete, measurable fixes to speed up your API, from database query optimization to HTTP caching. You’ll learn what to change, how to test the impact, and what to watch out for—so you can cut latency and improve throughput without guessing.

Why API Speed Matters

API latency directly affects user experience and your bottom line. A 100-millisecond delay can reduce conversion rates by up to 7% (according to a widely cited Google study). Slow APIs also tie up server resources, increase infrastructure costs, and frustrate third-party developers who depend on your endpoints. Speeding up your API isn’t just about feeling faster—it’s about reliability, cost, and customer retention.

Before you start tweaking, establish a baseline. Measure response time, throughput (requests per second), and error rate under normal and peak load. Use tools like curl, Postman, or load testers (e.g., k6, Locust) to capture numbers. Only after you have a baseline can you verify that a fix actually helped.

Fix 1: Optimize Database Queries

The database is the most common bottleneck. A single slow query can turn a 10ms endpoint into a 500ms one. Start by enabling slow query logging in your database (e.g., MySQL’s slow_query_log or PostgreSQL’s log_min_duration_statement). Identify queries that run frequently or take longer than 100ms.

  • Add indexes on columns used in WHERE, JOIN, and ORDER BY clauses. Use EXPLAIN to verify the query plan uses them.
  • Reduce fetched columns—don’t SELECT *; only request what the client needs.
  • Use pagination (e.g., keyset or offset) to avoid loading thousands of rows at once.
  • Cache query results in memory (Redis, Memcached) for read-heavy endpoints.

Example: If you have a users table and your endpoint filters by email, adding an index on email can reduce lookup time from O(n) to O(log n). Measure before and after with EXPLAIN ANALYZE.

Fix 2: Implement HTTP Caching

HTTP caching lets clients and intermediate proxies reuse responses without hitting your server. This is one of the highest-impact fixes for read-heavy APIs.

  • Cache-Control headers: Set Cache-Control: public, max-age=3600 for public, non-sensitive data. For personalized data, use private.
  • ETags: Return an entity tag that changes when the resource changes. The client sends If-None-Match, and your server replies 304 Not Modified if the tag matches—saving bandwidth and processing.
  • Last-Modified: Similar to ETag but based on a timestamp. Use If-Modified-Since.

Be careful with caching authenticated endpoints—never cache responses that contain sensitive user data unless you use private or vary by user. Test with tools like curl -I to inspect headers.

Fix 3: Use Compression (Gzip/Brotli)

Compressing response bodies reduces network transfer time, especially for JSON payloads. Most web servers and frameworks support gzip or Brotli out of the box.

  • Enable compression on your reverse proxy (Nginx, Apache) or in your app (e.g., Express compression middleware).
  • For JSON, you can also use a more efficient format like MessagePack or Protobuf, but compression alone often yields 60-80% size reduction.
  • Set Content-Encoding: gzip and ensure clients send Accept-Encoding: gzip.

Measure the effect: compare response size and time with and without compression. A 200KB JSON body might compress to 20KB, cutting transfer time by 10x on slow connections.

Fix 4: Reduce Payload Size

Even with compression, sending extra fields wastes time and bandwidth. Trim your responses to only what the client needs.

  • Use sparse fieldsets (e.g., ?fields=id,name) so clients can request only specific attributes.
  • Remove unnecessary nested objects or flatten them.
  • Use pagination for list endpoints—don’t return 10,000 items when the client shows 20.

Example: An endpoint that returns user profiles might include created_at, last_login, and settings that the mobile app never uses. Removing them can cut payload size by half.

Fix 5: Enable Keep-Alive and HTTP/2

Connection overhead adds latency, especially for mobile clients. HTTP keep-alive allows multiple requests over a single TCP connection, avoiding repeated handshakes. HTTP/2 goes further with multiplexing—multiple requests can be sent concurrently over one connection.

  • Ensure your server supports HTTP/2 (most modern servers like Nginx and Node.js do).
  • Set Connection: keep-alive (default in HTTP/1.1) and tune keep-alive timeout.
  • For HTTP/2, you don’t need to concatenate assets; multiplexing handles it.

Measure the difference with tools like ab (ApacheBench) or wrk. You’ll often see a 20-50% reduction in latency for multiple requests.

Fix 6: Parallelize Independent Calls

If your API endpoint makes multiple upstream calls (e.g., to other services or databases), do them in parallel instead of sequentially. This is especially relevant in microservice architectures.

  • Use Promise.all in JavaScript, asyncio.gather in Python, or CompletableFuture in Java.
  • Only parallelize calls that are independent—if one call’s result feeds another, you must sequence them.
  • Set timeouts on each call to avoid hanging.

Example: An endpoint that fetches user profile, recent orders, and notifications can run all three queries concurrently, reducing total time from the sum of three latencies to the maximum of the three.

Fix 7: Use a CDN and Edge Caching

For public, read-only endpoints (like product catalogs or static content), a Content Delivery Network (CDN) can serve responses from servers close to the user, cutting network latency dramatically.

  • Put your API behind a CDN like Cloudflare, Fastly, or Akamai.
  • Configure cache rules for GET endpoints that return public data.
  • Use edge caching for HTML, JSON, and images.

You can also use edge functions (e.g., Cloudflare Workers) to run simple logic at the edge, reducing round trips to your origin server. Measure the Time to First Byte (TTFB) from different geographic locations before and after.

How to Measure the Impact

Each fix should be validated with data. Here’s a simple process:

  1. Pick one endpoint that is slow or frequently used.
  2. Record baseline metrics: average latency, p95 latency, throughput, and error rate.
  3. Apply one fix at a time (not all at once) so you know what worked.
  4. Re-run the same load test and compare.
  5. If the fix helps, keep it; if not, revert and try another.

Use percentile metrics (p50, p95, p99) because averages hide outliers. A single slow query can ruin p99 even if the average looks fine.

Common Pitfalls to Avoid

  • Over-caching: Caching too aggressively can serve stale data. Use short max-age for dynamic content.
  • Ignoring slow downstream calls: If your API calls a third-party service, that latency is in your critical path. Consider caching or timeouts.
  • Premature optimization: Don’t add complexity (like message queues) before you’ve measured the actual bottleneck.
  • Not testing under realistic load: A fix that works for 10 users may fail at 1000. Use load testing with concurrent users.

Frequently Asked Questions

How do I find which endpoint is slow?

Use APM tools (e.g., New Relic, Datadog) or add logging for response times. Look for endpoints with high p95 or p99 latency. You can also use curl -w "%{time_total}" to quickly test individual endpoints.

Should I use a cache or a database index first?

Start with database indexes because they fix the root cause of slow queries. Caching is a band-aid that can mask problems and introduce staleness. Indexes are cheap and permanent.

Is compression always good?

For most text-based responses (JSON, HTML), yes. But for already-compressed data (e.g., images, videos), compression wastes CPU. Also, very small responses (<1KB) may not benefit. Test with your actual payloads.

Can HTTP/2 work over plain HTTP?

No, HTTP/2 requires TLS (HTTPS) in practice. Most browsers and servers only support HTTP/2 over TLS. So you’ll need an SSL certificate, which you should have anyway.

What if my API is write-heavy?

For write-heavy APIs, focus on database write optimization (batch inserts, async processing), connection pooling, and using a message queue for non-critical writes. Caching is less useful for POST/PUT endpoints.

How much speedup can I expect?

It varies. Database optimization alone can cut latency by 50-80% if you have a bad query. Compression can reduce transfer time by 60-90%. CDN can cut network latency by half for distant users. But the total depends on your current bottlenecks.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →