Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to choose to store just one integer? Sqlite? or Text file?

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.

like image 472
rogeriopvl Avatar asked Jan 25 '09 00:01

rogeriopvl


2 Answers

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.

like image 129
Javier Avatar answered Nov 14 '22 23:11

Javier


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.

like image 32
Mitch Wheat Avatar answered Nov 14 '22 22:11

Mitch Wheat