2009年4月27日 星期一

Advanced Rails , Database Chapter memo(2)

Large/Binary Objects

Database Storage
Sooner or later, many web applications must deal with the issue of LOB (large object) data.

LOB storage is usually divided into CLOB (character large object) for text data and
BLOB (binary large object) for everything else. Some DBMSs separate the two as separate
data types. CLOB types can often be indexed, collated, and searched; BLOBs
cannot.



PostgreSQL
My recommendation is to use filesystem storage for all binary objects if you use
PostgreSQL. Although the database might be the more proper place for this type of
data, it just does not work well enough yet.


MySQL

MySQL suffers from issues similar to PostgreSQL with streaming data, and it is always more awkward for a web application to stream data from the database than from the filesystem.



Filesystem Storage

The reality is that filesystem storage is the best option, as a general rule. Filesystems
are optimized to handle large amounts of binary and/or character data, and they are
fast at it.



Why Is Filesystem Storage So Fast?
the secret to this performance, under Linux and various BSDs, is the kernel
sendfile( ) syscall (not to be confused with X-Sendfile, discussed later). The sendfile( )
function copies data quickly from a file descriptor (which represents an open file) to a
socket (which is connected to the client). This happens in kernel mode, not user mode—
the entire process is handled by the operating system. The web server doesn’t even have
to think about it. When sendfile( ) is invoked



Sending Data with X-Sendfile
Often you will need to send a file to the client for download after doing some processing
in Rails. The easy way to do this is with the send_file or send_data API calls, which stream data from the server to the client:



The X-Sendfile protocol is a very simple standard, first introduced in the Lighttpd
web server, which directs the web server to send a file from the filesystem to the client
rather than a response generated by the application server. Because the web
server is optimized for throwing files at the client, this usually yields a decent speed
improvement over reading the file into memory and sending it from Rails with the
send_file or send_data API calls.

X-Sendfile uses the X-Sendfile HTTPheader pointing to the server’s path to the file
to send, in conjunction with the other standard HTTPheaders. A typical response
using X-Sendfile would look something like this:

From Rails, you can set the response headers by modifying the response.headers hash:




Web server configuration
Of course, the front end web server must be properly configured to recognize and
process the X-Sendfile header

沒有留言: