0
0
NextJSframework~20 mins

Connection pooling for serverless in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Serverless Connection Pooling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use connection pooling in serverless Next.js apps?

In serverless environments like Next.js API routes, why is connection pooling important?

AIt reduces the number of database connections by reusing existing ones, improving performance and avoiding connection limits.
BIt creates a new database connection for every request to ensure data freshness.
CIt caches database queries on the client side to reduce server load.
DIt automatically scales the database server horizontally without configuration.
Attempts:
2 left
💡 Hint

Think about how serverless functions start and stop frequently and how databases limit connections.

component_behavior
intermediate
2:00remaining
What happens when you don't use connection pooling in Next.js API routes?

Consider a Next.js API route that opens a new database connection on every request without pooling. What is the most likely outcome under heavy traffic?

AThe database will reject all queries due to slow network latency.
BThe serverless functions will cache connections automatically, so no problem occurs.
CThe app will run smoothly with no issues because serverless functions are stateless.
DThe database will quickly reach its maximum connection limit, causing errors and failed requests.
Attempts:
2 left
💡 Hint

Think about how many connections a database can handle and what happens if too many open at once.

📝 Syntax
advanced
2:00remaining
Identify the correct way to implement connection pooling in a Next.js API route

Which code snippet correctly implements connection pooling using a global variable to reuse a single database client instance in Next.js API routes?

NextJS
import { Client } from 'pg';

let client;

export default async function handler(req, res) {
  if (!client) {
    client = new Client({ connectionString: process.env.DATABASE_URL });
    await client.connect();
  }
  const result = await client.query('SELECT NOW()');
  res.status(200).json({ time: result.rows[0].now });
}
AThe code creates a new client on every request without reusing, causing connection overload.
BThe code correctly reuses the client connection across requests by storing it in a global variable.
CThe code uses a client but never calls connect(), so queries will fail.
DThe code closes the client after each request, preventing reuse.
Attempts:
2 left
💡 Hint

Look for where the client is created and if it is reused across requests.

🔧 Debug
advanced
2:00remaining
Why does this Next.js API route cause 'too many clients' error?

Given this code, why might the app throw a 'too many clients' error under load?

NextJS
import { Pool } from 'pg';

export default async function handler(req, res) {
  const pool = new Pool({ connectionString: process.env.DATABASE_URL });
  const client = await pool.connect();
  const result = await client.query('SELECT NOW()');
  client.release();
  res.status(200).json({ time: result.rows[0].now });
}
AThe environment variable DATABASE_URL is missing, causing connection failures.
BThe query syntax is incorrect, causing the database to reject connections.
CA new Pool instance is created on every request, so connections are not reused, causing overload.
DThe client is never released back to the pool, causing connection leaks.
Attempts:
2 left
💡 Hint

Check where the Pool is created and how often.

state_output
expert
2:00remaining
What is the output of this Next.js API route with connection pooling under concurrent requests?

Consider this Next.js API route code. What will be the output when 3 requests arrive almost simultaneously?

NextJS
import { Pool } from 'pg';

const pool = new Pool({ connectionString: process.env.DATABASE_URL, max: 2 });

export default async function handler(req, res) {
  const client = await pool.connect();
  const result = await client.query('SELECT pg_sleep(1); SELECT NOW()');
  client.release();
  res.status(200).json({ time: result[1].rows[0].now });
}
ATwo requests will complete after about 1 second; the third will wait for a connection and complete after about 2 seconds.
BAll three requests will complete simultaneously after 1 second because the pool allows unlimited connections.
CThe third request will fail immediately with a connection error because the pool max is 2.
DAll requests will fail because pg_sleep is not supported in queries.
Attempts:
2 left
💡 Hint

Think about the pool max connections and how pg_sleep delays queries.