Latency is the time delay between initiating a request and receiving a response. In API development, it measures how long it takes for data to travel from client to server and back.
Latency
The time delay between a request being sent and a response being received.
Types of Latency
| Type | Description |
|---|---|
| Network Latency | Time for data to travel over the network |
| Server Latency | Time for server to process request |
| Database Latency | Time for database queries |
| Total Latency | End-to-end response time |
Measuring Latency
- P50 (Median): 50% of requests faster than this
- P95: 95% of requests faster than this
- P99: 99% of requests faster than this
- Average: Mean response time (can be misleading)
Factors Affecting Latency
- Geographic distance between client and server
- Network congestion and bandwidth
- Server processing power
- Database query complexity
- Payload size
Reducing Latency
- CDN: Serve content from edge locations
- Caching: Reduce database queries
- Connection Pooling: Reuse database connections
- Async Processing: Move heavy tasks to background
- Compression: Reduce payload size
- HTTP/2: Multiplexing, header compression
Code Examples
Measuring API Latency
async function measureLatency(url) {
const start = performance.now();
await fetch(url);
const end = performance.now();
const latency = end - start;
console.log(`Latency: ${latency.toFixed(2)}ms`);
return latency;
}
// Measure multiple times for accuracy
const results = await Promise.all([
measureLatency("/api/users"),
measureLatency("/api/users"),
measureLatency("/api/users")
]);
const avg = results.reduce((a, b) => a + b) / results.length;
console.log(`Average: ${avg.toFixed(2)}ms`);Related Terms
Server-Sent Events (SSE)
A standard for pushing real-time updates from server to client over HTTP.
Read more
REST API
An architectural style for building web APIs using HTTP methods and stateless communication.
Read more
API Mocking
Simulating API behavior to enable development and testing without real backend services.
Read more
Mock Server
A fake server that simulates API responses for testing and development purposes.
Read more