交易系统是现代商业和金融活动的核心,涵盖从电子商务订单处理到金融机构的实时清算等多种场景。这类系统通常需要高并发处理能力、实时数据存储和高效检索功能,同时需要在数据一致性与性能之间找到平衡。随着交易数据的规模和复杂性不断增长,传统交易系统架构在应对这些挑战时,往往因固定的表结构和横向扩展能力的不足而受限。

MongoDB 作为一款分布式文档型数据库,以其灵活的架构、高吞吐能力和内置的事务支持,为构建复杂、高效的交易系统提供了一种现代化的解决方案,能够满足多样化的业务需求。

  1. 高并发性 交易系统需要能够处理大规模的并发请求,尤其是在金融领域和电子商务中,用户同时进行交易、查询余额和其他操作的场景非常普遍。如果系统无法承载高并发,可能会导致响应延迟甚至系统崩溃。

  2. 数据一致性与事务管理 交易系统必须确保数据的一致性,尤其是涉及账户余额、交易记录和消费等敏感操作时。一旦数据不一致,可能会引发财务风险和用户信任问题。分布式环境下的事务管理进一步加大了实现难度。

  3. 灵活的数据建模 传统关系型数据库通常使用固定表结构,难以应对交易系统中复杂多变的业务逻辑和频繁变化的需求。例如,新增业务功能可能需要调整数据模型,这会导致系统修改成本高昂,甚至中断服务。

  4. 实时数据处理与查询性能 在海量交易数据的背景下,系统需要能够高效存储、检索并实时处理用户的查询请求,例如账户余额查询和历史交易明细的检索。这要求底层数据库在提供高吞吐能力的同时,也要有极快的查询性能。

  5. 系统可扩展性 随着业务的增长,交易系统的数据规模会持续增加。这就要求系统具备良好的横向扩展能力,以确保在扩展服务器或存储节点时,性能能够线性提升。

对于余额的操作也是构建后续交易API的基础,这两个API的实现使用MongoDB的Inc原语进行原子操作,同时支持事务。它们不会被单独使用,因为增加或扣减余额都需要有对应的交易记录,因此,IClientSessionHandle是必选参数:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private async Task<AccountEntry> AddBalanceAsync(string accountId, decimal amount, CancellationToken ct, IClientSessionHandle s)
{
    if (amount <= 0)
    {
        throw new ArgumentException("Amount should > 0");
    }
    var filter = Builders<AccountEntry>.Filter.Eq(x => x.Uuid, accountId);
    var update = Builders<AccountEntry>.Update
        .Inc(x => x.Balance, amount)
        .Set(x => x.UpdateTime, DateTime.UtcNow);
    var options = new FindOneAndUpdateOptions<AccountEntry> { ReturnDocument = ReturnDocument.After, IsUpsert = true };
    return await _accountCollection.FindOneAndUpdateAsync(s, filter, update, options, cancellationToken: ct);
}

private async Task<AccountEntry> DecreaseBalanceAsync(string accountId, decimal amount, CancellationToken ct, IClientSessionHandle s)
{
    if (amount <= 0)
    {
        throw new ArgumentException("Amount should > 0");
    }
    var filter = Builders<AccountEntry>.Filter.And(
        Builders<AccountEntry>.Filter.Eq(x => x.Uuid, accountId),
        Builders<AccountEntry>.Filter.Gte(x => x.Balance, amount)
    );
    var update = Builders<AccountEntry>.Update
        .Inc(x => x.Balance, -amount)
        .Set(x => x.UpdateTime, DateTime.UtcNow);
    var options = new FindOneAndUpdateOptions<AccountEntry> { ReturnDocument = ReturnDocument.After };
    return await _accountCollection.FindOneAndUpdateAsync(s, filter, update, options, cancellationToken: ct);
}

