Security, Auth & Payment Systems
Design stateless/revokable auth, protect sensitive data, and guarantee exact double-entry financial ledgers.
In plain words
Authentication checks who you are, authorization checks what you’re allowed to do, and payment systems must never charge you twice or lose money.
Think of it like…
JWTs are stamped event wristbands (fast check at the door without calling HQ); a double-entry ledger is a bound ink accounting book where you never erase an error - you write an offsetting adjustment entry.
Where you’ve seen it
"Log in with Google" uses OAuth. When you pay, an idempotency key makes sure a double-click doesn’t charge your card twice.
Words you’ll hear
- Authentication
- Proving identity (password, OTP, passkey).
- Authorization
- Deciding what an authenticated user may access.
- JWT
- A signed token carrying user info that servers can verify.
- Idempotency key
- A unique ID per payment attempt so retries don’t double-charge.
Visual diagram
Theory & deep dive
Double-Entry Bookkeeping: Never just `UPDATE balance = balance - 50`
If you only store a single balance integer and overwrite it, you have zero audit trail when a bug or dispute happens. Banks and fintechs use an append-only double-entry ledger: every movement creates two balanced rows (Buyer -$50 and Merchant +$50) so the sum of every transaction is always 0.
How it works, step by step
Step 1 of 41. Authenticate cleanly (JWT + Refresh Tokens)
Issue short-lived signed JWT access tokens (5–15 min) verified statelessly at the gateway, paired with revokable opaque refresh tokens stored in DB/Redis.
- Access JWT TTL
- 5–15 min
- Signing
- RS256 / EdDSA
When to use it & trade-offs
What it is
Security & Payment architecture covers authentication/authorization (OAuth 2.0, JWT, RBAC), encryption & secrets hygiene, and immutable double-entry ledgers with idempotent payment state machines.
Why it matters: A bug in caching or feeds is annoying; a bug in auth leaks private data, and a floating-point or non-idempotent payment bug literally loses money.
When to reach for it
- Short-lived JWT access tokens + rotating refresh tokens (or Redis session IDs for high-security banking)
- Append-only double-entry ledger in ACID PostgreSQL for any wallet, checkout, or payout system
- KMS/Secrets Manager + TLS 1.3 everywhere + zero secrets in logs or URLs
When to avoid or be careful
- Using IEEE-754
float/doublefor currency amounts (0.1 + 0.2 !== 0.3) - Granting 30-day JWTs with no revocation mechanism
Auth & Financial Design Choices
Strengths: Sessions revoke in 1 delete; JWTs validate signature with zero DB lookup.
Watch out: Long-lived JWTs cannot be revoked instantly without a Redis denylist.
Strengths: Every dollar has a traceable source and destination; prevents negative drift.
Watch out: Requires periodic balance snapshots / materialized totals for fast reads.
Strengths: RBAC is simple; ReBAC scales to billions of fine-grained resource ACLs.
Watch out: ReBAC requires graph indexing and careful permission caching.
Interview signals & numbers to know
When this topic comes up
- payment processor
- wallet / ledger
- OAuth / RBAC
- double spend
- PCI compliance
Benchmarks & rules of thumb
- Access JWT TTL
- 5–15 minutes
- Currency unit
- Integer cents / minor units
- Password hash
- Argon2id / bcrypt
- Reconciliation cadence
- Hourly / Daily vs PSP
Common mistakes & follow-ups
Common interview mistakes
- Doing
SELECT balancethenUPDATE balance = balance - 10without row locking (FOR UPDATE) or atomic conditional update (allows race-condition overdraft!) - Using
FLOATorDOUBLEfor money instead of integer minor units (BIGINT) - Trusting incoming webhook JSON without verifying the provider’s cryptographic HMAC signature
Likely interviewer follow-ups
- How do you prevent a user with $10 from spending $20 by sending two concurrent withdrawal requests?
- If JWTs are stateless, how do you force-log-out a compromised user immediately?
- Why is a reconciliation pipeline still required even if your ledger uses ACID transactions?
Knowledge check
Put it into practice
Design full architectures with AI feedback
Apply Security, Auth & Payment Systems inside real High-Level Design prompts or review Q&A flashcards.