Let us assume we have to handle 10 Million transactions per day. To calculate the transactions per second, we'll divide the total number of transactions per day by the number of seconds in a day.
Total transactions per day = 10,000,000
Number of seconds in a day = 86,400
Transactions per second = Total transactions per day / Number of seconds in a day
Transactions per second = 10,000,000 / 86,400
Transactions per second ≈ 115.74 transactions per second
So, the system needs to process approximately 115.74 transactions per second.
Given that 1 server can handle 1000 concurrent requests, we'll need to calculate the number of servers required to handle the total load.
However, for high availability scenarios, we need to consider redundancy and failover. Let's assume a redundancy factor of 2 for high availability.
We can roughly estimate 200 servers will be required to support our scale.
If each transaction contains 100 KB of data, we can calculate the total data generated per year and then estimate the storage required for 5 years.
Data per transaction = 100 KB
Total transactions per year = 10,000,000 * 365 ≈ 3,650,000,000
Total data generated per year = Data per transaction * Total transactions per year
Total data generated per year = 100 KB * 3,650,000,000 ≈ 365,000,000,000 KB
Total data generated for 5 years = Total data generated per year * 5
Total data generated for 5 years ≈ 365,000,000,000 KB * 5
Total data generated for 5 years ≈ 1,825,000,000,000 KB
Total data generated for 5 years ≈ 1.825 petabytes (PB)
So, approximately 1.825 petabytes of storage will be required for 5 years.
Performance Requirements:
For designing payment systems, multiple APIs will be required, below is a list of few essential APIs.
For the tables required in this design, refer to the class diagram, the list of classes is not exhaustive but this is a good number of tables to start with.
For the given problem of designing an online payment system, the best partitioning strategy would likely involve a combination of regional or geographical partitioning and functional partitioning.
Partitioning Algorithm:
When it comes to implementing partitioning for the system, a common algorithm used is consistent hashing.
Horizontal scaling would be the best strategy for scaling the databases in this scenario. It allows for adding more servers to the existing infrastructure, enabling better performance and higher availability without significant changes to the application architecture. With the growing user base and transaction volume, horizontal scaling ensures seamless expansion by distributing the workload across multiple nodes.
Implementing Read/Write Separation could be beneficial to optimize the system's performance and scalability. By directing read operations to read replicas or caches, the system can handle a larger volume of read requests without impacting write operations. This separation improves overall system responsiveness and user experience, especially during peak usage periods.
Below is a simple sequence diagram for Payment flow.
Before jumping into the detailed design, let's first understand a few components and their roles.
Lets have a look at a typical flow
Step 1: User Purchase
Step 2: Payment Gateway (PG)
Step 3: Payment Processor (PP)
Step 4: Card Association (CA)
Step 5: Issuer Bank (IB)
Step 6: Transaction Authorization
Step 7: Transaction Outcome
Now let's discuss the detailed component design
Payment Executor:
The double-entry system states that the sum of all the transaction entries must be 0. One cent lost means someone else gains a cent. It provides end-to-end traceability and ensures consistency throughout the payment cycle. Double-entry system is fundamental to any payment system and is key to accurate bookkeeping. It records every payment transaction into two separate ledger accounts with the same amount.
Overview:
Application in Payment Systems:
In an asynchronous communication environment, where messages may not be delivered or responses returned, ensuring correctness becomes crucial. Reconciliation serves as a practice to periodically compare states among related services, verifying their agreement, and acting as the last line of defense in payment systems. Every night the PSP or banks send a settlement file to their clients. The settlement file contains the balance of the bank account, together with all the transactions that took place on this bank account during the day. The reconciliation system parses the settlement file and compares the details with the ledger system. Reconciliation is also used to verify that the payment system is internally consistent. For example, the states in the ledger and wallet might diverge and we could use the reconciliation system to detect any discrepancy.
An end-to-end payment request flows through many components and involves both internal and external parties. While in most cases a payment request would complete in seconds, there are situations where a payment request would stall and sometimes take hours or days before it is completed or rejected. Here are some examples where a payment request could take longer than usual:
The payment service must be able to handle these payment requests that take a long time to process. If the buy page is hosted by an external PSP, which is quite common these days, the PSP would handle these long-running payment requests in the following ways:
When the payment request is finally completed, the PSP calls the registered webhook mentioned above. The payment service updates its internal system and completes the shipment to the customer.
Network failures can disrupt fund transfers, leading to transaction delays or failures. Common causes include network congestion, server outages, or connectivity issues between payment gateways, banks, and other financial institutions.
Handling failed transactions in a payment system is crucial for ensuring reliability and fault tolerance. Here's how we can tackle these challenges based on the provided information:
Tracking Payment State:
Retry Queue and Dead Letter Queue:
Retry Strategies:
Example of Retry:
Trade-off: Consistency vs. Availability
Explanation: We prioritize availability over strong consistency in our payment system to ensure uninterrupted service, even if it means sacrificing a slight degree of data consistency. This choice is vital in financial systems where downtime can lead to significant losses.
Tech Choices: We utilize distributed databases like Apache Cassandra or Amazon DynamoDB, offering high availability and partition tolerance while maintaining eventual consistency to ensure data integrity over time.
Trade-off: Latency vs. Cost in Payment Processing
Explanation: We prioritize low latency in payment processing to enhance user experience and drive increased conversion rates, balancing this with managing operational expenses effectively. This ensures a fast, responsive payment experience while optimizing operational costs.
Tech Choices: To minimize latency, we employ in-memory caching solutions like Redis or Memcached and leverage content delivery networks (CDNs) to cache static assets, optimizing performance without compromising on cost-effectiveness.
Machine Learning for Fraud Detection:
Enhanced Security and Transparency: