dongqi0644 2019-07-06 00:45
浏览 77

在异步请求上从数据库获取延迟

I Have a table named 'transactions', and another named 'users'

When a user requests to the server, i get from transactions his last transaction balance and increase or decrease the balance base on his request,

all works good,

but problem is when he tries to request 10 times or more in a second without considering the response something like async requests, the speed of sending request is more than speed of getting balance from database, so i have problem to calculate the new balance.

something like this:

Section A: balance:100, amount 0, new balance: 100

Section B: balance(got from section A):100, amount:-10, new balance: 90

Section C: balance(got from last section):100(because section b is not finished yet to set balance to 90), amount:-10, new balance: 90

When this is happening at the milliseconds The balance get at the section C is the balance in section A, not balance in section B! because section B is not finished yet to call database set the balance,90,So this is problem

a better example:

Example

I know some solutions to handle this issue but i want to solve this problem in other way.

solutions i found:

1-Lock the database for current user at beginning of the section B and unlock it at the end of it and don't let section C starts until database(or user) is lock!

2-Throttle requests for each user,don't let users to send 10 request for example in a minute or something like that

For reasons i don't want to limit users from sending requests. and solution number 1 reduces performance of responding to users.

what algorithms do you recommend?

Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试