Ways to improve API performance
Seven levers that turn a sluggish endpoint into a fast one.
Most slow APIs are slow for boring, fixable reasons: they return too much data, recompute what they could cache, reopen connections, and fan a single request into hundreds of database round-trips. Paginate, cache at every layer, compress payloads, pool connections, kill N+1 queries, go async, batch, and serialize efficiently.
Paginate
The first fix is to stop returning everything. An endpoint that serializes an unbounded list will grow slower and heavier with every new row until it times out entirely. Returning results in bounded pages — via cursor or offset — caps the work per request and keeps latency flat no matter how large the underlying dataset becomes.
Cache at every layer
The fastest work is work you never repeat. Cache aggressively across the whole path: HTTP caching in the client and CDN for static or slow-changing responses, and a server-side cache like Redis for hot query results and computed objects. A high cache hit ratio removes the majority of load from your slowest components — the database and any downstream services.
Compress payloads
Bytes on the wire cost time, especially on mobile networks. Enabling gzip or brotli compression shrinks JSON responses dramatically for near-zero CPU, and trimming the payload to only the fields a client actually needs cuts it further. Smaller responses mean lower latency, less bandwidth, and faster parsing on the client.
Pool connections
Opening a fresh database or TLS connection per request wastes a full round-trip on setup every time. A connection pool keeps a set of established connections warm and hands them out to incoming requests, so the expensive handshake is paid once and amortized across thousands of calls. This alone often halves latency on database-heavy endpoints.
Kill N+1 queries
The classic hidden killer: you fetch a list of N items, then loop and issue one more query per item to load its details — N+1 round-trips for one logical request. Replace the loop with a single join or a batched WHERE id IN (...) query so the database returns everything in one trip. This is frequently the single biggest win when profiling a slow endpoint.
Go async & non-blocking
Not everything a request triggers needs to finish before you respond. Push slow, non-critical work — sending email, generating a report, updating a search index — onto a background queue and return immediately. Combined with a non-blocking (async) server model, this frees threads to handle other requests instead of sitting idle waiting on I/O, raising throughput under load.
Batch & serialize well
Collapse many small operations into one: accept bulk requests, batch downstream calls, and group writes so you make one round-trip instead of dozens. Finally, choose the right serialization — efficient JSON handling, or a compact binary format like Protobuf for high-volume internal traffic — because encoding and decoding cost real CPU at scale. Together these trim the per-request overhead that dominates once the obvious wins are done.
Why APIs get slow
Almost no API is slow because of one dramatic flaw. It is slow because of an accumulation of small, ordinary inefficiencies: it returns more data than anyone needs, recomputes results it could have remembered, reopens expensive connections on every call, and turns a single logical request into a swarm of database round-trips. The good news is that these causes are well understood and individually fixable, and a handful of standard levers will take most endpoints from sluggish to fast.
The right mindset is to think about where a request actually spends its time. Every millisecond falls into one of a few buckets: time on the network moving bytes, time waiting on the database or a downstream service, time doing CPU work like serialization, and time blocked on I/O. Each performance technique in this guide attacks one of those buckets. You do not apply all of them blindly — you measure to find which bucket dominates, then reach for the lever that drains it. But knowing the full toolkit is what lets you diagnose quickly instead of guessing.
Send less: pagination and compression
The cheapest byte to transfer is the one you never send, so the first family of optimizations is about shrinking the response.
Pagination is non-negotiable for any collection that can grow. An endpoint that serializes an unbounded list gets steadily slower and heavier as data accumulates, until one day it exhausts memory or times out. Returning bounded pages — cursor-based for stability, offset-based for simplicity — caps the work per request so latency stays flat regardless of dataset size. It is both a performance fix and a stability guarantee.
Compression attacks the bytes that remain. Text formats like JSON compress extremely well, and enabling gzip or brotli at the server or CDN typically shrinks a response by 60–80% for negligible CPU cost — an enormous win on slow or mobile networks where transfer time dominates. Pair it with field trimming: let clients request only the fields they need (?fields=id,name) instead of shipping every column of every row. Less data on the wire means lower latency, less bandwidth billed, and faster parsing on the client side.
Do less repeated work: caching
The single highest-leverage optimization in most systems is caching, because the fastest possible work is work you never repeat. A cache keeps a copy of a result close and cheap to retrieve, so a request that would have cost a slow database query or an expensive computation instead returns in microseconds.
Caching is not one thing but a series of layers between the client and your source of truth:
- Client and browser caching — with proper
Cache-ControlandETagheaders, the client reuses a response without any network call at all. - CDN caching — static assets and slow-changing responses are served from edge locations physically near the user, cutting both latency and origin load.
- Server-side caching — an in-memory store like
Redisholds hot query results, session data, and computed objects, so repeated reads never touch the database.
The metric that matters is the hit ratio: the fraction of requests a cache satisfies. Even a modest hit ratio removes a large share of load from your slowest, hardest-to-scale components. The cost is staleness — a cached copy can disagree with its source — so you bound it with TTLs and invalidate on writes where correctness demands it.
Talk to the database better: pooling and N+1
Database interaction is where most APIs actually spend their time, and two techniques dominate here.
The first is connection pooling. Establishing a database connection — with its TCP and often TLS handshake — costs a full round-trip, and doing that on every single request is enormously wasteful. A connection pool keeps a set of already-established connections warm and lends them to incoming requests, so the handshake is paid once and amortized across thousands of calls. The same principle applies to HTTP connections to downstream services: reuse keep-alive connections instead of reopening them. On a database-heavy endpoint, pooling alone often halves latency.
The second, and frequently the biggest single win, is eliminating the N+1 query problem. It hides inside innocent-looking code: you fetch a list of N orders, then loop over them and issue one more query per order to load its customer — N + 1 separate round-trips for what should be one operation. Because each round-trip carries network and query overhead, this scales terribly. The fix is to fetch everything in one query using a JOIN or a batched WHERE id IN (...) clause, collapsing N + 1 trips into a single one. ORMs make N+1 easy to introduce by accident, which is exactly why profiling a slow endpoint so often reveals it as the culprit.
Overlap and streamline: async, batching, serialization
The last group of levers is about doing work concurrently and cutting per-request overhead.
Async and non-blocking processing recognizes that not everything a request kicks off needs to complete before you respond. Sending a confirmation email, generating a report, re-indexing for search — these can be pushed onto a background queue so the API returns immediately and the slow work happens out of band. Underneath, a non-blocking (async) server model lets a thread that would otherwise sit idle waiting on I/O go serve another request, dramatically raising throughput under concurrent load.
Batching collapses many small operations into one. Instead of forcing clients to make a hundred single-item calls, expose a bulk endpoint; instead of hitting a downstream service once per item, group the calls; instead of a hundred individual inserts, do one batch write. Each avoided round-trip is pure latency saved.
Finally, serialization — turning objects into bytes and back — is real CPU work at scale. Use an efficient JSON library, and for high-volume internal service-to-service traffic consider a compact binary format like Protobuf, which is smaller and faster to encode than JSON. None of these late-stage optimizations matter until the obvious ones are done, but once you've paginated, cached, pooled, and killed N+1, they're what shave off the remaining overhead. The overall discipline never changes: measure first, fix the dominant bottleneck, then measure again.
Paginate large results so you never serialize and ship an unbounded collection.
Cache at every layer — client, CDN, and server (Redis) — so repeated reads skip the expensive work.
Compress payloads (gzip/brotli) and trim fields to shrink bytes on the wire.
Pool and reuse connections to the database and downstream services instead of paying setup cost per request.
Eliminate N+1 queries, offload slow work to async jobs, and batch many small operations into one.
Quick quiz
What is an N+1 query problem?
Why does a connection pool improve latency?
Where do we cache — and how it goes wrong
Caching lives at every layer. So do the ways it can bite you.
How to load websites at lightning speed
The bytes you never send, the trips you never make: a tour of every layer that makes a page feel instant.
How to debug a slow API
Measure before you guess: a disciplined walk down the request path to find where the milliseconds go.
API design best practices
The conventions that make an API feel obvious instead of hostile.