99.99%
Consistency
Strong Consistency
Latency
<200 ms
Scalability
Horizontal
Security
PCI DSS Encryption Audit
Client | API Gateway | Authentication Service | +------------------+----------------+ | | Wallet Service Transaction Service | | +------------+----------------------+ | Ledger Service | Kafka Event Bus | +--------------+------------------+ | | Notification Audit Service | Email / SMS / Push | PostgreSQL Cluster | Redis Cluster
در سیستم بانکی
Wallet فقط موجودی نیست.
اصل حقیقت (Source of Truth)
همیشه Ledger است.
مثلاً
Wallet A
100$
↓
Transfer 30$
↓
Wallet B
Ledger
Debit Wallet A Credit Wallet B
اگر Wallet خراب شود
Ledger هنوز حقیقت را نگه داشته است.
+-----------+ | User | +-----------+ | userId PK | | name | | kycStatus | +-----------+ 1 | * +------------+ | Wallet | +------------+ | walletId | | userId FK | | currency | | status | +------------+ 1 | * +----------------+ | LedgerAccount | +----------------+ | accountId | | walletId | | balance | +----------------+ 1 | * +----------------+ | Transaction | +----------------+ | transactionId | | type | | amount | | status | | createdAt | +----------------+ 1 | * +----------------+ | LedgerEntry | +----------------+ | entryId | | debitAccount | | creditAccount | | amount | +----------------+
هیچوقت Balance را مستقیماً Update نکن.
اشتباه
UPDATE wallet SET balance=balance-100
در سیستم بانکی این کار ممنوع است.
در عوض
Ledger Entry Debit Credit
بعداً
Balance
از روی Ledger محاسبه میشود.
فرض کنید
Wallet A
100$
Wallet B
50$
Transfer
30$
Ledger
Wallet A -30 Wallet B +30
جمع کل
-30 +30 = 0
هیچ پولی خلق نشد.
Transfer Money
Client | Wallet Service | Validate Wallet | Transaction Service | Check Balance | Ledger Service | Create Debit Entry | Create Credit Entry | Commit | Publish Kafka | Notification | Return Success
جزئیات
Client | API Gateway | Wallet Service | Ledger Service | PostgreSQL | Kafka | Notification | Client
Customer | | Digital Wallet Platform
API Gateway Authentication Wallet Service Ledger Service Transaction Service Notification Service Audit Service Fraud Detection KYC Service
داخل Wallet Service
Wallet API Wallet Validation Limit Checker Balance Calculator Ledger Adapter Transaction Coordinator Event Publisher
PostgreSQL به جای Mongo
Decision
PostgreSQL
Reason
ACID Transaction Consistency
Double Entry Accounting
Decision
Ledger
Reason
Audit Financial Safety Recovery
Kafka
Decision
Kafka
Reason
Async Notification Audit Analytics Retry
Redis
Decision
Redis Cache
Reason
Balance Cache Rate Limit Session
Idempotency Key
تمام درخواستهای انتقال باید دارای شناسه یکتای Idempotency-Key باشند تا در صورت Retry یا دوبار کلیک کاربر، تراکنش تکراری ایجاد نشود.
Saga Pattern
برای انتقال بین سرویسهای مختلف از Saga استفاده میشود تا در صورت شکست بخشی از فرآیند، عملیات جبرانی (Compensation) انجام شود.
DataDatabaseUserPostgreSQLWalletPostgreSQLLedgerPostgreSQLTransactionPostgreSQLCacheRedisEventsKafkaSearchElasticsearch
Client ↓ Gateway ↓ Wallet Service ↓ Transaction Service ↓ Ledger ↓ Commit ↓ Kafka ↓ Notification
Transfer موفق است. Kafka بعداً Notification را ارسال میکند.
Transaction Rollback
از Outbox Pattern استفاده میشود؛ ابتدا تراکنش و رویداد در یک تراکنش دیتابیس ثبت میشوند و سپس یک Outbox Publisher رویداد را به Kafka ارسال میکند. بنابراین هیچ رویدادی از دست نمیرود.
Replication Automatic Failover
Idempotency Key
SELECT ... FOR UPDATE
یا
Optimistic Lock
TLS برای تمام ارتباطات
Encryption at Rest
HSM برای نگهداری کلیدها
JWT + OAuth2
RBAC
Audit Log غیرقابل تغییر (Immutable)
Tokenization برای اطلاعات حساس
KYC/AML و موتور تشخیص Fraud
BottleneckSolutionLedger WritePartition بر اساس WalletId + Connection PoolBalance ReadRedis Cache با Invalidating پس از CommitNotificationKafka + Consumer GroupAuditAsync ConsumerReportingRead ReplicaHot WalletSharding بر اساس WalletId