Databases Reference
In-Depth Information
total of three seconds of network latency, which means that your page can be unac-
ceptably slow even if it's served without a single database access! Using a multi-get call
to the cache is absolutely vital in these situations. Using a cache hierarchy, with a smaller
local cache, can also be very beneficial.
Pregenerating Content
In addition to caching bits of data at the application level, you can prerequest some
pages with background processes and store the results as static pages. If your pages are
dynamic, you can pregenerate parts of the pages and use a technique such as server-
side includes to build the final pages. This can help to reduce the size and cost of the
pregenerated content, because you might otherwise duplicate a lot of content due to
minor variations in how the constituent pieces are assembled into the final page. You
can use a pregeneration strategy for almost any type of caching, including memcached .
Pregenerating content has several important benefits:
• Your application's code doesn't have to be complicated with hit and miss paths.
• It works well when the miss path is unacceptably slow, because it ensures that a
miss never happens. In fact, anytime you design any type of caching system, you
should always consider how slow the miss path is. If the average performance
increases a lot but the occasional request becomes extremely slow due to regener-
ating cached content, it might actually be worse than not using a cache. Consistent
performance is often as important as fast performance.
• Pregenerating content avoids a stampede to the cache when there's a miss.
Caching pregenerated content can take a lot of space, and it's not always possible to
pregenerate everything. As with any form of caching, the most important pieces of
content to pregenerate are those that are requested the most or are the most expensive
to generate, so you can do on-demand generation with the 404 error handlers we men-
tioned earlier in this chapter.
Pregenerated content sometimes benefits from being stored on an in-memory filesystem
to avoid disk I/O.
The Cache as an Infrastructure Component
A cache is likely to become a vital part of your infrastructure. It can be easy to fall into
the trap of thinking of a cache as a nice thing to have, but not something so important
that you can't live without it. You might reason that if the cache server goes down, or
you lose the cached content, the request will simply go to the database instead. This
might be true when you initially add the cache into the application, but the cache can
enable the application to grow significantly without increasing the resources dedicated
to some portion of the system—typically the database. As a result, you might become
dependent on the cache without realizing it.
 
Search WWH ::




Custom Search