๐Ÿ—๏ธ
๐Ÿ—๏ธ Build a SaaS with Larz ยท LARZ-601 ยท Session 5 of 6

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.

โฑ 8 min read๐Ÿ“… Updated 2026-07-27๐Ÿ“Š Tables, charts & examples

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'
NeedLarz 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 emailops.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()
๐ŸŽฏ
Mind the multi-worker gotcha. Background jobs and schedules run per server-process, so in production (multiple gunicorn workers) a scheduled task can fire multiple times. The fix: run schedules in one dedicated process, or gate them behind a database lock so only one worker runs them. For heavy volume, swap the in-process queue for a Redis-backed one with the same API, or use the stack's larztask (larz[ops]).

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.

Session 5 of 6 complete

Next up: Ship It to Production

Continue to Session 6 โ†’

Related lessons

More in this course

Course home → · Browse all courses →