views:

766

answers:

8

I need a simple way for multiple running PHP scripts to share data.

Should I create a MySQL DB with a RAM storage engine, and share data via that (can multiple scripts connect to the same DB simultaneously?)

Or would flat files with one piece of data per line be better?

+10  A: 

Flat files? Nooooooo...

Use a good DB engine (MySQL, SQLite, etc). Then, for maximum performance, use memcached to cache content.


In this way, you have the ease and reliability of sharing data between processes using proven server software that handles concurrency, etc... But you get the speed of having your data cached.

Keep in mind a couple things:

  1. MySQL has a query cache. If you are issuing the same queries repeteadly, you can gain a lot of performance without adding a caching layer.
  2. MySQL is really fast anyway. Have you load-tested to demonstrate it is not fast enough?
gahooa
+5  A: 

Please don't use flat files, for the sanity of the maintainers.

If you're just looking to have shared data, as fast as possible, and you can hold it all in RAM, then memcached is the perfect solution.

If you'd like persistence of data, then use a DBMS, like MySQL.

Ben S
A: 

I would say that the MySql DB would be better choice unless you have some mechanism in place to deal with locks on the flat files (and some way to control access). In this case the DB layer (regardless of specific DBMS) is acting as an indirection layer, letting you not worry about it.

Since the OP doesn't specify a web server (and PHP actually can run from a commandline) then I'm not certain that the caching technologies are what they're after here. The OP could be looking to do some sort of flying data transform that isn't website driven. Who knows.

OldTroll
Well, and a RAM layer is just RAM, flat files still talk to a disk subsystem. Unless you still have a RAM drive (from DOS days), which would be sorta the same, just no locking semantics. Owww. My head hurts now.
OldTroll
+2  A: 

Generally, a DB is better, however, if you are sharing a small, mostly static amount of data, there might be performance benefits (and simplicity) of doing it with flat files.

Anything other than trivial data sharing and I would pick a DB however.

EJB
A: 

If your system has a PHP cache (that caches compiled PHP code in memory, like APC), try putting your data into a PHP file, as PHP code. If you have to write data, there are some security issues.

+1  A: 

If you're running them on multiple servers, a filesystem-based approach will not cut it (unless you've got a consistent shared filesystem, which is unlikely and may not be scalable).

Therefore you'll need a server-based database anyway to allow the sharing of data between web servers. If you're serious about either performance or availability, your application will support multiple web servers.

MarkR
A: 

I need a simple way for multiple running PHP scripts to share data.

APC, and memcached are both good options depending on context. shared memory may also be an option.

Should I create a MySQL DB with a RAM storage engine, and share data via that (can multiple scripts connect to the same DB simultaneously?)

That's also a decent option, but will probably not be as fast as APC or memcached.

Or would flat files with one piece of data per line be better?

If this is read-only data, that's a possibility -- but may be slower than any of the options above. Especially if the data is large. Rather than writing custom parsing code, however, consider simply building a PHP array, and include() the file.

If this is a datastore that may be accessed by several writers simultaneously, by all means do NOT use a flat file! Writing to a flat file from multiple processes is likely to lead to file corruption. You can lock the file, but you risk lock contention issues, and long lock wait times.

Handling concurrent writes is the reason applications like mysql and memcached exist.

Frank Farmer
A: 

1- Where the flat file can be usefull: Flat file can be faster than a database, but in very specific applications. They are faster if the data is read from start to finish without any search or write. If the data dont fit in memory and need to be read fully to get the job done, It 'can' be faster than a database. Also if there is lot more write than read, flat file also shine, most default databases setups will need to make the read queries wait for the write to finish in order maintain indexes and foreign keys. Making the write queries usually slower than simple reads.

TD/LR vesion: Use flat files for jobs based system(Aka, simple logs parsing), not for web searches queries.

2- Flat files pit falls: If your going with a flat file, you will need to synchronize your scripts when the file change using custom lock mechanism. Which can lead to slowdown, corruption up to dead lock if you have a bug.

3- Ram based Database ? Most databases have in memory cache for query results, search indexes, making them very hard to beat with a flat file. Because they cache in memory, making it run entirely from memory is most of the time ineffective and dangerous. Better to properly tune the database configuration.

If your looking to optimize performance using ram, I would first look at running your php scrips, html pages, and small images from a ram drive. Where the cache mechanism is more likely to be crude and hit the hard drive systematically for non changing static data.

Better result can be reach with a load balancer, clustering with a back plane connections up to ram based SAN array. But that's a whole other topic.

5- can multiple scripts connect to the same DB simultaneously?

Yes, its called connection pooling. In php (client side) its the function to open a connection its mysql-pconnect(http://php.net/manual/en/function.mysql-pconnect.php). You can configure the maximum open connection in php.ini I think. Similar setting on mysql server side define the maximum of concurrent client connections in /etc/mysql/my.cnf.

You must do this in order to take advantage of parrallel processessing of the cpu and avoid php script to wait the query of each other finish. It greatly increase performance under heavy load.

There is also one connection pool/thread pool in Apache configuration for regular web clients. See httpd.conf.

Sorry for the wall of text, was bored. Louis.

Louis