I've build a small web-service in PHP. I want to control the number of calls to a specific API method.
I thought at first of using a text file, because it's just an integer. But after taking a good look at SQLite it seemed much more convenient. So the code is just: get the "counter" from SQLite and increment it when that method is called, then store it back on SQLite db.
Is this a right way of doing it? Would it be simpler and more scalable to just use a file or... maybe something else?
Thanks in advance.
a third possibility: memcachedb. it's compatible with memcached, but stores it's key-value store to a BDB file. it not only has read-write commands, but also atomic increment/decrement for numeric values.
one more alternative would be to write a 'counter server'. an independent process that gets 'read' and 'increment' commands over a socket. the advantage is that it's really easy to do 'atomic' increments. it stores it's counter(s) on a simple file, without concurrency problems since there's never more than one server.
it should be easy to write in less than a hundred lines of C. just a tight loop processing one command at a time, flushing to disk every few seconds. since the processing is so simple, the latency is minimal.
A database would typically scale better than the filesystem, but you are going to need to be careful and use appropriate locks with either mechanism.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With