Nikita Makovei

Case I · Backpressure in a bulk import

The Flood

A customer uploads a file with a million contacts. The reader is fast, the database is not, and everything between them is memory.

Where
TextMagic
When
2024 – 2025
Pattern
Backpressure in a bulk import

I · Departure

A file larger than the room

TextMagic is a business messaging platform. Before a campaign, a customer uploads a contact list as a CSV or XLSX file. I re-engineered that import to handle more than one million rows a minute, and cut its runtime and CPU usage.

II · Initiation

The reader must learn to wait

Reading a file is cheap. Writing rows to a database is not. If the reader runs at its own speed, the rows it has read and the database has not yet taken must live somewhere, and that place is memory.

The pattern has three parts:

  • A bounded queue. When it is full, the reader stops. This is backpressure: the slow side sets the pace.
  • Batches. One insert of a hundred rows costs far less than a hundred inserts of one row.
  • A pool of writers. More writers help until the database becomes the limit. After that, they only add load.

Sandbox · Backpressure in a bulk import

A model, not production data

Press the steps from 1 to 5. Each one is a decision people really make, in the order they usually make it.

III · Return

What it cost

Backpressure does not make the import faster. It makes it honest: the import runs at the speed of its slowest part and uses a fixed amount of memory. The speed came from batching and from finding the real limit of the database.

Patterns in this case

What to call these things

Producer–Consumer Concurrency
One side makes work and another side does it, with a queue between them, so that neither needs to know the other's speed. In the sandbox: the file reader and the writers.
Bounded Buffer and Backpressure Concurrency
The queue has a limit. When it is full, the producer waits, so the slow side sets the pace and memory stays flat. In the sandbox: the dashed "queue limit" line and the reader that pauses.
Worker Pool Concurrency
A fixed number of workers take jobs from one queue. In Go this is a few goroutines reading one channel. In the sandbox: the squares under "Writers".
Batching Data access
Many rows travel in one insert, so the cost of a trip to the database is paid once for the whole batch. In the sandbox: step 1 against step 3.
Pipes and Filters Integration
Work is cut into stages joined by queues: read, validate, write. Each stage can be measured and scaled alone. In the sandbox: the whole picture, left to right.