如何在非阻塞系统中处理货币交易?

How to handle money transactions in non-blocking system?

我在做广告服务器(类似于 RTB),有些广告商付费宣传他们的广告活动。

当用户观看广告时我要向广告商收费。

Ad campaigns should participating auctions without blocking,意味着他可以同时为多个广告请求出价。因为我必须冻结他的余额,所以很难立即向广告客户收费。

另一种方法是不立即向他收费,而是在单独的过程中每 N 秒收费一次,并希望他没有购买超出他承受能力的展示次数。我可以设定他应该参加拍卖的某种门槛信用,这将消除大部分超支,但如果退出流程并且广告商没有被收取费用和超支,那将是一个问题。

有人可以告诉我这些事情通常是如何处理的,也许可以推荐一些关于这个话题的 book/article,好吗?

如果这是异步编程的问题,这意味着您担心当投标人一次设置过多的展示出价时他们会向他们收取过高的费用。然后我建议使用基于 mutex (i.e. locking system). You can set a threshold on the number of locks a bidder can have in your system at any given time. This threshold can be equivalent to the maximum amount the bidder is willing to spend on impressions. When the bidder requests a bid a lock for that bid on his account should be sent to your server. When the server responds with the ok response that the lock has been created, then the bid can take place. The bid is active until the lock is released by your server. This makes it so the server keeps track of all the bids and places locks on all of them. If the bidder has a threshold of 10 bids and he tries to make a bid for the 11th the server will not release a lock to the bidder to make this bid. Furthermore, if you are using a microservice architecture I suggest making a transaction manager service to handle all these requests and even use Saga 的事务在服务器出现故障时进行功能回滚。