Using API calls / functions or switching to work with Cron. Researching transactions. How do you handle this?

I am starting a new project with PHP and MySQL which is something like this:

Every minute I get a list of transactions from the API from different users.

Example:

user1 send $1 to user2
userx send $2 to usera
userw send $0.50 to user2

etc..

      

Let's say user1 wants to send $ 1 to user2. There are two possibilities: success or failure because insufficient funds are available, or the user entered the wrong username. If that fails, I send a message to the user.

Now I am faced with several options:

Option 1

Create a database table with transactions to be processed and use a cronjob that processes them every minute. The risk here is that the script might run in the event of an error or timeout, and other transactions will still appear in the process in the database table. So I'll need a second script to check this against the timestamp.

Option 2

Create an API or function that will be called for every transaction after I receive it and will give me a response. Then I can call another API or function to process this response, or move on to the next transaction. However, I will still have to put them in the database table, as I cannot risk losing them if the script stops executing. So it will work like this: put all transactions in the database table - start a transaction - when finished deleting a transaction from the table - start transaction 2.


Both options are wrong, because you don't know how long the list of transactions will last. If it's a long time, PHP is definitely not optimal to run for long periods of time - using a timeout of zero is risky. I am looking to create a solution that will scale with PHP. So I was thinking about Option 3.


Option 3 (Optimal solution?)

Use the API to return 10 transactions.

In the database, check the box to report that they were sent to the script and a timestamp to report when they were sent.

PHP script receives 10 transactions from API - processes 10 transactions.

After the transaction completes, delete it from this table and copy it to the completed transactions table.

The cronjob script checks every 3 minutes if the submit time is greater than X minutes (based on the upper bound for 10 transactions). If it is larger, set them to "Not sent" so that they can be sent again.


As you can see, I have written down my entire thought process on this matter and am looking for information. There must be things I missed. Also, please understand that these are not real financial transactions - they are simply the best metaphor I could use to clarify the situation.

+1


source to share


2 answers


If you want to do this using minimal deviation from the current technology stack, you are on the right track. Basically, you are re-creating the MQ or boneless job server.

The minimum features required for a job / task / transaction queue are:

  • job (user1 sends $ 2 to user2)
  • condition (ready, out for processing, error, done)

You probably want to too



  • last line of error (so you can figure out what the hell happened)
  • possibly a retry counter (for tasks that should be repeated before failing, anything that could fail due to temporary errors)

If you choose to parallelize your cron processing job, you will need to keep track of which script instance has the job to process, especially if you are starting out with large batches. (and if you do, you will want to make sure that any given transaction ends quickly, or you can effectively stop all jobs in one slow job)

Whether you get it from the API or straight from the database - 6 from one, half a dozen others.

+1


source


I suggest to process each transaction when a request for that transaction is received.

IE Bob clicks the Send Money button to send $ 20 to Alice. I would call a function to handle this transaction and show them the message "Transaction successful" or "There was a problem ..".



If it takes a long time to process transactions, you can do it via AJAX by showing them a nice progress bar saying "Processing .." when processing is done on the server side and then either redirecting them to yoursite.com/transactions.php?result=success&id=$id

oryoursite.com/transactions.php?result=failure&id=$id

0


source







All Articles