J
J
jiroler2018-09-10 22:35:55
linux
jiroler, 2018-09-10 22:35:55

PHP. How to implement a shared counter between multiple scripts?

Several daemons (endless php loops) are constantly running on a dedicated server + new ones are periodically launched + several scripts on the crown.
To implement some algorithm, it is necessary to use a common counter for each script/process while they are running. For example:

  • Daemon 1 : Counter = 1
  • Daemon 1 : Counter = 2
  • Daemon 3 : Counter = 3
  • Daemon 5 : Counter = 4
  • Daemon 2 : Counter = 5
  • Cron 1 : Counter = 6
  • Cron 2 : Counter = 7
  • Daemon 2: Counter = 8
  • Daemon 1 : Counter = 9
  • Daemon 1 : Counter = 10
  • Daemon 13 : Counter = 11
  • Cron 3 : Counter = 12

The use of the counter value may be needed in a very short period of time between several scripts, and with each use it should be 1 more than the previous one.
I tried to maintain the value in the file - at some points the value is simply not read and everything goes astray (I could not figure out the reason)
DB ( MySql) I'm afraid to use, because the calls will be very frequent and the frequency is growing.
Please tell me the best solutions.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Ezhgurov, 2018-09-10
@eandr_67

For example, like this: php.net/manual/en/memcached.increment.php

T
ThunderCat, 2018-09-10
@ThunderCat

I tried to maintain the value in the file - at some points the value is simply not read and everything goes astray (I could not figure out the reason)
most likely a banal blocking of an open file.
I'm afraid to use a database (MySql), because the calls will be very frequent and the frequency is growing.
well, I don't know, requesting an indexed field from the database takes about 0.0013 seconds on my not the most powerful machine, + non-blocking updates. In production, there are machines processing up to ~ 100 requests per second. Another thing is that this is shooting sparrows from a cannon.
As the best option IMHO - memkesh, or radish.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question