Home Projects Portfolio Dashboard Export PDF Log in

Dispelling the Serverless Database Connection Myth: A Real-World Load Test with Neon and Vercel

The promise of serverless computing is immense: scale on demand, pay-per-execution, and abstract away infrastructure. However, a common concern frequently arises when pairing serverless functions with traditional relational databases like PostgreSQL: connection pooling. The fear is that each short-lived lambda instance will open its own database connection, quickly exhausting the database's connection limits and leading to performance bottlenecks or outright failures. We recently tackled this challenge head-on while optimizing the backend for our Hidrobombas CMMS project.

The Serverless Database Connection Dilemma

Traditional applications often manage a fixed pool of database connections, reusing them efficiently. In a serverless environment, where functions spin up and down rapidly, this model breaks down. If every function invocation creates a new TCP connection, even a moderately busy application could overwhelm the database.

Our initial hypothesis for the Hidrobombas CMMS project was that, despite using modern serverless-first database solutions, the underlying connection pooling mechanism might still be a limiting factor. We needed empirical evidence to either confirm or refute this.

Our Hypothesis and Staging Setup

To test our hypothesis, we set up a staging environment mirroring our production architecture: Vercel serverless functions backed by a Neon PostgreSQL branch. Critically, Neon provides a serverless driver that operates over WebSockets (port 443), designed specifically for this ephemeral function model. We leveraged k6 for load testing to simulate concurrent users and high write throughput.

Our staging environment was carefully isolated:

  • An empty database with no real data.
  • Its own JWT_SECRET to prevent token cross-over.
  • No REDIS_URL configured.
  • Protected by Vercel Authentication, with a bypass secret for k6.

Surprising Results: WebSocket to the Rescue

Our load test pushed the system with 200 concurrent virtual users and approximately 90 writes per second. The results were clear and surprising: zero 5xx errors, zero timeouts, and zero connection errors. The connection pool was not exhausted.

This outcome directly refuted our initial hypothesis. The key enabler was the serverless driver's WebSocket-based approach. Unlike traditional TCP connections, it doesn't maintain a persistent, live TCP connection per lambda instance. This design fundamentally changes how connections are managed, allowing the system to scale effectively without hitting connection limits.

Furthermore, the deployed write throughput (~90 writes/s) actually exceeded our local development environment (~70 writes/s). This is attributable to Vercel's ability to provision more parallel instances than a single local machine.

While scaling improved throughput, we did observe an increase in latency, roughly five times higher uniformly (e.g., 19ms locally to 231ms on staging for reads). This is a common and expected trade-off in cloud environments, primarily due to network overhead and lambda cold starts, rather than database contention.

Ensuring Isolation and Testability with k6

For k6 testing, maintaining environment integrity and test flexibility was crucial. We centralized HTTP headers in a headers() function, allowing for dynamic token injection or omission:

// In k6 script
export function headers(token = null) {
  const commonHeaders = {
    'Content-Type': 'application/json',
    'X-Vercel-Auth-Bypass': __ENV.VERCEL_BYPASS_SECRET // For Vercel Auth
  };

  if (token) {
    commonHeaders['Authorization'] = `Bearer ${token}`;
  }

  return commonHeaders;
}

// Example usage for an authenticated request
const authenticatedRes = http.get(`${__ENV.API_URL}/secure-endpoint`, { headers: headers(authToken) });

// Example usage for an anonymous request (e.g., login or smoke test)
const anonymousRes = http.post(`${__ENV.API_URL}/login`, JSON.stringify(loginPayload), { headers: headers() });

This approach ensured that anonymous requests (like login or expected 401 smoke tests) received the actual API response instead of a 302 redirect to Vercel's SSO, which would have complicated test validation.

Conclusion

Our load testing unequivocally demonstrated that the Hidrobombas CMMS backend, leveraging Vercel and Neon's serverless PostgreSQL driver via WebSockets, exhibits robust scalability. The "connection pool exhaustion" concern for serverless functions with relational databases, at least with this architecture, proved unfounded. The system can handle orders of magnitude more load than current project needs, eliminating any lingering performance debt.

This experience highlights the importance of real-world load testing in serverless environments and trusting the design principles of serverless-native database solutions.


Generated with Gitvlg.com

Dispelling the Serverless Database Connection Myth: A Real-World Load Test with Neon and Vercel
Luis Romero

Luis Romero

Author

Share: