Payment System
- Build a Payment System for E-commerce site
- Data should be consistent
- Do not allow users to double submit payments
- Actual handling of payments can be done by an external service
- Example: Stripe, RazorPay
- Batch Payments to vendors: Tipalti??
- NFR:
- Latency is not important, it can take up to few seconds
- Generate Analytics for:
- Orders per user
- Revenue per seller
Database
- payments table
- (idempotency key, amount, status)
- (1234, ₹104, pending)
- The Database should be strongly consistent
- Google Spanner
- CockroachDB
Payment Gateway
- https://www.youtube.com/watch?v=m6DtqSb1BDM
- https://www.youtube.com/watch?v=rT4sS4l51PY
- The moment payment confirmation page is loaded, payment service generates a unique identifier called idempotency key
- All the subsequent communication with Payment Gateway will have this identifier as header:
Idempotency-Key: <your-key>
- If the request is sent multiple times to Gateway, gateway can ignore duplicate operations and make it idempotent
- Payment Gateway does not complete the processing instantly, instead it provides a webhook when the processing is complete
- Payment Service exposes an API which is used as webhook
Failures
- Payment Server dies after the table is populated with pending status
- Payment Gateway dies and webhook is never called, hence payment stuck on pending
- Ideas:
- Do polling after some time, if we don’t hear back from gateway
- If gateway does not recognize idempotency key, then delete from payments table
- if completed/failed, update the table accordingly
- If in-progress, do nothing!
Analytics
- Payments DB ⇒ CDC ⇒ Time-Series DB
- Orders DB sharding based on seller ID
- Revenue DB sharding based on buyer ID