Background Jobs, Email & Ops
Real apps do work off the request - sending emails, processing tasks, running scheduled jobs. Larz has background jobs, a scheduler, caching and email built in, so your app stays responsive.
A responsive SaaS doesn't make users wait while it sends an email or crunches a report - it does that work in the background. And it runs scheduled jobs (nightly cleanups, reminders), caches hot data, and sends transactional email. Larz's ops layer gives you all of this built in, so your app stays fast and does the behind-the-scenes work a real product needs.
Do slow work off the request
Wrap slow work in a background job so the user gets an instant response while the work happens behind the scenes.
import larz.ops as ops
@app.job # runs in the background
def send_welcome(email):
ops.send_email(smtp, email, 'Welcome!', body)
@app.post('/signup')
def signup(req):
user = create_user(req)
send_welcome(user.email) # returns instantly; email sends in background
return 'Check your inbox'| Need | Larz primitive |
|---|---|
| Slow work off the request | @app.job (background jobs) |
| Scheduled/recurring tasks | @app.schedule('0 3 * * *') |
| Speed up hot routes | @app.cache(ttl=60) |
| Transactional email | ops.send_email (SMTP) |
These keep the request fast and handle the behind-the-scenes work - the stuff that makes an app feel professional (welcome emails, receipts, reminders).
Schedule, cache, email
Recurring jobs, caching and email are one decorator or call each.
@app.schedule('0 9 * * 1') # every Monday 9am
def weekly_digest(): send_digests()
@app.cache(ttl=60) # cache a read-heavy route for 60s
@app.get('/leaderboard')
def board(req): return compute_board()Add the behind-the-scenes layer
- @app.job - move slow work off the request; keep responses instant.
- @app.schedule - recurring tasks (with a leader lock in production).
- @app.cache - speed up read-heavy routes.
- ops.send_email - welcome mails, receipts, reminders.
๐ฏ Key takeaways
- Responsive apps do slow work (email, processing) off the request in background jobs.
- Larz has @app.job, @app.schedule, @app.cache and ops.send_email built in.
- This handles the behind-the-scenes work that makes an app feel professional.
- Watch the multi-worker gotcha: gate schedules behind a leader lock, or use larztask (larz[ops]) at scale.
Frequently asked
Why not just do the work in the request - why background jobs?
Because slow work in the request makes users wait and ties up your server. If sending a welcome email, generating a PDF, or processing an upload happens inline, the user stares at a spinner and one slow operation can block a worker from serving others. Moving it to a background job lets you return an instant response ('Check your inbox') while the work happens behind the scenes, keeping the app fast and scalable. It's the difference between an app that feels snappy and professional and one that feels sluggish - and Larz makes it a single @app.job decorator.
What's the multi-worker gotcha with scheduled jobs?
In production you typically run several server processes (gunicorn workers) for concurrency, and Larz's in-process scheduler runs inside each one - so a task scheduled for 3am would fire once per worker, meaning your nightly job could run several times. The fixes are standard: designate one process to run schedules, or gate each scheduled task behind a database lock so only the first worker to grab it executes. For higher volume or reliability, move to a durable queue - the stack's larztask (via the larz[ops] extra) uses atomic file/DB claims that are safe across many workers. It's a normal distributed-systems detail, just worth knowing before you deploy.
Related lessons
Admin Panel & Revenue Dashboard
You need to see and manage what's happening - your data and your money. Larz auto-generateโฆ
Build a SaaS with LarzShip It to Production
A SaaS on your laptop isn't a business. This session takes it live: a real server, real paโฆ
Build a SaaS with LarzPlans, Trials & Subscriptions
This is where a SaaS makes money - and where Larz shines. A free tier, a paid Pro plan, frโฆ