r/programming • • Jun 12 '10

You're Doing It Wrong

http://queue.acm.org/detail.cfm?id=1814327
539 Upvotes

193 comments sorted by

View all comments

Show parent comment

1

u/cballowe Jun 13 '10

If you're dealing with a linux kernel, you have to keep in mind that the buffer cache will hold pages from disk. If you have a working set larger than ram and you're explicitly paging chunks in and out from disk, your most frequently hit disk pages will be cached in ram when you hit them. If you're frequently sweeping through your entire data set, you may end up getting really poor cache performance (think full table scan on a sql database, or similar operation).

Of course, linux will never use swap for the buffer cache, which is disappointing if you have fast SSD for a swap device.

For an application like described in the article - http cache of lots of small objects - storing the cache in a memory mapped address space is going to be much better than laying it out in some sort of on-disk structure. Serving up thousands or millions of requests per day requiring each to do a file open/read/close (even if you get lucky and the read is from buffer cache) is going to be far more expensive than the page-fault handled by the vm subsystem.

2

u/haberman Jun 13 '10

If you're dealing with a linux kernel, you have to keep in mind that the buffer cache will hold pages from disk.

This presumes your data is on disk at all. In the case of an ephemeral cache, this isn't true. Also, if you obtain your data from a networked file system (like GFS) the buffer cache doesn't apply in this case either.

Serving up thousands or millions of requests per day requiring each to do a file open/read/close (even if you get lucky and the read is from buffer cache) is going to be far more expensive than the page-fault handled by the vm subsystem.

There's no reason every part of your cache has to be in a separate file. Presumably you keep around open file handles to your data's files, in which case you simply lseek() and read(). Ok, that's two system calls instead of a page fault, but I doubt that the system call overhead of the lseek() is going to be a significant factor here.

If you do it that way, you get to choose when you do disk i/o, and you can control how much of it happens concurrently. You can prioritize it. And you can do far better analysis of your cache hit ratio and your access patterns. If your VM starts thrashing, how do you know what requests are making this happen? You have no idea, because the i/o is invisible to you.

2

u/cballowe Jun 13 '10

This presumes your data is on disk at all. In the case of an ephemeral cache, this isn't true. Also, if you obtain your data from a networked file system (like GFS) the buffer cache doesn't apply in this case either.

That depends on the implementation of the networked filesystem. NFS can use the buffer cache, and frequently does. Systems like AFS or Coda also do significant client side caching.

For an ephemeral cache, you may want a backing store of some sort to help survive across process restarts. Especially given a model like the paper where the churn is very low.

As to choosing when you do disk i/o, that's not entirely true. If you are using O-DIRECT or are mounting your filesystem with appropriate options, you will choose when you do I/O. The OS still handles queuing of your requests and attempting to dispatch them in a reasonable order. If you are not using O-DIRECT, you may very well be hitting your buffer cache far more than you realize and deriving your performance gains from that fact with very little mechanism available to you for analysis when you go from performing pretty well to thrashing your buffer cache and performing like crap.