HomeBytesheetsRoadmapsUI TemplatesUI ToolsStack Discussion
3 Topics
Chapter•180 Minutes

API Performance and Scalability

Optimize API latency and design for horizontal scale, caching, database efficiency, and controlled resource usage.

Understanding API Latency

Latency Budget

API latency is usually the sum of network, proxy, application, serialization, database, cache, and downstream dependency time.

Do not optimize blindly. Measure where time is spent.

A request taking 500 ms may consist of 20 ms application logic, 300 ms database queries, 100 ms external API calls, and 80 ms network/proxy overhead.

Profiling and tracing reveal the actual bottleneck.

Latency Components

Database Performance from the API Layer

API performance is frequently limited by database access.

Avoid N+1 queries. Select only required fields when possible. Create appropriate indexes. Use pagination. Analyze query plans. Limit expensive joins or aggregations. Cache suitable read-heavy data.

Never solve a database bottleneck by simply increasing the API timeout. A slow query multiplied across hundreds of concurrent requests can exhaust the database connection pool and cause cascading failure.

Beware N+1 Queries

Fetching one collection and then issuing one database query for every returned item can turn a single API request into hundreds of database operations.

Horizontal Scaling and Load Balancing

A stateless API can be replicated across multiple instances behind a load balancer.

The load balancer distributes requests while health checks prevent traffic from reaching unhealthy instances.

Application-local memory should not be treated as shared state in a horizontally scaled service. Shared state belongs in appropriate external systems such as databases, distributed caches, queues, or object storage.

This is one reason REST statelessness is valuable for scalable services.

Horizontally Scaled API