0
0
Flaskframework~8 mins

Password reset email pattern in Flask - Performance & Optimization

Choose your learning style9 modes available
Performance: Password reset email pattern
MEDIUM IMPACT
This pattern affects page load speed indirectly by impacting server response time and user interaction responsiveness during password reset requests.
Sending password reset emails after user request
Flask
from flask import Flask, request
from flask_mail import Mail, Message
from threading import Thread
app = Flask(__name__)
mail = Mail(app)

def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)

@app.route('/reset-password', methods=['POST'])
def reset_password():
    email = request.form['email']
    token = 'secure-token'
    msg = Message('Reset Your Password', recipients=[email])
    msg.body = f'Click to reset: https://example.com/reset/{token}'
    Thread(target=send_async_email, args=(app, msg)).start()  # Send email asynchronously
    return 'Email sent', 200
Sending email asynchronously frees server to respond immediately, improving interaction responsiveness and user experience.
📈 Performance GainNon-blocking response reduces INP by 500-2000ms, improving perceived speed.
Sending password reset emails after user request
Flask
from flask import Flask, request
from flask_mail import Mail, Message
app = Flask(__name__)
mail = Mail(app)

@app.route('/reset-password', methods=['POST'])
def reset_password():
    email = request.form['email']
    # Generate token here
    token = 'secure-token'
    msg = Message('Reset Your Password', recipients=[email])
    msg.body = f'Click to reset: https://example.com/reset/{token}'
    mail.send(msg)  # Sending email synchronously
    return 'Email sent', 200
Sending email synchronously blocks the server response, causing slow user feedback and poor interaction responsiveness.
📉 Performance CostBlocks server response for 500-2000ms depending on email server, increasing INP and user wait time.
Performance Comparison
PatternServer BlockingUser Wait TimeInteraction ResponsivenessVerdict
Synchronous email sendingBlocks server threadHigh (500-2000ms)Poor (high INP)[X] Bad
Asynchronous email sendingNon-blockingLow (<100ms)Good (low INP)[OK] Good
Rendering Pipeline
Password reset email sending affects server response time, which impacts how quickly the browser can update the UI after user interaction.
Server Processing
Network Response
User Interaction
⚠️ BottleneckServer Processing when sending email synchronously
Core Web Vital Affected
INP
This pattern affects page load speed indirectly by impacting server response time and user interaction responsiveness during password reset requests.
Optimization Tips
1Never send emails synchronously during user requests; use asynchronous methods.
2Offload heavy tasks like email sending to background threads or jobs.
3Monitor server response times to keep interaction responsiveness high.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance problem with sending password reset emails synchronously in Flask?
AIt blocks the server response, increasing user wait time.
BIt increases the bundle size sent to the browser.
CIt causes layout shifts in the browser.
DIt reduces network bandwidth.
DevTools: Network and Performance panels
How to check: Use Network panel to check server response time for password reset request. Use Performance panel to record interaction and see delay before next paint.
What to look for: Look for long server response times and delayed UI updates indicating blocking operations.