Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Any way to ask Nginx to cache everything to RAM?
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Any way to ask Nginx to cache everything to RAM?

CoolMoonCoolMoon Member

A friend of mine just gave me a VPS with 128M RAM, but the disk I/O is terrible (dd test <20MB/s).

However the network I/O seems ok. So I was thinking about to host some static websites on it.

My plan is to install nginx without php or mysql, and try to load all the static files (html, css, js etc) to memory so that it might not suffer under the low disk I/O.

Is there any way to do that with nginx? Thanks.

Comments

  • AbdussamadAbdussamad Member
    edited June 2013

    Linux already caches disk access using RAM so you don't have to do anything. And anyway with just 128MB RAM you will struggle to cache anything.

  • flyfly Member

    send a very polite letter

  • MunMun Member
    edited June 2013

    20 MB/s is slow, send a support ticket in.

    exec this on your console

    dd if=/dev/zero of=test bs=64k count=16k conv=fdatasync; unlink test

    and send it to them.

  • My friend just gave me the root access to this fresh re-installed VPS, I even don't know who is the provider...

  • How does Linux choose which files to cache? It seems the provider disabled the cache function so it shows 0 under cache and buffer in "free -m"

  • jimaekjimaek Member
    proxy_cache_path /var/cache/nginx levels=2 keys_zone=mycache:512m inactive=365d max_size=10m;
    

    Something like this? It should use 512MB of RAM to cache files. Plus 10mb of HDD.

  • So there's nothing I can do with this VPS? Maybe that's why my friend dump it to me...

  • What if I just host small site with total file size <50MB, do I still need 512MB for cache? There are only a bunch of HTML files with a few CSS and JS. not many png files

  • jimaekjimaek Member

    @CoolMoon Well of course not. It was just an example. You can set whatever you want.

  • SpiritSpirit Member
    edited June 2013

    @CoolMoon said:
    What if I just host small site with total file size <50MB, do I still need 512MB for cache? There are only a bunch of HTML files with a few CSS and JS. not many png files

    I hosted two small websites + kloxo control panel at 96MB VPS (till recently Cinipac http://lowendtalk.com/discussion/comment/279905/#Comment_279905 decided to discontinue all LEB VPSs).
    What I am trying to say is that yes, you can host some low traffic website at low specs VPS. If it's static even easier.

  • jcalebjcaleb Member

    if low traffic maybe you can live with what you have? slow io and low ram

  • dd test results have little or no bearing on your VPS's ability to serve static files. Go ahead, install nginx and use it, you may be pleasantly surprised :)

    Even if your VPS isn't caching to RAM, the host node is. Though whether or not your files are in the host node's RAM cache will depend on many factors.

  • bcrlsnbcrlsn Member

    @coolmoon said: What if I just host small site with total file size <50MB, do I still need 512MB for cache? There are only a bunch of HTML files with a few CSS and JS. not many png files

    Put the CSS and JS on a cdn and put the static files on the VPS with nginx. I bet you'll get really good results. :)

  • Well, for little traffic static site that is more than enough. 20MB/s is not that really bad... i mean it is not that easy to create a site that uses more than that, unless hosting big files and images. A few kb html page needs a lot of concurrent visitors to overgrow the disk i/o.
    People tend to think that their websites gonna crash vps-s and they will be so popular because their ideas are really unique... well, most of the time that doesn't happen, and if yes - it happens only with time. As a startup, that vps should be enough if the uptime is good.

  • @jimaek said:
    proxy_cache_path /var/cache/nginx levels=2 keys_zone=mycache:512m inactive=365d max_size=10m;

    Something like this? It should use 512MB of RAM to cache files. Plus 10mb of HDD.

    This is a common misunderstanding. keys_zone refers to the metadata about the cache not the actual cached content:

    Cached data is stored in files. An MD5 hash of the proxied URL is used as the key for the cache entry, and is also used as the filename in the cache path for the response contents and metadata.

    http://wiki.nginx.org/HttpProxyModule#proxy_cache_path

    So in the above example you are setting the size of the cache metadata to 512MB. So two things here:

  • @CoolMoon said:
    How does Linux choose which files to cache? It seems the provider disabled the cache function so it shows 0 under cache and buffer in "free -m"

    The kernel does it automatically. It is very complex stuff that us mere mortals can't understand. If you want to try start by googling "memory management".

    Anyway post the output of free -m. Let's see what's going on.

  • SplitIceSplitIce Member, Host Rep

    srcache and memcache / redis will allow you to cache only to ram (with redis storing to disk asynchronously).

  • So you say nginx can't cache to ram? But it cache everything on disk?

    What if i create ramdisk and set the cache folder on the ramdisk?

  • 128Mb of RAM is way too little. However, you can anyway mount tmpfs as cache directory and specify it in Nginx config file.

  • As I said before the OS caches disk access using RAM so there is no need to manually do caching in RAM anymore. If you try you will only make things worse. Please read this page from the developer of Varnish that explains things in more detail:

    https://www.varnish-cache.org/trac/wiki/ArchitectNotes

  • @Master_Bo said:
    128Mb of RAM is way too little. However, you can anyway mount tmpfs as cache directory and specify it in Nginx config file.

    There is no guarantee that a tmpfs is actually in memory. It may be on disk, depending on the node's RAM utilization.

  • @sleddog said:
    There is no guarantee that a tmpfs is actually in memory. It may be on disk, depending on the node's RAM utilization.

    Of course. tmpfs tries to use RAM whenever possible. If no RAM is available - it writes to disk.

    If you need more guarantee, use something like memcached and much RAM. Otherwise, you have little choice.

  • SplitIceSplitIce Member, Host Rep

    Redis 2.4 (outdated) has virtual memory on disk support, it could meet your needs.

Sign In or Register to comment.