Console Login
Home / Blog / DevOps & Infrastructure / Stop Blaming the Database: Tuning Your API Gateway for Heavy Loads
DevOps & Infrastructure 0 views

Stop Blaming the Database: Tuning Your API Gateway for Heavy Loads

@

The "Microservices" Hangover

It’s 2015, and the monolith is dead—or so the conference speakers keep telling us. Everyone is rushing to break their PHP and Java applications into decoupled APIs. It sounds great until you realize you've just replaced function calls with HTTP requests. Suddenly, your latency isn't measured in memory access times; it's measured in network round-trips.

I've spent the last month debugging a "slow" Magento backend for a client in Oslo. The developers were convinced it was a MySQL deadlock. It wasn't. The database was sleeping. The bottleneck was their API Gateway—a default Nginx installation choking on TCP handshakes.

If you are deploying APIs on a Virtual Private Server (VPS) targeting Norwegian users, you cannot rely on defaults. Here is how we tune the stack to handle thousands of concurrent connections without melting the CPU.

1. The Kernel is Your Gatekeeper

Most Linux distributions ship with conservative defaults intended for desktop usage, not high-throughput API serving. When you have an API gateway receiving thousands of requests per second, you will run out of ephemeral ports fast.

Open your /etc/sysctl.conf. We need to widen the port range and enable reuse for connections in the TIME_WAIT state.

# Increase system-wide file descriptors
fs.file-max = 2097152

# Widen the port range
net.ipv4.ip_local_port_range = 1024 65535

# Reuse sockets in TIME_WAIT state for new connections
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15

Run sysctl -p to apply. Without this, your API gateway will simply drop packets during traffic spikes, leaving your users staring at a spinning loading icon.

2. Nginx: Beyond the Basics

Whether you are using Nginx directly or as the engine behind a tool like Kong (which is looking promising this year), the configuration is often too timid.

Worker Processes & Connections

Ensure your worker_processes is set to auto (available in recent versions like 1.7.x+), binding processes to CPU cores. But the real killer is worker_connections. The default of 768 is laughable for an API gateway.

events {
    worker_connections 10240;
    use epoll;
    multi_accept on;
}

Keepalive is Mandatory

SSL handshakes are expensive. In 2015, with 2048-bit RSA keys becoming standard, you do not want to negotiate a handshake for every single API call. Use upstream keepalives.

upstream backend_api {
    server 10.0.0.5:8080;
    keepalive 64;
}

3. The Hardware Reality: "Steal Time" Kills APIs

You can tune your config all day, but if your underlying host is oversubscribing CPUs, you are fighting a losing battle. In a virtualized environment, Steal Time (%st) is the percentage of time your virtual CPU waits for the physical CPU to serve it.

If you run top and see %st above 5.0, your provider is jamming too many tenants onto one server. For an API gateway, where processing latency needs to be consistent, this jitter is unacceptable.

Pro Tip: Avoid OpenVZ or container-based VPS for critical gateways. They often share kernel resources too aggressively. We use KVM virtualization exclusively at CoolVDS because it provides hardware-level isolation. Your RAM is yours. Your CPU cycles are yours.

4. Latency and Geography: The Oslo Advantage

If your users are in Norway, why is your server in Frankfurt? Light speed is a physical limit. Round-trip time (RTT) from Oslo to Frankfurt is roughly 25-30ms. From Oslo to Oslo, via the NIX (Norwegian Internet Exchange), it is under 3ms.

For an API that makes multiple internal calls, that 25ms delay compounds. Hosting locally isn't just about patriotism; it's about physics. Furthermore, with the Datatilsynet becoming increasingly strict about data sovereignty and the uncertainty surrounding the Safe Harbor agreement this year, keeping data on Norwegian soil is the safest bet for compliance.

5. Storage I/O: The Hidden Bottleneck

APIs log heavily. Access logs, error logs, audit trails. On a standard SATA drive, high-concurrency logging can cause I/O wait (iowait) that blocks the CPU.

We recently benchmarked standard SSDs against the new PCIe-based storage solutions. The difference in write latency is staggering. While standard SSDs are "good enough" for static sites, high-traffic APIs require the low latency of enterprise flash storage. This is why CoolVDS has rolled out high-performance storage tiers specifically for I/O heavy workloads.

Summary

Building a robust API gateway in 2015 requires more than just deploying code. It requires a holistic view of the stack:

  • Kernel: Tune sysctl to handle high connection churn.
  • Nginx: Maximize file descriptors and enable upstream keepalives.
  • Infrastructure: Demand KVM isolation to avoid noisy neighbors.
  • Network: Host close to your users to minimize RTT.

Don't let your infrastructure be the reason your microservices architecture fails. If you need a sandbox to test these configurations without the noisy neighbor effect, spin up a KVM instance on CoolVDS today.

/// TAGS

/// RELATED POSTS

Building a CI/CD Pipeline on CoolVDS

Step-by-step guide to setting up a modern CI/CD pipeline using Firecracker MicroVMs....

Read More →

Latency is the Enemy: Why Centralized Architectures Fail Norwegian Users (And How to Fix It)

In 2015, hosting in Frankfurt isn't enough. We explore practical strategies for distributed infrastr...

Read More →

Docker in Production: Security Survival Guide for the Paranoia-Prone

Containerization is sweeping through Norwegian dev teams, but the default settings are a security ni...

Read More →

Stop Using Ping: A Sysadmin’s Guide to Infrastructure Monitoring at Scale

Is your monitoring strategy just a cron job and a prayer? In 2015, 'uptime' isn't enough. We explore...

Read More →

The Truth About "Slow": A SysAdmin’s Guide to Application Performance Monitoring in 2015

Uptime isn't enough. Discover how to diagnose high latency, banish I/O wait time, and why KVM virtua...

Read More →

The CTO’s Guide to Cloud Economics: Reducing TCO Without Choking I/O in Norway

Is your monthly infrastructure bill scaling faster than your user base? We dissect the hidden costs ...

Read More →
← Back to All Posts