Understand the Problem
In this chapter, we design a payment system. E-commerce has exploded in popularity across the world in recent years. What makes every transaction possible is a payment system running behind the scenes. A reliable, scalable, and flexible payment system is essential.
What is a payment system? According to Wikipedia, “a payment system is any system used to settle financial transactions through the transfer of monetary value. This includes the institutions, instruments, people, rules, procedures, standards, and technologies that make its exchange possible” 1.
A payment system is easy to understand on the surface but is also intimidating for many developers to work on. A small slip could potentially cause significant revenue loss and destroy credibility among users. But fear not! In this chapter, we demystify payment systems.
Step 1 – Understand the problem and establish design scope
A payment system can mean very different things to different people. Some may think it’s a digital wallet like Apple Pay or Google Pay. Others may think it’s a backend system that handles payments such as PayPal or Stripe. It is very important to determine the exact requirements at the beginning of the interview. These are some questions you can ask the interviewer:
What kind of payment system are we building?
Assume you are building a payment backend for an e-commerce application like Amazon.com. When a customer places an order on Amazon.com, the payment system handles everything related to money movement.
What payment options are supported? Credit cards, PayPal, bank cards, etc?
The payment system should support all of these options in real life. However, in this interview, we can use credit card payment as an example.
Do we handle credit card payment processing ourselves?
No, we use third-party payment processors, such as Stripe, Braintree, Square, etc.
Do we store credit card data in our system?
Due to extremely high security and compliance requirements, we do not store card numbers directly in our system. We rely on third-party payment processors to handle sensitive credit card data.
Is the application global? Do we need to support different currencies and international payments?
Great question. Yes, the application would be global but we assume only one currency is used in this interview.
How many payment transactions per day?
1 million transactions per day.
Do we need to support the pay-out flow, which an e-commerce site like Amazon uses to pay sellers every month?
Yes, we need to support that.
I think I have gathered all the requirements. Is there anything else I should pay attention to?
Yes. A payment system interacts with a lot of internal services (accounting, analytics, etc.) and external services (payment service providers). When a service fails, we may see inconsistent states among services. Therefore, we need to perform reconciliation and fix any inconsistencies. This is also a requirement.
With these questions, we get a clear picture of both the functional and non-functional requirements. In this interview, we focus on designing a payment system that supports the following.
Functional requirements
-
Pay-in flow: payment system receives money from customers on behalf of sellers.
-
Pay-out flow: payment system sends money to sellers around the world.
Non-functional requirements
-
Reliability and fault tolerance. Failed payments need to be carefully handled.
-
A reconciliation process between internal services (payment systems, accounting systems) and external services (payment service providers) is required. The process asynchronously verifies that the payment information across these systems is consistent.
Back-of-the-envelope estimation
The system needs to process 1 million transactions per day, which is 1,000,000 transactions / seconds = 10 transactions per second (TPS). 10 TPS is not a big number for a typical database, which means the focus of this system design interview is on how to correctly handle payment transactions, rather than aiming for high throughput.
Finished reading?
Mark it complete to track your progress.