Teach Varnish to write letters

We've had people typing response letters for quite a while, but it's slow and costly, and the only difference in the letters is the greeting by name. We also can't change it to an impersonal response like "Dear Customer". Here's an example:

Request Response
Dear Varnish,
  Please give me the information I seek.
Thank you,
Joshua
Dear Joshua,
  Here is the information you requested. ...
All the best,
Varnish (21 May 2012)
Dear Varnish,
  Please give me the information I seek.
Thank you,
Caleb
Dear Caleb,
  Here is the information you requested. ...
All the best,
Varnish (21 May 2012)

So how can we make this more efficient? Let's make lots of copies of the letter without the name, leaving a hole in it's place. Then when we get a request we'll use a special process to lift the name off the page and place it into the response.

Blank copy Personal response
Dear        ,
  Here is the information you requested. ...
All the best,
Varnish (21 May 2012)
Dear Joshua,
  Here is the information you requested. ...
All the best,
Varnish (21 May 2012)

Now because of the date we can't use this for months at a time. Each day we'll have a person type up a fresh blank with the current date, then make as many copies as we need throughout the day. These examples are short, but what if each response was 1500 words?

So what does this have to do with the web? Many websites are created using software that stores the content in a database. Later when someone views a page the software creates a complete HTML page on the fly every time the page is requested. The scenario is not unlike the 1500 word example above. I want to use the Varnish Cache web application accelerator to make the process more efficient The information on the page changes infrequently, but the page is often personalized. The situation is pretty bad when some JavaScript libraries (like jQuery) make AJAX requests using JSONP, because every request includes a callback function uniquely named with the current timestamp, which means nothing can be cached. The goal is to be able to remove the callback function from the request (and store it), make the JSON (not JSONP) request, cache the response in Varnish, then use ESI as the special process to paste the callback function back into the response. This is what I was working through when I tweeted the solution I used in my VCL:

set req.url = regsub(req.url,
 "((\?)jsonp=[^&]*$|(\?)jsonp=[^&]*&|(\?.*)&jsonp=[^&]*)", "\3\4");

I found that the difficult part was writing the Regexp to remove the JSONP function name from the request URL. Here are the test matches I used:

Before After Match and remove
service service
service? service?
service?jsonp=jsonp1337198006253 service ?jsonp=jsonp1337198006253$
service?jsonp=jsonp1337198006253&c=3 service?c=3 jsonp=jsonp1337198006253&
service?a=1 service?a=1
service?a=1&jsonp=jsonp1337198006253 service?a=1 &jsonp=jsonp1337198006253
service?a=1&jsonp=jsonp1337198006253&c=3 service?a=1&c=3 &jsonp=jsonp1337198006253

So how much faster is it? If the web server takes 100ms to put the HTML together and Varnish can deliver the cached version through ESI in 33us, it's like comparing the 1500 word letter it takes someone 50 minutes to type with a machine stamping out ten response letters every second. How fast is 33us? Varnish can create 30,000 personalized responses per second!