1

Would saving a large array in a PHP session be hard on the server? By "large" array, I mean an array that has perhaps 500 elements with each element having up to 100 fields.

I could have thousands of users doing this process several times / minute.

Andrew
  • 2,691
  • 6
  • 31
  • 47
  • Yes, it would. What's in the array? You might be able to solve your problem with JavaScript-based localStorage. – Blazemonger May 14 '13 at 17:41
  • I guess it may depends on whether you store your session data in files or in database... – Wiktor May 14 '13 at 17:42
  • It depends on your server configuration. Sessions are stored on the RAM and then HDD, and if you access them very often, it is inefficient .. use cookies, or a database .. – tobspr May 14 '13 at 17:42
  • if the array is being used with every request the cost to deserialize it is probably less then to recreate it. If it's only used sometimes it would make more sense to find a different way to persist it – Orangepill May 14 '13 at 17:44
  • I would like to somehow store the array in memory as an auto-save feature before I write to XML on the server. The array is a bunch of objects that hold properties (like size, position, color, etc...). There could be hundreds or even thousands of objects each having up to 100 properties or so. – Andrew May 14 '13 at 17:45
  • Sounds like you have two things going on here ... you need to keep a working copy of something until a time when it makes sense to put it in long term storage. If that is the case then I think sessions would be a good option so long as when you persist the data you unset the array in the session. also if file system access is causing a bottleneck you may want to use memcached for persisting your sessions. – Orangepill May 14 '13 at 17:50
  • Yes, exactly. What do you mean by "persist" the data? Sorry for my ignorance... – Andrew May 14 '13 at 17:55

1 Answers1

3

First, a brief intro to session handling in PHP:

When you open a session, a cookie is created that contains the ID of the session, and is sent to the client. PHP will then use the path defined in session.save_path to save a file using the id as filename ( reference ).

What does that mean in your case? It means you'll be creating an additional bottleneck (disk I/O is one of the slowest things in most setups) because you'll be writing/reading files all the time.

Database servers have tons of code to handle that kind of latency, so it might be very beneficial to just use a table in a database that has your serialized array as a string, keyed by an id in the $_SESSION.

Community
  • 1
  • 1
Nick Andriopoulos
  • 10,313
  • 6
  • 32
  • 56
  • So you're suggesting to write to the database instead of the session? The sounds fine but I would have thought this would be even more of a concern for performance, no? – Andrew May 14 '13 at 17:47
  • my actual suggestion would be to not request that much data in one go, but in all cases you should be requesting from a different mechanism than session. Database is the easiest, but you might want to checkout [memcached](http://memcached.org/) or a similar in memory caching mechanism. – Nick Andriopoulos May 14 '13 at 17:48