views:

744

answers:

7

Can I set up an IIS server so that it will cache the most frequently used static files (binary) from disk into RAM, and serve from RAM on request?

Update: mod_mem_cache in Apache Caching Guide seems to be what I'm looking for. Any equivalent thing in IIS?

Thanks.

A: 

You may be able to setup a ram drive, and then move your files there and setup IIS virtual directory there.

Nate Bross
I was going to suggest that.
Joel Coehoorn
sorry, RAM drive is not what i'm looking for, since it is not smart enough to cache most frequently used files.
Henry
A: 

Set up a RAM Disk if you have lots of RAM

http://www.tweakxp.com/article37232.aspx links to a free one. Have your application copy the relevant files to that drive and set your wwwroot to point at that location.

This data is not safe between boots though.

Also I run a big IIS site and serve tons of static files. The windows file cache is fine and I get more problems on network latency. Time to first byte e.t.c. My disks are never bound. But ram disk will help if you have a known problem.

Stewart Robinson
You'd want to script the ram disk creation.
Joel Coehoorn
sorry, RAM drive is not what i'm looking for, since it is not smart enough to cache most frequently used files.
Henry
A: 

What Nate Bross said is probably the most reliable way to keep them in ram, assuming the RAM disk is dynamically created from a real disk somewhere at boot.

Additionally, you could set up an asp.net handler (*.ashx) for the files to use the cache built into ASP.Net. It would try to serve from the cache first and only load them if needed. This has the advantage of allowing you to easily expire the cache from the time to time if the file might occasionally change and allow IIS to re-claim that memory if it decides it needs it more for something else at the moment.

Joel Coehoorn
ASP.net handler? So I have to go all the way to the Application Server (ASP.net) to do caching? I don't want to use an App Server, I think there should be some file server specialized for serving file from RAM?
Henry
You dont' _need_ asp.net- this is just one option if you want some additional features (allowing the server to automatically reclaim the ram if it's needs it more elsewhere)
Joel Coehoorn
So I can't just ask IIS to cache the static files for me into their caching layer?
Henry
BTW: if you're really not at all interested in using app server, than this is more a sysadmin question than a programmer question and doesn't really belong on SO.
Joel Coehoorn
Sorry: that last comment came out wrong. The point was "probably you could, but the _programmers_ here won't necessarily know the best way: it's outside our area of expertise.
Joel Coehoorn
I understand what you mean.I'm not sure if Stackoverflow should only limit to programming questions, if it was meant to provide a service that competes with experts-exchange.
Henry
There's an IT version coming sometime soon.
Joel Coehoorn
A: 

In ASP.NET:

Response.Cache.SetExpires(DateTime.Now.AddDays(1));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);

This is to say, not that an ASP Solution is the best, but rather that IIS obeys the caching directives, an may opt to cache in RAM.

However, I believe you can signal IIS to cache a single file in the IIS management snap-in, if ASP/ASP.NET is not an option, by setting the content expiration 1 or more days in the future.

John Gietzen
"setting the content expiration 1 or more days" would ask IIS to cache the file to RAM? or does it only set the 1 day expiration in http header?
Henry
I believe that it manages all of that itself, and if it deems it necessary, it will cache it in RAM. That is to say: IIS obeys caching headers.
John Gietzen
The code you wrote that... doesn't that only apply to... asking IIS to cache an asp/aspx page? I do not want to use aspx?imgid=xxx to get an image.
Henry
Yeah, but it is possible to use URL rewriting to send all files in a folder to an ASHX page, which would perform the writing of the file. Anyways, I wasn't necessarily saying that an ASHX file is the best, just that IIS obeys any caching directives, and may cache in RAM.
John Gietzen
+1  A: 

IIS should be doing this already. In .Net this is what the output caching would do for you.

David
So how do I configure IIS to consume all xGB of RAM for caching the most frequently used static content? Or is the OS layer already cached that? I'm confused.
Henry
+1  A: 

Here's a bit on IIS 6.0's file cache: http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/a0483502-c6da-486a-917a-586c463b7ed6.mspx?mfr=true. As David mentioned, IIS is likely doing this for you already.

IIS 7.0 Output Caching

IIS 6.0 file cache behavior is included in IIS 7.0 output caching. You can define your own rules if the default timeout seems too short. Kernel Caching takes advantage of OS caching.

Corbin March
Thanks, I'd love to read IIS 7's File Cache document, if it exists.
Henry
"The IIS 6.0 file cache uses an algorithm that attempts to cache only... file requested at least twice in a 10-second activity period, or the file will not be cached.", this is not good.
Henry
IIS 7 output caching replaces IIS 6 file cache. You can set your own timeouts - see new links above.
Corbin March
Great, seems like all I need to do is set Maximum Cache Response Size to something larger than 256k, according to http://technet.microsoft.com/en-us/library/cc771895.aspx
Henry
+2  A: 

Even if IIS isn't actually set up to perform caching on its own, for true static files that are only loaded from disk and sent over the wire (i.e. images, .css, .js), you'll likely end up using the in-memory file cache built into Windows itself. In Task Manager, you'll notice a "System Cache" metric in the Physical Memory section; that shows you how much space the OS is using for the cache. So, as long as you're talking true static files, adding explicit caching is unnecessary.

Edit:
For more details, here's a couple links about the Windows cache (you probably could find more with Google):

iammichael
does "System Cache" in Windows Server aggressively cache the file to fill up all system RAM available?
Henry
Edited to add in some links that may help you find an answer to that part. I don't really know details on how the cache works.
iammichael
Although the OS can do file caching, I don't think that's optimal 'cause the OS can be caching other system files in RAM rather than the content that I need IIS to serve.
Henry
I'm not sure how that's not optimal. Yes, you can't make sure specific files are kept in the os cache, but windows is supposed to be smart enough to cache only those files that are accessed and needed. (cont)
iammichael
If you'd rather your static files be cached over other needed system files, you're going to end up hurting performance since those system files would need to be read from disk each time.
iammichael
good point, thank you!Any doc on Windows 2k8 File Cache? I can't find any.
Henry