Saving State in Your THIL Scripts
Because each request of your THIL script runs completely independently - with the execution environment removed and rebuilt for each HTTP request event - any stateful data you wish to maintain must be done via an external database service. Forget using cached files or any other form of web-server based state maintenance techniques.
Thankfully, the age of SaaS database services (DBaaS) is upon us, and there is a range of providers which give you a wide range of options to save your data from THIL, and often over HTTP. Here’s a few of my favourites (but there are many more), all providers mentioned have a free tier:
- CouchDB: CouchDB is a NOSQL database with a simple HTTP API, reasonable speed and great potential for scalability and complex queries and operations. Here are two services offering CouchDB-based DBaaS: Cloudant and Iris Couch. With Cloudant, you can choose where you want to have your data located, which is great for THIL users, as you can run your THIL script in the Rackspace “ord” (Chicago) region, and have your data located in the same datacenter too - greatly decreasing network delays which make your script less responsive.
- Redis: a superfast in-memory datastore which THIL uses internally - chosen because of it’s speed, scalability and simplicity. There are a number of Redis DBaaS providers out there, here’s two which offer a useful free plan: http://garantiadata.com/ and http://redis4you.com/
- MongoDB: probably the most widely used NoSQL database option around, MongoDB is a great option with several DBaaS providers to choose from. mongolab.com offers the option of placing your data in the Rackspace ”ord” (Chicago), which is great for THIL users, with mongohq.com worth a look too.
- MySQL: if you’re looking for a casual MySQL hosting service which has a free tier, you’ll be looking for a while. I haven’t tried the following service, but in the interest of completion, if you are really needing a low-cost tiny MySQL solution, check out http://www.freemysqlhosting.net/ and let me know how it goes :)
- Other: for other ways to save data which will work well with THIL, you can try database.com, Dropbox’s Datastore API and Google’s Spreadsheet API.
So there’s a quick run-down for you. I’d love to hear back how you go :)