Design a Daily Reward Points Pipeline for Bank Files
Turn a bank's daily file of 100 million transactions into reward points with an unreliable bonus API, then tell each user what they earned and let them see it.
Problem statement
Design a system that turns a bank's daily transaction file into reward points. Once a day, the bank puts a file with about 100 million of the previous day's transactions on its SFTP server (a secure file server that we log in to and download from).
The system gives every transaction points based on its merchant code plus a bonus from an outside bonus API, tells each user how many points they earned that day, and lets users see their transactions and points.
What the system must do. - Download each day's file from the bank's SFTP server and keep the original file unchanged. - Build it as an event-driven pipeline: split the file into chunks, connect the steps with queues, and let pools of workers take work from those queues. - Give each transaction base points from its merchant code (the merchant category code, a 4-digit code for the type of business, such as restaurants or airlines). - Call the bonus API for each transaction to get its bonus points. The API is unreliable: some calls fail or time out. - A transaction's points become spendable (usable by the user) only after the bonus API gives a good answer for that transaction. Until then they are pending. - Store everything: the file, every transaction and every points result. - After the day's file is processed, send each user one notification with the points they earned that day. - Users can see their transactions and the points each one earned.
For this exercise, assume: - Each row has a bank transaction ID that is unique across all files, a bank account ID, an amount, a merchant code and the transaction time. Every bank account ID belongs to exactly one user. Every row is a purchase. - The file is a text file of about 20 GB.
After the file is complete, the bank uploads a small manifest file with the row count and a checksum (a fingerprint of the file's bytes). Sometimes the bank uploads the same file twice.
- Base points come from a table that maps each merchant code to points per dollar (for example, 3 for restaurants and 1 for everything else). The table has a few thousand rows and changes rarely; a change applies from the next file. - The bonus API takes one transaction per call and returns its bonus points, often 0.
An HTTP 200 answer with a bonus value is a good answer. Asking twice about the same transaction gives the same answer. - Users also see their spendable balance: the total of their spendable points. - Points that are still pending when the daily notification goes out are listed as pending.
They become spendable later, when a retry succeeds, without a second notification.
Out of scope: spending points, refunds and other corrections to past transactions, editing the merchant-code table, fraud checks, user login, and how the push or email provider delivers a message.
What to build
- Download the bank's daily file from its SFTP server, keep the original file, and store every transaction in it exactly once, even when the bank sends the file twice or a worker crashes partway through.
- Give each transaction base points from its merchant code and a bonus from the bonus API, and make the transaction's points spendable only after the API gives a good answer for it.
- After a day's file is processed, send each user who had transactions in it one notification with the points earned that day, showing what is spendable and what is still pending.
- Let users page through their transactions with the base points, bonus points and spendable or pending status of each one, and see their current spendable balance.
At scale
- One file per day with about 100 million transactions from the previous day. For this exercise, assume the file is about 20 GB and about 20 million different users have at least one transaction in it.
- For this exercise, assume every transaction must be stored with its base points, and every user's daily notification sent, within 6 hours of the file arriving.
- For this exercise, assume the bonus API allows at most 10,000 calls per second from this system in total, usually answers in about 100 ms, fails or times out on about 1 call in 100, and is sometimes down for up to an hour.
- For this exercise, assume 50 million users in total, up to 20,000 history reads per second in the hour after notifications go out, and that every file, transaction and points record is kept for 7 years.