0
0
Flaskframework~8 mins

Email verification pattern in Flask - Performance & Optimization

Choose your learning style9 modes available
Performance: Email verification pattern
MEDIUM IMPACT
This pattern affects page load speed and interaction responsiveness during user registration and verification steps.
Verifying user email during registration without blocking UI
Flask
from flask import Flask, request, jsonify
from threading import Thread
app = Flask(__name__)

def send_verification_email_async(email):
    # simulate sending email
    import time
    time.sleep(5)
    print(f'Verification email sent to {email}')

@app.route('/register', methods=['POST'])
def register():
    email = request.form['email']
    Thread(target=send_verification_email_async, args=(email,)).start()
    return jsonify({'message': 'Registration complete, verification email sent asynchronously'})
Sends email in background thread, allowing immediate HTTP response and smooth UI interaction.
📈 Performance GainNon-blocking request, reduces user wait time by 5 seconds, improves INP significantly.
Verifying user email during registration without blocking UI
Flask
from flask import Flask, request
import time
app = Flask(__name__)

@app.route('/register', methods=['POST'])
def register():
    email = request.form['email']
    # Simulate sending email synchronously
    send_verification_email(email)  # blocks request until done
    return 'Registration complete, check your email!'

def send_verification_email(email):
    time.sleep(5)  # simulate delay
    print(f'Verification email sent to {email}')
Sends verification email synchronously, blocking the HTTP request and delaying response to user.
📉 Performance CostBlocks rendering and response for 5 seconds, causing poor INP and user frustration.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Synchronous email sendingMinimalN/ABlocks paint until response[X] Bad
Asynchronous email sendingMinimalN/AImmediate paint and interaction[OK] Good
Rendering Pipeline
Synchronous email sending blocks server response, delaying browser rendering and interaction readiness. Asynchronous sending frees server to respond immediately, allowing browser to paint and become interactive faster.
Network
JavaScript Execution
Rendering
⚠️ BottleneckNetwork and server processing blocking response
Core Web Vital Affected
INP
This pattern affects page load speed and interaction responsiveness during user registration and verification steps.
Optimization Tips
1Never block HTTP responses with long-running tasks like sending emails.
2Use background threads or task queues to handle email sending asynchronously.
3Test user interaction speed with DevTools Performance panel to catch blocking tasks.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance problem with sending verification emails synchronously during registration?
AIt increases CSS paint time
BIt blocks the server response, delaying page interaction
CIt causes layout shifts on the page
DIt reduces image loading speed
DevTools: Performance
How to check: Record a performance profile while submitting registration form; look for long tasks blocking main thread and delayed Time to Interactive.
What to look for: Long blocking tasks during request and delayed user interaction readiness indicate synchronous blocking.