PHP: tmpfs vs memcached

I want to store information (sessions and lots of lines) in RAM, and I don't know if I should use tmpfs or memcached server. Someone made some benchmark and knows which one is faster? This is necessary for some ajax scripts that request information every 1-5 seconds for each user who logs in, such as webchat in PHP. Therefore, PHP often needs to connect to memcache.

The advantage of using tmpfs is that I can create many files and have structur (dirs), while I only have a key-value system in memcached, but there I can use arrays or objects to store information. CPU usage will also be interesting if there is any difference.

Thanks.

+4
source share
5 answers

Just a couple of points

  • tmpfs or ramdisk are more mature than memcached (were longer, but both are stable
  • tmpfs is scalable (you can resize or increase as needed without losing tmpfs content
  • memcahced is great if you need memory on another computer or you need to share this information between machines.
  • The performance of the local file / socket / channel is ALWAYS faster than the network socket, and access to the file in tmpfs is the same as any other file, so it does not require any third-party libraries.

If you do not expect data for our growth to have memory on your server, use tmpfs.

If you need to share data between servers or want to save more data that fits into RAM on your local server, use memcahed.

+4
source

I don't know about speed, but here are a few things to consider in memcached:

  • memcached is based on a cluster type architecture: you can add as many physical servers as you want, install the memcached daemon on it, and you will have more memory in your cluster
    • which means that the amount of data you can cache is practically unlimited using memcached: just add a couple of servers if you need more memory
    • with tmpfs, you are limited by the amount of RAM available on each server.
  • memcached - caching of mecanism; It is not intended for data storage; What means:
    • when there is not enough memory left to store a new item, old items are removed from the cache
    • each element has a lifespan; after this period the item is removed from the cache
  • memcached is generic: you can have multiple PHP servers to access a single memcached cluster.
  • There are many existing memcached-based libraries or created to access memcached
    • including some mecanism session for php
    • and several caching libraries
  • no memcached or tmpfs are saved for saving - if you need your data to be saved (i.e. still accessible even after a reboot), you need to use something like a database.


In the end, not sure about tmpfs, but I would probably use memcached, at least when it comes to:

  • session
  • caching

Why? Because it:

  • mature - used a lot, there are many libraries, ...
  • and scalable
+2
source

Both ramdisk and memcached are growing fast.

I do not think that speed will matter if you use MySQL in your problem.

I personally would prefer Redis instead of memcached.

Here are the pros / cons:

  • memcached can delete a key if there is no bar. Redis and files will never do this.

  • some software that Joomla cannot install if the sessions are not in files (e.g. memcached / redis)

  • both memcache and redis will be able to serve multiple php servers, so you cannot use stick sessions in a cluster.

  • memcached is faster, then it is redis, then it is ramdisk, then memcachedb, then mysql and then file system sessions.

  • ramdisk mimics the behavior of regular php sessions and does not need to be installed.

  • If ramdisk is not mounted, php will return to the file system and still work (assuming the server boots)

  • If memcached or redis stop working, php gives an unpleasant error and does not start at all.

Hope this helps.

0
source

The access record for the session data must be atomic or protected by a lock per session, otherwise it will be damaged. For file-based sessions, it was decided by locking the file, I donโ€™t know how memcached works with it. Using a separate ext4 partition is not as bad as you think - VFS will cache your I / O files in RAM , so you may not need anything else. You can fine tune ext4 (per partition) to cache your records, so you can get performance and RAM resistance for both large and RAM sizes. For example, you get a write-back cache and a 60 second window (with a hold delay) with this:

mount /dev/sda4 -t ext4 -o rw,data=writeback,nobh,commit=60 

It will work quickly and will not work outside of RAM; it will use all available RAM since the file system cache is dynamic. Try this, if you say a 4MB file several times over the second file (overwriting it), you will get a very fast write.

0
source

In fact, you can use memcacheDB for what you need.

-1
source

Source: https://habr.com/ru/post/1303351/


All Articles