I noticed my PHP sessions folder is filled with millions of files with the same session data. For example let's assume its a 48 byte long string, content remains the same.

Let's assume there are approximately 1.5 million of these session files in /var/tmp/php/sessions

If I do the math right 48 bytes * 1500000 files / 1024 / 1024 = 68 MB approx.

But if I do the following I get a 6G result

du -ch

1) Why does it show 6G (approximately)? (Possible answer in my 2nd question below)

More checks : Sorting files in descending order of file size to check the largest file size. Individual file size is 4k

du -a | sort -n -r | head -n 10
6464600 .
4       ./sess_fffffABCDcdf9c312340f094d165678

I've replaced some characters in the filename above.

2) Why does it show 4k? This is related to file system block size. 6G usage makes sense a bit now. Reference : https://stackoverflow.com/questions/26666642/why-the-size-of-an-empty-directory-in-linux-is-4kb

3) If I go with Redis as a the session handle for PHP it will definitely be faster than disk but will it theoretically only use around 68 MB of data on the RAM, plus some overhead maybe?

4) Does Redis try to reduce actual memory usage if the the values stored on it are the same, i.e. same value repeated 1.5 million times for different session keys?

I've no control over the session and it's data and cannot prevent sessions from being created or remove their content even though it's repetitive.

I'm looking for confirmation on my first two questions and slightly more explanation with references on questions 3 and 4 if possible.

  1. It shows six gigabytes because of the minimum allocation block size on your filesystem is four kilobytes.
  2. Yes, it is related to FS block size.
  3. Redis will be faster than disk, although not that much faster because the data most likely is in the OS filesystem cache. It will use less memory than session storage on disk. However, the exact scale is tough to know, one would need to read the code in order to estimate it, or set a test system up and measure memory usage.
  4. A short google for "redis deduplication" showed some pages which indicate that Redis could be able to perform the task you described to make memory footprint smaller. I don't know about the actual implementation status of those features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.