Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent concurrent transactions #1158

Closed
devformatters opened this issue Jun 8, 2020 · 2 comments
Closed

Prevent concurrent transactions #1158

devformatters opened this issue Jun 8, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@devformatters
Copy link
Contributor

Here are flows for preventing concurrent transactions to Postgres:

queue

Couple things to be clarified since flows are in sync style:

  • Pending transaction are always returned, which means client side need to revise feedback message. Client get notices when transaction state has been updated.
  • Transaction history must includes pending transaction.

Any supplementation to those flows? @robertu7 @guoliu

cc @gyuetong

@devformatters devformatters added the enhancement New feature or request label Jun 8, 2020
@devformatters devformatters changed the title Prevent concurrent transaction Prevent concurrent transactions Jun 8, 2020
@devformatters
Copy link
Contributor Author

To test queue solution, we need stress testing tool. I'm using k6, written in Golang, supports JS scripts to test.

Installation (mac)

brew install k6

Project structure

folder
├── appreciation.js
├── payout.js
├── payto.js
└── shared.js

You can download scripts from here:

Run scripts

Please follow the instruction in each scripts to setup, and then run the script with k6. Like this:

k6 run appreciation.js
k6 run payout.js
k6 run payto.js

Verify result

Appreciation

After rounds of execution, you should see each user can only have 5 appreciations to the article.

Payout

After execution, you should see user's HKD balance is always >= 0 by running SQL:

select sum("src"."amount") as "amount" from (
  select
    sum(amount) as amount
  from
    "transaction"
  where
    recipient_id = ${id} and currency = 'HKD' and state = 'succeeded'
  union
  select
    sum((0 - amount)) as amount
  from
    "transaction"
  where
    sender_id = ${id} and currency = 'HKD' and (state = 'succeeded' or state = 'pending')
) as src

and list transactions:

select * from "transaction" where "sender_id" = ${id} or "recipient_id" = ${id} order by created_at desc

Pay-to

After running script, you should see users's HKD balance is always >= 0, and transactions might be succeeded or canceled by running SQL queries above.

@guoliu
Copy link
Contributor

guoliu commented Aug 3, 2020

Implemented with #1115 and #1165

@guoliu guoliu closed this as completed Aug 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants