Flask · Celery · Redis · Python
Adding a Queue Didn't Kill My Monolith
A 150-line Flask + Celery + Redis prototype to learn task queues before my personal assistant needs them.

A small Flask app, a Redis instance and a Celery worker, about 150 lines in total, built to learn one pattern before I need it: the task queue. This post covers what the prototype does, why the worker is still part of the monolith, and why none of this is going into my personal assistant yet.
Why a toy project
I'm building a prototype of a personal assistant. It has a chat, and at some point it will need to run tasks that take minutes without freezing the conversation. The textbook answer is a queue, a worker and retries. The honest answer is that queues, failover and retries solve scale problems, and the assistant doesn't have one yet: one user, short tasks.
So the queue went into a separate project. The goal was to get familiar with how it behaves, so that when the assistant actually needs it, I already know how it works and where it breaks.
The flow
browser ──POST /tasks──▶ Flask ──.delay()──▶ Redis (queue)
▲ │ replies "ID xyz" right away │
│ │ ▼
└──GET /tasks/xyz───────┘◀── status / result ──── Celery worker
(polling every 1s)
Flask never does the heavy work inside the request. It puts the task on the queue, returns 202 Accepted with an ID in a few milliseconds, and from then on only answers "how is task X doing?". The worker takes tasks from Redis, runs them, and writes progress and results back to Redis. The page polls every second and shows PENDING, PROGRESS 3/5, RETRY, SUCCESS.
The task itself is fake on purpose: it sleeps for N seconds, reports progress each second, and fails at random with a probability you choose. A real one would call a model, render a PDF or transcode audio. The plumbing is the same.
Still a monolith
I almost got this wrong. My first sketch had the worker as a separate FastAPI service. It isn't one. A Celery worker serves no HTTP. It's another process started from the same code: tasks.py is imported by Flask, which only calls .delay(), and by the worker, which runs the function.
One repository, one codebase, one deploy, two processes. That's still a monolith. A queue doesn't turn a system into microservices. It moves slow work off the request path.
Retries are a decorator
@celery_app.task(bind=True, max_retries=3, default_retry_delay=2)
def process(self, text, seconds=5, failure_chance=0.0):
...
if random.random() < failure_chance:
raise self.retry(exc=RuntimeError("simulated failure"))
Set the failure chance to 0.7 and the worker log shows attempt one fail, attempt two fail, attempt three succeed, or the whole task ending in FAILURE after the limit. The web side does nothing different. It just sees RETRY for a while.
The experiments
Most of what I learned came from these, not from writing the code:
- Instant response. A 10-second task gets an HTTP response in a few milliseconds. The user waits for the result, not for the request.
- A real queue. Five clicks with
--concurrency=2: two run, three sit inPENDING. - Scaling sideways. A second worker in another terminal drains the queue faster, and Flask doesn't change at all.
- Retry. As above.
- Worker down. Stop the worker, queue tasks, start it again: they were waiting in Redis and run. One catch: with Celery's defaults a task is acknowledged when it starts, so a task that is running when the worker dies is lost.
acks_late=Truechanges that, but then the task can run twice and has to be safe to repeat. - Redis down. Stop Redis and try to create a task. The request hangs and then errors. The single Redis is now the single point of failure for everything asynchronous, and that's the problem failover exists to solve.
Experiment 6 showed me that adding a queue doesn't remove a failure point. It replaces one.
What is not there
No authentication. Results expire from Redis after an hour, and nothing is saved anywhere else. Polling instead of WebSockets. No failover, no monitoring (Flower would be the next step), no idempotency. All deliberate: each one adds complexity this prototype doesn't need.
Status
Runs locally with three terminals: Valkey (Arch's Redis fork, same protocol), the worker and Flask. It's a study project and isn't headed for production. When the assistant gets more users or tasks that take minutes, this is the pattern I'll implement there, and I'll have already broken it on purpose six ways. Code at Arquitetura-Monolitica.
Join the builders
New projects, the code behind them and what broke on the way. One e-mail when there is something worth opening.
We send a link to confirm. Leave anytime with one click. Never sold or shared. How we handle it
Keep reading
Adobe Acrobat Pro 2020 perpetual license stops activating
Acrobat Pro 2020 asks for a serial and fails to activate. What Adobe has said, what Hacker News thinks, and what to do with your PDFs.
Bayer's Feed a Bee campaign and the bee protection fight
A new investigation says Bayer's Feed a Bee PR was tied to work against pesticide laws. What it shows, Bayer's reply and lessons for small brands.
Bitcoin and AI cryptography fears: what is proven
Bitcoin fell about 10% after AI and NSA warnings on cryptography. What is proven, what is feared, and which coins are really exposed.
Comments
No comments yet. Start the conversation.
Sign in with Google to comment
Signing in keeps out bots and spam. Only your Google name and photo appear next to what you write; your e-mail is never shown.