Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is connection pooling in the context of serverless applications?
Connection pooling is a technique to reuse database connections instead of opening a new one for each request. This helps reduce latency and resource use, especially important in serverless where functions start and stop frequently.
Click to reveal answer
intermediate
Why is connection pooling challenging in serverless environments like Next.js API routes?
Serverless functions can start many instances quickly and shut down after use, making it hard to maintain persistent connections. Without pooling, each function might open a new database connection, causing overload.
Click to reveal answer
intermediate
How can you implement connection pooling in Next.js API routes?
You can create a global connection pool outside the handler function. This way, the pool persists between function invocations if the serverless environment reuses the instance, reducing new connections.
Click to reveal answer
beginner
What is a common library used for connection pooling with PostgreSQL in Next.js?
The 'pg' library with its Pool class is commonly used. It manages a pool of connections that can be shared across requests in serverless functions.
Click to reveal answer
beginner
What is a key benefit of using connection pooling in serverless apps?
It reduces the time and resources needed to connect to the database on each request, improving performance and avoiding connection limits.
Click to reveal answer
Why is opening a new database connection on every serverless function call a problem?
AIt reduces memory usage on the server.
BIt improves security by isolating connections.
CIt can exhaust database connection limits and slow down responses.
DIt makes the code simpler to write.
✗ Incorrect
Opening new connections each time can overload the database and increase latency.
Where should you place the connection pool in a Next.js API route for serverless?
AOutside the handler function, at the module level.
BInside the handler function.
CInside a client-side React component.
DIn a separate CSS file.
✗ Incorrect
Placing the pool outside the handler allows reuse across function calls if the instance is reused.
Which library is commonly used for PostgreSQL connection pooling in Next.js?
Amongoose
Bpg
Caxios
Dexpress
✗ Incorrect
'pg' provides a Pool class for managing PostgreSQL connections.
What happens if you don't use connection pooling in serverless functions?
AEach function call opens a new connection, risking overload.
BThe serverless function runs faster.
CDatabase connections are reused automatically.
DThe database becomes read-only.
✗ Incorrect
Without pooling, many connections open and close rapidly, causing problems.
What is a benefit of connection pooling in serverless apps?
ASlower response times.
BIncreased serverless function cold starts.
CMore database connections used.
DReduced latency and resource use.
✗ Incorrect
Pooling reduces the overhead of opening new connections, improving speed and efficiency.
Explain how connection pooling works in serverless Next.js API routes and why it is important.
Think about how serverless functions start and stop and how pooling helps.
You got /5 concepts.
Describe a simple way to implement connection pooling with PostgreSQL in a Next.js serverless function.
Focus on where to create the pool and how to use it in the API route.
You got /4 concepts.
Practice
(1/5)
1. What is the main benefit of using connection pooling in a Next.js serverless app?
easy
A. It automatically scales the number of serverless functions.
B. It reuses database connections to improve speed and avoid connection limits.
C. It caches API responses for faster loading.
D. It encrypts database connections for security.
Solution
Step 1: Understand connection pooling purpose
Connection pooling allows reusing existing database connections instead of opening new ones each time.
Step 2: Identify benefits in serverless context
This reuse improves speed and prevents hitting database connection limits common in serverless environments.
Final Answer:
It reuses database connections to improve speed and avoid connection limits. -> Option B
Quick Check:
Connection pooling = reuse connections [OK]
Hint: Pooling means reusing connections to avoid limits [OK]
Common Mistakes:
Confusing pooling with caching data
Thinking pooling scales serverless functions
Assuming pooling encrypts connections
2. Which code snippet correctly creates a MySQL connection pool using mysql2/promise in Next.js?
easy
A. const pool = mysql2.createPool({ host: 'localhost', user: 'root', database: 'test' }).promise();
B. const pool = mysql2.promise.createPool({ host: 'localhost', user: 'root', database: 'test' });
C. const pool = mysql.createPool({ host: 'localhost', user: 'root', database: 'test' });
D. const pool = mysql2/promise.createPool({ host: 'localhost', user: 'root', database: 'test' });
Solution
Step 1: Recall mysql2/promise usage
When importing mysql from 'mysql2/promise', mysql.createPool() directly creates a promise-based pool.
Step 2: Match correct syntax
const pool = mysql.createPool({ host: 'localhost', user: 'root', database: 'test' }); correctly uses the mysql2/promise import.
Final Answer:
const pool = mysql.createPool({ host: 'localhost', user: 'root', database: 'test' }); -> Option C
Hint: mysql from 'mysql2/promise'; mysql.createPool() [OK]
Common Mistakes:
Trying to call createPool directly on mysql2/promise import
Missing .promise() for async support
Using wrong import or syntax
3. Given this Next.js API handler using a PostgreSQL pool, what will be the output if the database connection fails?
import { Pool } from 'pg';
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
export default async function handler(req, res) {
try {
const client = await pool.connect();
const result = await client.query('SELECT NOW()');
client.release();
res.status(200).json({ time: result.rows[0].now });
} catch (error) {
res.status(500).json({ error: 'Database connection failed' });
}
}
medium
A. Returns JSON with error message 'Database connection failed'.
B. Returns empty JSON object {}.
C. Throws an unhandled exception crashing the server.
D. Returns JSON with current time from database.
Solution
Step 1: Analyze try-catch behavior
If pool.connect() fails, the code jumps to the catch block.
Step 2: Check catch block response
The catch block sends a 500 status with JSON error message 'Database connection failed'.
Final Answer:
Returns JSON with error message 'Database connection failed'. -> Option A
Quick Check:
Error caught = JSON error response [OK]
Hint: Errors in try send JSON error response [OK]
Common Mistakes:
Assuming unhandled exception crashes server
Expecting successful time JSON on failure
Thinking empty JSON is returned
4. Identify the bug in this Next.js serverless function using MySQL connection pooling:
import mysql from 'mysql2/promise';
const pool = mysql.createPool({ host: 'localhost', user: 'root', database: 'test' });
export default async function handler(req, res) {
const connection = await pool.getConnection();
const [rows] = await connection.query('SELECT * FROM users');
res.status(200).json(rows);
}
medium
A. Missing connection.release() after query.
B. Using getConnection() instead of connect().
C. Pool should be created inside the handler.
D. Query syntax is incorrect.
Solution
Step 1: Check connection usage
The code gets a connection from the pool but never releases it back.
Step 2: Understand pooling best practice
Connections must be released with connection.release() to avoid leaks and exhaustion.
Final Answer:
Missing connection.release() after query. -> Option A
Quick Check:
Always release pooled connections [OK]
Hint: Always release connections after use [OK]
Common Mistakes:
Forgetting to release connections
Thinking getConnection() is invalid
Creating pool inside handler causing overhead
5. You want to optimize a Next.js serverless app connecting to PostgreSQL with connection pooling. Which approach best prevents exhausting database connections during high traffic?
hard
A. Close the pool after each query to free resources.
B. Create a new pool inside each API handler call.
C. Use a new client connection for every query without pooling.
D. Create a single global pool instance reused across requests.