DevForth

Blocking vs asynchronous code, explained simply

Blocking code stands and waits, async code works on something else in the meantime. Why that matters, and why neither one fixes a slow query.

Ivan Borshchov
Ivan Borshchov
CEO & System Architect @ DevForth
Blocking vs asynchronous code, explained simply
Blocking vs asynchronous code, explained simply

Picture a café with one waiter.

Blocking: the waiter takes your order, walks to the kitchen and stands there until the soup is ready. The next customer waits at the door the whole time, even though the waiter is doing nothing.

Asynchronous: the waiter hands your order to the kitchen and goes back to take the next one. When the soup is ready, the kitchen rings a bell and the waiter brings it over.

In code, the "kitchen" is anything slow that isn't your own program: a database query, a call to another API, reading a file. A blocking server, such as a classic Django or Flask app on Gunicorn's sync workers, handles one request per worker, so you add workers or threads to serve more people. An async server (Node.js, or Python with asyncio and FastAPI) starts the slow call, serves other requests while it waits, and picks the work up again when the answer arrives.

import asyncio

async def load_dashboard():
    users, orders = await asyncio.gather(
        fetch_users(),
        fetch_orders(),
    )

Both calls wait at the same time, so this takes as long as the slower one, not as long as both added together.

Where async doesn't help

Async only helps with waiting. If the kitchen itself is slow, a faster waiter changes nothing. A query that takes one second still keeps the database busy for one second, and many of them at once will overload it, whichever style your app uses. Heavy CPU work like resizing images or big calculations also blocks an async server, because there is no waiting to hand off. Move that work to a separate worker or a job queue.

So when an app feels slow, measure first. Often the fix is an index or a better query rather than a new programming model. We cover that in SQL query optimization: understanding the key principle.

In short: async lets one worker serve many requests while they wait, but it can't make the slow thing itself any faster.

Get your project quote

Free result within 48-72 hours

Related insights