AWS Serverless Payment Architecture: Design Patterns and Implementation
date
May 2, 2023
slug
processing-payment-a-serverless-journey
status
Published
tags
serverless
aws
sqs
sns
lambda
architecture
summary
Donald Knuth once said, 'Premature optimization is the root of all evil in programming.' So don't overthink it - just embrace the scalability!”
type
Post
As I start exploring serverless computing, I'm excited to share my thoughts on how it can help with payment processing. This is just the beginning, and I'll dive deeper into the details in future articles.
In this article we’ll cover scheduled payment processing with multiple entities involved, which can influence the amount and payment receivers.
Imagine you're in charge of processing payments for multiple events every day at 3 AM. You need to split the revenue between the event organisers and their associated clubs. Sounds straightforward, but it's not as simple as it seems. When solving such problem technology is just one part of the equation. we needed to break it down, identify the key components, and understand the assumptions and expected outcomes before even thinking about the solution.
Traditional payment systems, built as monoliths, can struggle to keep up with the demands of payment processing. They can lead to:
- Scalability issues When there's a lot of traffic, traditional systems can get overwhelmed and slow down. You'd be constantly tuning infrastructure and application distribution to scale without breaking the bank.
- High costs Maintaining and scaling monoliths systems can be expensive.
- Security risks Traditional systems can be vulnerable to security threats. Unless performing a regular checks and patches.
An alternative approach:
By using serverless computing, I can build a payment processing system that's designed to handle the complexities of payment workflows. I can focus on writing code that's efficient, reliable, and accurate, without worrying about the underlying infrastructure. This approach allows me to:
- Simplify payment workflows Serverless architecture enables me to break down complex payment workflows into smaller, more manageable components.
- Improve reliability Serverless systems provide built-in redundancy and failover capabilities, ensuring that payments are processed reliably and accurately.
- Enhance security Serverless systems provide a secure environment for processing sensitive payment information.
In this article, I'll explore the details of building a serverless payment processing system, including design patterns, implementation, and best practices. By the end of this journey, you'll have a better understanding of how serverless computing can help you build a reliable, efficient, and secure payment processing system.
SQS and SNS are flexible tools. You can use them in many ways, depending on what you need. For this instance, we're using them to sort out payments for football matches that were played the day before.
Here’s a sequence diagram of how it’ll work. (Designed using PlanUML)
A high level data architecture.
The infrastructure is defined using AWS SAM with CloudFormation, chosen for its tight AWS integration and simplified Lambda deployment workflow. The stack uses TypeScript for type safety and better developer experience.
Database queries use raw SQL for precise performance control and transaction management. While an ORM like Prisma could provide type safety and migration tools, raw SQL currently offers better control over complex payment-related queries and transaction boundaries.
Here’s a Github repo to the project 📦 https://github.com/devbks/batch-process-using-sqs-sns.
There are lots of space for improvement still. Since this is a early version of the system, I focused on high level overview and getting a proof of concept to cloud. Next time we’ll cover more crucial aspects of the project such as below.
- Add transaction boundaries around match fetching
- Implement idempotency checks using match IDs
- Add basic error logging
- Include payment status tracking