DEV Community

Your payment processor can be the reason your app feels slow.

A user clicks "Pay now" on a 50 shillings order. Your server then calls the payment processor and waits for it to confirm that the charge went through. If the payment processor is down, your user waits too. If it's down, the whole process just fails. Your user is now stuck staring at a spinner, not knowing whether the charge went through or not. Here's the problem: your app is the one making the call. The payment processor is the one answering it. As long as your app calls it directly and waits, your app speed is tied to the payment processor's speed. If the payment processor struggles, so does everyone checking out. Something needs to sit between them so that your app doesn't have to wait. So you try this: save the order into the order table. Then hand off the task of confirming the payment to the payment service, asynchronously. Main point: your app doesn't wait for that confirmation. It hands off the task and immediately tells the user, "You're all set." That solves the spinner problem. But here's a new one: what if order 88 gets saved, but before the handoff happens, the app crashes? Order 88 exists, but nobody asks the payment service to confirm whether the payment exists.

Or the handoff goes out, but the call fails. Payment service was down. Either way, you already told the user, "You're all set." So nothing looks wrong. Order 88 exists; nobody confirmed the payment. The problem is that you're separating these two actions. That is, saving the order and handing off the task. Fix is to make them one. When you save order 88 into the order table, also save a record into the second table, in the same transaction. That record says: "Order 88 was placed, confirm 50 shillings payment." This is called a message. Either both writes happen, or neither does. We're leaving no gap for a crash to slip through. That second table is called an outbox. Now, order 88's message is safely saved. But a record sitting on a table does not send itself. Something needs to watch that table. Find records that haven't gone out and push them forward. A background process does exactly that. It finds order 88's record, sends it onward, then marks it as sent so that it's never sent twice. That process is called a relay. So where does the relay actually send it? It sends it to a waiting line.

It's basically a place that holds tasks in order until something is ready to take one. And that's a queue. Teams usually use a tool already built for this, like Redis, which holds that line, order 88's tasks included, and hands them out as things ask for them. But something has to actually pull order 88's task out of the queue and actually call the payment service. And that's a worker. It picks the task, calls the payment service, and gets a real answer. Now here's where it splits. If the call itself fails—network issue, service went down—the worker doesn't get an answer back. So the queue puts the task back for another worker to try. But sometimes the call succeeds and the answer is simply declined. Card didn't work. That's not a crash. So retrying that task won't fix a declined card. So instead, the task gets pulled aside into a separate place, and the order gets marked as "payment declined." And an email goes out to the customer saying the charge didn't go through, with a link to try a different card. This separate place is called a dead-letter queue. It's not for things that crashed. It's for things that finished but failed anyway.

So let's follow order 88 from start to finish. The user checks out. The app saves order 88 and its payment task in one transaction. Then the relay finds that task and saves it to the queue. A worker then pulls it and calls the payment service. If the call fails, it gets retried. If it's declined, the order gets declined and the customer gets a notification. If it succeeds, the worker acknowledges it and order 88 is confirmed. So the user is not left staring at a spinner or wondering what actually happened. So that's basically the idea behind message queues. A direct call makes you wait on someone else's speed. Going async fixes that, but opens a gap. An outbox closes that gap. And that's how systems stay fast and stay honest about what actually happened.

Top comments (0)