值得一提的是,在我们的设计中,这两个API中的amount都是正数,好处是在扣减余额可以加入Filter.Gte(x => x.Balance, amount),进一步保证扣除余额的正确性。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public async Task<TransResult> RechargeAsync(string accountId, string tid, decimal amount)
{
    using (var session = await _mongoClient.StartSessionAsync())
    using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
    {
        return await session.WithTransactionAsync(
            async (s, ct) =>
            {
                var existingTransEntry = await GetTransactionByTidAsync(tid, ct, s);
                if (existingTransEntry != null)
                {
                    return new TransResult { Code = TransCode.DuplicateTrans };
                }

                var newBalanceEntry = await AddBalanceAsync(accountId, amount, ct, s);

                var newTransEntry = new TransEntry
                {
                    Tid = GenerateTid(),
                    Uuid = accountId,
                    Amount = amount,
                    NewBalance = newBalanceEntry.Balance,
                    Type = TransType.Recharge,
                    CreateTime = DateTime.UtcNow,
                    UpdateTime = DateTime.UtcNow,
                };

                await _transCollection.InsertOneAsync(s, newTransEntry, cancellationToken: ct);

                return new TransResult
                {
                    NewBalance = newTransEntry.NewBalance,
                    Tid = newTransEntry.Tid
                };
            }, cancellationToken: cts.Token);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public async Task<TransResult> PurchaseAsync(string accountId, string tid, decimal amount)
{
    using (var session = await _mongoClient.StartSessionAsync())
    using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
    {
        return await session.WithTransactionAsync(
            async (s, ct) =>
            {
                var existingTransEntry = await GetTransactionByTidAsync(tid, ct, s);
                if (existingTransEntry != null)
                {
                    return new TransResult { Code = TransCode.DuplicateTrans };
                }

                var newBalanceEntry = await DecreaseBalanceAsync(accountId, amount, ct, s);
                if (newBalanceEntry == null)
                {
                    return new TransResult { Code = TransCode.InsufficientBalance };
                }

                var newTransEntry = new TransEntry
                {
                    Tid = GenerateTid(),
                    Uuid = accountId,
                    Amount = amount,
                    NewBalance = newBalanceEntry.Balance,
                    Type = TransType.Purchase,
                    CreateTime = DateTime.UtcNow,
                    UpdateTime = DateTime.UtcNow,
                };

                await _transCollection.InsertOneAsync(s, newTransEntry, cancellationToken: ct);

                return new TransResult
                {
                    NewBalance = newTransEntry.NewBalance,
                    Tid = newTransEntry.Tid
                };
            },
            cancellationToken: cts.Token);
    }
}

创建 10 个账户,并为每个账户进行初始充值 1000。对每个账户生成 5 笔交易,随机决定交易类型(充值或消费),交易金额在 0~300 之间。 打印每个账户的当前余额,以及最近10笔交易的详情。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public async Task RunAsync()
{
    var random = new Random(42);

    // Create 10 accounts
    var accountIds = new List<string>();
    for (int i = 0; i < 10; i++)
    {
        var accountId = GenerateAccountUuid();
        var account = await CreateAccountAsync(accountId);
        accountIds.Add(account.Uuid);

        await RechargeAsync(account.Uuid, GenerateTid(), 1000);
        Console.WriteLine($"Created account {account.Uuid} with initial balance of 1000.");
    }

    Console.WriteLine("------------------------------------------------------------------------------");

    foreach (var accountId in accountIds)
    {
        for (int i = 0; i < 5; i++)
        {
            var isRecharge = random.Next(0, 2) == 0;
            var amount = (decimal)(random.NextDouble() * 300);
            var tid = GenerateTid();

            if (isRecharge)
            {
                var result = await RechargeAsync(accountId, tid, amount);
                if (result.Code == TransCode.Success)
                {
                    Console.WriteLine($"Recharge: Account {accountId}, Amount: {Math.Round(amount, 2)}, New Balance: {Math.Round(result.NewBalance, 2)}, Tid: {tid}");
                }
                else
                {
                    Console.WriteLine($"Recharge failed: {result.Code}");
                }
            }
            else
            {
                var result = await PurchaseAsync(accountId, tid, amount);
                if (result.Code == TransCode.Success)
                {
                    Console.WriteLine($"Purchase: Account {accountId}, Amount: {Math.Round(amount, 2)}, New Balance: {Math.Round(result.NewBalance, 2)}, Tid: {tid}");
                }
                else
                {
                    Console.WriteLine($"Purchase failed: {result.Code}");
                }
            }
        }
    }

    Console.WriteLine("------------------------------------------------------------------------------");
    foreach (var accountId in accountIds)
    {
        var balance = await GetBalanceAsync(accountId, CancellationToken.None);
        var transactions = await GetTransactionsAsync(accountId, CancellationToken.None);

        Console.WriteLine($"Account {accountId} Balance: {Math.Round(balance, 2)}");
        Console.WriteLine("Recent Transactions:");
        foreach (var transaction in transactions)
        {
            Console.WriteLine($"Tid: {transaction.Tid}, Type: {transaction.Type}, Amount: {Math.Round(transaction.Amount, 2)}, New Balance: {Math.Round(transaction.NewBalance, 2)}, Time: {transaction.CreateTime}");
        }
        Console.WriteLine("****************************************************************************");
    }
}

Account Acc_BE2E2B2FD3A2490CBEEA9625C3BD7C18 Balance: 1267.83 Recent Transactions: Tid: T_6DA267BECAC945E2973903C7EE36C599, Type: Recharge, Amount: 1000, New Balance: 1000, Time: 11/24/2024 4:00:42 AM Tid: T_6BB3C3B2E9CD4197B609957C2937D218, Type: Purchase, Amount: 42.27, New Balance: 957.73, Time: 11/24/2024 4:00:43 AM Tid: T_0D63FB52BCBF4FF382D7C611252329F4, Type: Recharge, Amount: 156.83, New Balance: 1114.56, Time: 11/24/2024 4:00:43 AM Tid: T_30201F58B3AA4BD6B14C4B9DC0A20272, Type: Recharge, Amount: 78.78, New Balance: 1193.33, Time: 11/24/2024 4:00:43 AM Tid: T_42837124561748BFBECA1677FD951338, Type: Purchase, Amount: 153.88, New Balance: 1039.46, Time: 11/24/2024 4:00:43 AM Tid: T_16DC47F772814D4A8A3E1DBCF26B661D, Type: Recharge, Amount: 228.38, New Balance: 1267.83, Time: 11/24/2024 4:00:43 AM

本文基于 MongoDB 提供了完整的账户管理与交易支持功能,适合于中小型交易系统,易于扩展到更加复杂的应用场景,例如多货币支持、国际支付等。每个功能模块通过事务、过滤器和幂等性处理相互配合,构建了一个安全、高效且易于扩展的交易系统框架。这种设计能够满足实际业务中高并发、数据一致性和复杂交易的需求,同时提供了明确的错误处理路径,便于维护与扩展。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Driver;

namespace MongoTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var model = new TransModel();

            model.RunAsync().Wait();

            Console.WriteLine("Done");
        }


    }

    public class AccountEntry
    {
        public string Uuid { get; set; }
        public decimal Balance { get; set; }
        public DateTime CreateTime { get; set; }
        public DateTime UpdateTime { get; set; }
    }

    public class TransEntry
    {
        public string Tid { get; set; }
        public string Uuid { get; set; }
        public decimal Amount { get; set; }
        public decimal NewBalance { get; set; }
        public TransType Type { get; set; }
        public DateTime CreateTime { get; set; }
        public DateTime UpdateTime { get; set; }
    }

    public enum TransType
    {
        None = 0,
        Recharge = 1,
        Purchase = 2,
    }

    public class TransResult
    {
        public string Tid { get; set; }
        public decimal NewBalance { get; set; }
        public TransCode Code { get; set; }
    }

    public enum TransCode
    {
        Success = 0,
        DuplicateTrans = 1,
        Purchase = 2,
        InsufficientBalance = 3
    }

    public class TransModel
    {
        private readonly IMongoClient _mongoClient;
        private readonly IMongoCollection<AccountEntry> _accountCollection;
        private readonly IMongoCollection<TransEntry> _transCollection;

        private const string DatabaseName = "Transaction";
        private const string AccountCollectionName = "Account";
        private const string TransCollectionName = "Trans";
        public const string ConnectionString = "";

        public TransModel()
        {
            _mongoClient = new MongoClient(ConnectionString);
            var database = _mongoClient.GetDatabase(DatabaseName);

            var ignoreExtraElementsConvention = new ConventionPack { new IgnoreExtraElementsConvention(true) };
            ConventionRegistry.Register("IgnoreExtraElements", ignoreExtraElementsConvention, type => true);
            _accountCollection = database.GetCollection<AccountEntry>(AccountCollectionName);
            _transCollection = database.GetCollection<TransEntry>(TransCollectionName);
        }

        public static string GenerateTid() => $"T_{Guid.NewGuid().ToString("N").ToUpper()}";
        public static string GenerateAccountUuid() => $"Acc_{Guid.NewGuid().ToString("N").ToUpper()}";

        public async Task<AccountEntry> CreateAccountAsync(string uuid, CancellationToken cancellationToken = default)
        {
            using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
            {
                var newAccount = new AccountEntry
                {
                    Uuid = uuid,
                    Balance = 0,
                    CreateTime = DateTime.UtcNow,
                    UpdateTime = DateTime.UtcNow
                };

                await _accountCollection.InsertOneAsync(newAccount, cancellationToken: cts.Token);

                return newAccount;
            }
        }

        public async Task<decimal> GetBalanceAsync(string uuid, CancellationToken ct, IClientSessionHandle s = null)
        {
            AccountEntry account;
            if (s == null)
            {
                account = await _accountCollection.Find(x => x.Uuid == uuid).FirstOrDefaultAsync(ct);
            }
            else
            {
                account = await _accountCollection.Find(s, x => x.Uuid == uuid).FirstOrDefaultAsync(ct);
            }
            if (account == null)
            {
                throw new KeyNotFoundException($"Account '{uuid}' does not exist.");
            }

            return account.Balance;
        }

        private async Task<AccountEntry> AddBalanceAsync(string accountId, decimal amount, CancellationToken ct, IClientSessionHandle s)
        {
            if (amount <= 0)
            {
                throw new ArgumentException("Amount should > 0");
            }
            var filter = Builders<AccountEntry>.Filter.Eq(x => x.Uuid, accountId);
            var update = Builders<AccountEntry>.Update
                .Inc(x => x.Balance, amount)
                .Set(x => x.UpdateTime, DateTime.UtcNow);
            var options = new FindOneAndUpdateOptions<AccountEntry> { ReturnDocument = ReturnDocument.After, IsUpsert = true };
            return await _accountCollection.FindOneAndUpdateAsync(s, filter, update, options, cancellationToken: ct);
        }

        private async Task<AccountEntry> DecreaseBalanceAsync(string accountId, decimal amount, CancellationToken ct, IClientSessionHandle s)
        {
            if (amount <= 0)
            {
                throw new ArgumentException("Amount should > 0");
            }
            var filter = Builders<AccountEntry>.Filter.And(
                Builders<AccountEntry>.Filter.Eq(x => x.Uuid, accountId),
                Builders<AccountEntry>.Filter.Gte(x => x.Balance, amount)
            );
            var update = Builders<AccountEntry>.Update
                .Inc(x => x.Balance, -amount)
                .Set(x => x.UpdateTime, DateTime.UtcNow);
            var options = new FindOneAndUpdateOptions<AccountEntry> { ReturnDocument = ReturnDocument.After };
            return await _accountCollection.FindOneAndUpdateAsync(s, filter, update, options, cancellationToken: ct);
        }

        public async Task<TransEntry> GetTransactionByTidAsync(string tid, CancellationToken ct, IClientSessionHandle s = null)
        {
            if (s == null)
            {
                return await _transCollection.Find(x => x.Tid == tid).FirstOrDefaultAsync(ct);
            }
            return await _transCollection.Find(s, x => x.Tid == tid).FirstOrDefaultAsync(ct);
        }

        public async Task<List<TransEntry>> GetTransactionsAsync(string accountId, CancellationToken ct, int n = 10)
        {
            var result = await _transCollection.Find(x => x.Uuid == accountId).SortBy(x => x.CreateTime).Limit(n).ToListAsync(ct);
            return result;
        }

        public async Task<TransResult> RechargeAsync(string accountId, string tid, decimal amount)
        {
            using (var session = await _mongoClient.StartSessionAsync())
            using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
            {
                return await session.WithTransactionAsync(
                    async (s, ct) =>
                    {
                        var existingTransEntry = await GetTransactionByTidAsync(tid, ct, s);
                        if (existingTransEntry != null)
                        {
                            return new TransResult { Code = TransCode.DuplicateTrans };
                        }

                        var newBalanceEntry = await AddBalanceAsync(accountId, amount, ct, s);

                        var newTransEntry = new TransEntry
                        {
                            Tid = GenerateTid(),
                            Uuid = accountId,
                            Amount = amount,
                            NewBalance = newBalanceEntry.Balance,
                            Type = TransType.Recharge,
                            CreateTime = DateTime.UtcNow,
                            UpdateTime = DateTime.UtcNow,
                        };

                        await _transCollection.InsertOneAsync(s, newTransEntry, cancellationToken: ct);

                        return new TransResult
                        {
                            NewBalance = newTransEntry.NewBalance,
                            Tid = newTransEntry.Tid
                        };
                    }, cancellationToken: cts.Token);
            }
        }

        public async Task<TransResult> PurchaseAsync(string accountId, string tid, decimal amount)
        {
            using (var session = await _mongoClient.StartSessionAsync())
            using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
            {
                return await session.WithTransactionAsync(
                    async (s, ct) =>
                    {
                        var existingTransEntry = await GetTransactionByTidAsync(tid, ct, s);
                        if (existingTransEntry != null)
                        {
                            return new TransResult { Code = TransCode.DuplicateTrans };
                        }

                        var newBalanceEntry = await DecreaseBalanceAsync(accountId, amount, ct, s);
                        if (newBalanceEntry == null)
                        {
                            return new TransResult { Code = TransCode.InsufficientBalance };
                        }

                        var newTransEntry = new TransEntry
                        {
                            Tid = GenerateTid(),
                            Uuid = accountId,
                            Amount = amount,
                            NewBalance = newBalanceEntry.Balance,
                            Type = TransType.Purchase,
                            CreateTime = DateTime.UtcNow,
                            UpdateTime = DateTime.UtcNow,
                        };

                        await _transCollection.InsertOneAsync(s, newTransEntry, cancellationToken: ct);

                        return new TransResult
                        {
                            NewBalance = newTransEntry.NewBalance,
                            Tid = newTransEntry.Tid
                        };
                    },
                    cancellationToken: cts.Token);
            }
        }

        public async Task RunAsync()
        {
            var random = new Random(42);

            // Create 10 accounts
            var accountIds = new List<string>();
            for (int i = 0; i < 10; i++)
            {
                var accountId = GenerateAccountUuid();
                var account = await CreateAccountAsync(accountId);
                accountIds.Add(account.Uuid);

                await RechargeAsync(account.Uuid, GenerateTid(), 1000);
                Console.WriteLine($"Created account {account.Uuid} with initial balance of 1000.");
            }

            Console.WriteLine("------------------------------------------------------------------------------");

            foreach (var accountId in accountIds)
            {
                for (int i = 0; i < 5; i++)
                {
                    var isRecharge = random.Next(0, 2) == 0;
                    var amount = (decimal)(random.NextDouble() * 300);
                    var tid = GenerateTid();

                    if (isRecharge)
                    {
                        var result = await RechargeAsync(accountId, tid, amount);
                        if (result.Code == TransCode.Success)
                        {
                            Console.WriteLine($"Recharge: Account {accountId}, Amount: {Math.Round(amount, 2)}, New Balance: {Math.Round(result.NewBalance, 2)}, Tid: {tid}");
                        }
                        else
                        {
                            Console.WriteLine($"Recharge failed: {result.Code}");
                        }
                    }
                    else
                    {
                        var result = await PurchaseAsync(accountId, tid, amount);
                        if (result.Code == TransCode.Success)
                        {
                            Console.WriteLine($"Purchase: Account {accountId}, Amount: {Math.Round(amount, 2)}, New Balance: {Math.Round(result.NewBalance, 2)}, Tid: {tid}");
                        }
                        else
                        {
                            Console.WriteLine($"Purchase failed: {result.Code}");
                        }
                    }
                }
            }

            Console.WriteLine("------------------------------------------------------------------------------");
            foreach (var accountId in accountIds)
            {
                var balance = await GetBalanceAsync(accountId, CancellationToken.None);
                var transactions = await GetTransactionsAsync(accountId, CancellationToken.None);

                Console.WriteLine($"Account {accountId} Balance: {Math.Round(balance, 2)}");
                Console.WriteLine("Recent Transactions:");
                foreach (var transaction in transactions)
                {
                    Console.WriteLine($"Tid: {transaction.Tid}, Type: {transaction.Type}, Amount: {Math.Round(transaction.Amount, 2)}, New Balance: {Math.Round(transaction.NewBalance, 2)}, Time: {transaction.CreateTime}");
                }
                Console.WriteLine("****************************************************************************");
            }
        }
    }
}