Skip to content
← Back to blog

The send button that kept accounting waiting for hours

Work done at Logiztik Alliance Group

Context

Logiztik is a logistics company in Ecuador, handling imports and exports for hundreds of clients. Accounting used a module to send invoices to those clients. It loaded what each client was paying for, generated the documents and emailed them to the client as attachments. It was not only invoices. Depending on the client there were other documents about the services we provided, and the formats changed depending on certain conditions.

This happened every day, for hundreds of clients. The screen was simple: a list of pending invoices. You could open one to see some details, but in practice the user checked all of them and clicked send.

I was a senior developer there. This came to me once it had become a big enough problem.

The problem

Behind that button was a blocking operation. The backend went through the selected invoices in a loop, one by one, building the documents and sending each email. When the company was small and sent a few tens a day, that was fine. By the time I was there it was at least 800 documents a day, and it could reach the thousands.

So the user sat there for hours, watching a loading dialog until everything was done. And if one of them failed along the way, the whole operation returned an error.

Each invoice had a status, but the statuses were only updated once everything had finished. After an error, sometimes nothing had changed, even if many emails had already gone out. The user had no way to know which ones. In accounting you cannot just send the same document twice, so they had to check one by one what had been sent, and then avoid selecting those or set their status by hand.

What I did

A service we already had

Just before this, we had built a small service with one purpose: generate and send emails in the background with Hangfire. I wrote it, and it was as small as it sounds, a couple of endpoints. It receives the data, including which templates to use for the email and the attachments, and does the whole job in the background. When it finishes, it puts an event in a RabbitMQ queue saying whether it worked or failed, with some data. The company was just starting to set up async jobs like this.

We agreed to use it to fix the accounting module too. For that we added one thing: the caller decides which queues the result events go to. That came in handy right after.

A small service just for this module

The plan was for the old backend to stop doing the work itself. It already had the data, so it would only build it and hand it to the email service. But then we needed to listen for the results, to know which emails went out and update each status.

The old backend could not do that. It was on .NET Framework, without the tools we have today, so we could not just run a background host inside it to subscribe to the messages. So we built a separate service, on a newer .NET version, just for this module.

Now the send button only sends the ids of the selected invoices to this new service. The service builds the data and sends it to the email service as a background job. A background host waits for the success and failure events and updates the statuses and other info in the database. Each invoice gets its own status from its own result.

Result

Now the user selects what they want to send, clicks send, and that is it. They can leave and do whatever they want. When they come back they see how many went out and how many failed, fix the failed ones and retry what they need. No more duplicates.

What I would do differently

There are two things we skipped that I would do from the start today.

We did not set up a dead letter queue. In some really odd cases the result event never arrived, so the status in the database was never updated. It happened so rarely that we left it for “later”. It is not hard to add, and it would let you recover from those cases.

We also did not add enough observability. By the time I left it was not really needed, but as this grows I imagine it will be. At the very least I would add good logging.

What I would tell a client

Async work is worth it. Anything long and heavy that a user sits waiting for can probably go to the background, and this was only one use case. Queues and events have a lot of others.

But they make debugging a lot harder. With several services working asynchronously, the only way to trace what happened when something fails is good observability. Someone who does not know what to look for will not find the error. So if you go this way, plan for logging and a dead letter queue from the start, not for later.