A gift card is the one thing in our system that cannot be taken back. Once a brand issues a code, it can be spent straight away. Everything about how we keep balances follows from that.
The problem
A single purchase on Hubble can touch several balances. An app or bank that sells through our API pays from a prepaid wallet it keeps with us. A user may pay part of the price in coins or reward points. Some of those we keep ourselves. Others belong to the client’s own program and live in its system. A brand may fund a discount from a budget it has set aside. Then there is the payment, and the brand’s system that issues the card.
What makes it hard
Each of these answers on its own time. A payment can confirm late. A brand can be slow to answer. A client can resend a request it thinks was lost. No transaction spans all of them, so all-or-nothing is not on offer. What we can do is make every step safe to repeat and safe to undo.
Three more facts sharpen the problem. An issued code cannot be recalled. Some balances are not ours: when points belong to a bank’s program, the bank’s figure is the truth, and we can only ask it to debit. And many orders can draw on the same balance at once, as when a client running a campaign spends from one wallet across many users.
The principles we chose
Every movement is recorded, once. Each movement of value gets its own record and its own key. A retry finds the first record instead of making a second. When the balance lives in a client’s system, we send our own reference with the request, so their side can spot a repeat too.
Records are added, never edited. A correction is a new record, not a change to an old one. An edited record tells you what is true now. A list of added records tells you what happened, when, and why.
Every balance can be explained. A balance and the records behind it must always agree, and we can show how any figure was reached. We state each balance rule as an automated check that compares the two, so a rule is something we test, not something we hope.
Take first, then issue. Money is taken before a brand is asked for a card. If no card comes, the money goes back automatically. The opposite order would mean issuing something we cannot recall and hoping the charge goes through. Coins follow the same idea: a purchase that never finishes has taken nothing.
Know what funded what. Coins arrive in lots, each with its own expiry. A spend records how much it drew from each lot. That record is what makes undo exact: value goes back to the same lots, with their original expiry, not into a new lot that would outlive them.
Undo is one step. When a purchase fails after payment, one undo runs over everything it touched: the payment, the coins, any brand budget and any reward the purchase earned. Value returns to exactly where it came from.
Recovery uses the normal path. When our records and another system disagree, we ask the system that owns the fact and pass its answer through the same code the original event would have reached. Repair and normal work cannot drift apart.
What it costs
Strictness costs throughput. Keeping one busy balance exactly right means orders on it cannot all move at once. We accept that: being right about money comes before speed.
History takes room. A record that is never edited means more records, and they take more care to read than a single figure. Running totals kept beside them help with speed, and they can be compared with the records at any time.
Totals need labels. Some totals only ever grow, and others move back on an undo. Compare the wrong two and you invent a mismatch.
Held money shows early. Taking money before the card is safe, but a client sees its balance drop before it has the card.
What we would tell another team
- Give every movement a key before you need one. Adding keys after duplicates appear is much harder.
- Take the money before you issue anything you cannot recall.
- Record what funded each spend. Exact undo depends on it.
- Make recovery call the same code as the normal path.
- Start append-only, and write down which totals are gross and which are net.
The API docs show how orders and wallets look from a client’s side. For how an order itself stays safe, see never keep money without a code. If you would like to work on systems like this, see careers at Hubble.