0
0
Flaskframework~8 mins

Connection pooling in Flask - Performance & Optimization

Choose your learning style9 modes available
Performance: Connection pooling
HIGH IMPACT
Connection pooling affects how quickly the backend can handle database requests, impacting server response time and user experience.
Managing database connections efficiently in a Flask app
Flask
from flask import Flask
def create_app():
    from sqlalchemy import create_engine
    from sqlalchemy.orm import scoped_session, sessionmaker

    app = Flask(__name__)
    engine = create_engine('sqlite:///database.db', pool_size=5, max_overflow=10)
    db_session = scoped_session(sessionmaker(bind=engine))

    @app.route('/')
    def index():
        data = db_session.execute('SELECT * FROM table').fetchall()
        return str(data)

    @app.teardown_appcontext
    def shutdown_session(exception=None):
        db_session.remove()

    return app
Reuses a pool of persistent connections, reducing connection setup time and server resource use.
📈 Performance GainReduces request latency by tens of milliseconds; lowers CPU and memory usage under load.
Managing database connections efficiently in a Flask app
Flask
from flask import Flask
import sqlite3

app = Flask(__name__)

def get_db_connection():
    conn = sqlite3.connect('database.db')
    return conn

@app.route('/')
def index():
    conn = get_db_connection()
    data = conn.execute('SELECT * FROM table').fetchall()
    conn.close()
    return str(data)
Opening and closing a new database connection on every request causes high latency and resource overhead.
📉 Performance CostBlocks request handling for tens to hundreds of milliseconds per connection; increases server load under traffic.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
No connection pooling (new connection per request)N/AN/AIncreases server response time delaying paint[X] Bad
Connection pooling with SQLAlchemy engineN/AN/AFaster server response enables quicker paint[OK] Good
Rendering Pipeline
Connection pooling reduces backend wait time for database responses, speeding up server response that affects browser rendering start.
Server Processing
Network Request
First Paint
⚠️ BottleneckServer Processing (waiting for DB connection and query execution)
Core Web Vital Affected
INP
Connection pooling affects how quickly the backend can handle database requests, impacting server response time and user experience.
Optimization Tips
1Always use connection pooling to reuse database connections and reduce latency.
2Avoid opening and closing DB connections on every request to prevent server overload.
3Monitor backend response times in DevTools Network panel to verify pooling effectiveness.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using connection pooling in a Flask app?
AReduces latency by reusing existing database connections
BIncreases the number of database connections opened per request
CBlocks rendering until all connections are closed
DImproves CSS rendering speed
DevTools: Network
How to check: Open DevTools Network panel, reload page, and check server response times for API/database calls.
What to look for: Look for reduced backend response times and faster time-to-first-byte indicating efficient DB connection reuse.