good_evening
good_evening

Reputation: 21749

Is it okay to save lots of information in $_SESSION?

I need to store many arrays in $_SESSION to prevent retrieving information from MySQL. Is it okay? How much is "too much" information in $_SESSION or there isn't any "too much"? Thanks.

P.S. Or it's better to use http://php.net/manual/en/book.memcache.php ?

Upvotes: 9

Views: 4687

Answers (5)

hakre
hakre

Reputation: 197777

The limit of data you can store inside a session is limited by the session storage layer. The default sessions store is the file-system and one session is stored into one file. The name of the session variable/array-key is stored as well as it's data in a serialized form. A pipe symbol separates variable names and values from each other.

If you're storing arrays with strings, then the file will be similar large than the length of the strings plus the length of the keys and a little overhead for meta data as well as the size of the variable names.

The size of a file is limited by the file-system. For example, in EXT3 this are 16 Gigabyte per file. So this is one "too much". You can not store more data into a session than the storage layer allows you to.

The next limit I can think of is the one of your memory. As PHP needs to load the data from the file into the memory and stores it from memory to the file at the end of the request. So if you've got a memory limit in PHP then this will actually limit as well the size of your session. For example, there is a standard memory limit of 16MB in PHP 5.2, but this may vary with your installation.

Using the whole memory for the session only does not make much sense by the way.

Next to these hard limits there might be performance limits which are related to the number of congurent requests, how fast your harddisk is etc.

As your question is pretty short I assume you didn't run into any concrete problems so far, so I think it would be out of scope. E.g. using memcached if you don't really need is would be only overhead. As well as discussing design decisions (never cache in sessions) which can not be answered in general at all.

The 100 or 200 Kilobyte per session (locate the session directory on your system and take an actual look how large the files are becomming) should not break your program. As suggested you should take care that old session files that aren't needed any longer get removed after a certain period of time automatically.

To learn more about your session configuration in PHP please see Session Runtime Configuration in the PHP Manual.

Upvotes: 4

Chad
Chad

Reputation: 255

A lot of what you are asking depends on how many users you are expecting and the type of hardware you use. Assuming your PHP configuration is using file storage for session information (the default), and you have plenty of tmp space, fairly large chunks of data can be stored in the session.

Personally I have stored a dozen or so data objects (typically database results pertaining to the user) in sessions. The site was on a company Intranet serving about ten thousand requests an hour. It was fast and the load was low. If I had to guess each session was near 100KB.

I would not recommend storing much more than a megabyte or two in a session. You will also need to make sure PHP and Apache (IIS, whatever) are cleaning up after themselves too. If you store a lot of session data temp space will fill quickly.

If you really want to make something like this blazing fast, and have the cash, go for a SSD disk drive. That would make retrieval even faster.

Hope this helps.

Upvotes: 2

Chris Henry
Chris Henry

Reputation: 12010

The amount of data in your session will have an effect on performance. If you intend to use files for session, note that php might hit a bottleneck at the disk. Reading, writing and serializing that data might incur heavy disk i/o, which is bad.

As far as a hard upper limit, I suspect those limits are the same as your filesystem's. For example, if you save massive amounts of data to session, and you fill up your disk, you'll not be able to write additional sessions.

Using memcached will alleviate the disk i/o performance issues, as writing and reading from memcached is much faster. Your limits there will be 1MB per session, as per the memcached spec. Your total space for sessions will be the size of the memcache instance you start (dictated by the -m flag when starting the memcache instance).

Upvotes: 1

0xAli
0xAli

Reputation: 1059

Take a look at this What can be the maximum size for the $_SESSION?

Upvotes: 2

tereško
tereško

Reputation: 58444

$_SESSION is not for caching.

If you need to cache stuff, then use APC , or Memcached or even Redis.

Upvotes: 1

Related Questions