
Backend Engineer | Web3 | Golang | Laravel | Mod @BerfBased
127.0.0.1
Joined May 2018
- Tweets10.8K
- Following2.9K
- Followers1.8K
- Likes51.2K
Database Transactions
One thing you don't want in a wallet system is money leaving one account without reaching the other.
I am building PropInvest, a property investment platform where users can fund wallets and invest in real estate. When an investor puts money into a property, the system needs to:
- Deduct funds from their wallet
- Create the investment record
- Update the property's funding status
- Log the transaction
Now, what happens if the wallet is debited, but the server crashes before creating the investment?
The investor's money is gone, but they don't own any shares. That's a nightmare.
This is why database transactions matter.
A database transaction lets you group multiple operations together so they either all succeed or all fail.
If the wallet debit, investment creation, and transaction log all succeed, the transaction commits.
If any step fails, everything rolls back. The wallet stays untouched. No partial state.
This is called atomicity, the A in ACID.
ACID stands for:
- Atomicity: All operations succeed, or none do.
- Consistency: The transaction preserves database rules and constraints.
- Isolation: Concurrent transactions don't interfere with each other.
- Durability: Once committed, changes persist even if the system crashes.
For PropVest's wallet operations, every debit, credit, and record is wrapped in the same database transaction.
One important detail though: this protects database changes within that transaction. It doesn't automatically reverse an external action, like a payment already sent to Paystack.
That is why I also used idempotency keys, locked balances, and reconciliation workers. But transactions were the foundation.
When handling money, a successful API response isn't enough. You need to make sure the underlying operations are actually safe.
Never assume your code will complete every step. Design your database operations to handle failure.
🤖 Made with AI
Transactional Outbox Pattern
One thing I've implemented in PropInvest - a platform I am building for fractional real estate ownership, is the Transactional Outbox Pattern.
Say you're building a wallet system for property investments. A user purchases shares in a property. You debit their wallet, credit the property pool, and commit the transaction. Then you publish an "investment_completed" event to Kafka (or any other message broker e.g RabbitMQ) so that the notification service can alert the user and update their portfolio.
Simple enough, right?
But what happens if your application crashes after the database commit but before publishing the event? The investment succeeded, money moved, but the notification was never sent. Other services that depend on that event - like portfolio tracking - may never know the investment happened.
Now, if you publish the event before committing the transaction, you have the opposite problem. The event gets published, but the database transaction might fail. Other services are reacting to an investment that never actually went through.
This is the dual-write problem.
The Transactional Outbox Pattern solves this by saving the business data and the event in the same database transaction. If the transaction commits, both are saved. If it rolls back, neither is saved.
A separate worker reads pending events from the outbox table, publishes them to the message broker - Kafka in this case, and marks them as processed. If the worker goes down, the event is still in the database and can be published when the worker comes back up.
But there's a catch.
The worker could publish an event successfully, crash before marking it as processed, and publish it again after restarting. That means duplicates are possible.
This is where idempotency comes in. Give every event a unique ID, and let consumers track which events they've already processed. If an event arrives again, the consumer can safely skip the duplicate.
The same principle applies to API requests. If a client retries an investment purchase because of a timeout, an idempotency key helps prevent the same shares from being purchased twice.
One thing to keep in mind: the outbox prevents the database change and the intent to publish from getting out of sync. It doesn't guarantee exactly-once delivery or processing. You still have to handle retries and duplicates yourself.
The outbox handles reliable event delivery.
Idempotency handles repeated execution.
Both matter when building systems where a missed event or a duplicate operation can cause real problems - especially when real money is involved.
🤖 Made with AI
When a deal is accepted, we don’t move money in the API. We send a Kafka message to "otc_order_processor." An external service picks it up, moves the funds and securities, then calls back with settle_success or settle_fail.
1
12
Two balance checks happen before any deal goes through,
1) When the bargain is opened (pre-flight).
2) Again inside the database transaction.
This is because price/balance can change. You can’t trust state from 10 minutes ago.
N.B = there is also a balance guard in the consumer
1
14
Before a db transaction is opened, we mark the bargain as "processing" so that if the server crashes during the transaction, we won't lose track of what was happening. The Kafka consumer checks for orphaned "processing" bargains and recovers them
1
12
If an offer is negotiable, the user can suggest a different price or quantity. The dealer can accept, counter, or reject.
The counter/negotiation process continues until someone agrees, walks away/cancels the negotiation or rejects a bargain.
Every action is a state transition.
1
17
I just built an OTC trading system in Go, and honestly, it was one of the more humbling projects I've worked on. OTC (Over the counter) means:
- Dealers list bonds or securities privately
- Users buy or negotiate the price directly
Here’s what really went into it:
3
6
344
POW 🦾- Hit me up for top tier moderation for your web3 projects
Huge thanks to the teams I've worked with for the kind words. It's rewarding to know the effort is appreciated 🙏.
LIFE AUDIT
Full disclosure, the first few months of this year, hell the past year, has been chaotic as hell for me. It has been a very intense roller coaster, sometimes with more lows than highs 👇
Gm To believers
Here's the to-do list
● keep clicking
● keep clicking
● keep clicking
Not stopping
28














