Write some more docs
[blerg.git] / www / doc / index.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Blërg Documentation</title>
5 <link rel="stylesheet" href="/css/doc.css">
6 </head>
7 <body>
8
9 <h1>Blërg</h1>
10
11 Blërg is a minimalistic tagged text document database engine that also
12 pretends to be a <a href="/">microblogging system</a>.  It is designed
13 to efficiently store small (&lt; 64K) pieces of text in a way that they
14 can be quickly retrieved by record number or by querying for tags
15 embedded in the text.  Its native interface is HTTP &mdash; Blërg comes
16 as either a standalone HTTP server, or a CGI.  Blërg is written in pure
17 C.
18
19 <ul class="toc">
20   <li><a href="#installing">Installing</a>
21     <ul>
22       <li><a href="#getting_the_source">Getting the source</a></li>
23       <li><a href="#requirements">Requirements</a></li>
24       <li><a href="#configuring">Configuring</a></li>
25       <li><a href="#building">Building</a></li>
26       <li><a href="#installing">Installing</a></li>
27     </ul>
28   </li>
29   <li><a href="#design">Design</a>
30     <ul>
31       <li><a href="#motivation">Motivation</a></li>
32       <li><a href="#web_app_stack">Web App Stack</a></li>
33       <li><a href="#database">Database</a></li>
34       <li><a href="#problems_and_future_work">Problems and Future Work</a></li>
35     </ul>
36   </li>
37 </ul>
38
39 <h2><a name="installing">Installing</a></h2>
40
41 <h3><a name="getting_the_source">Getting the source</a></h3>
42
43 <p>There's no stable release, yet, but you can get everything currently
44 running on blerg.dominionofawesome.com by cloning the git repository at
45 http://git.bytex64.net/blerg.git.
46
47 <h3><a name="requirements">Requirements</a></h3>
48
49 <p>Blërg has varying requirements depending on how you want to run it
50 &mdash; as a standalone HTTP server, or as a CGI.  You will need:
51
52 <ul>
53 <li><a href="http://lloyd.github.com/yajl/">yajl</a> &gt;= 1.0.0
54 (yajl is a JSON parser/generator written in C which, by some twisted
55 sense of humor, requires ruby to compile)</li>
56 </ul>
57
58 <p>As a standalone HTTP, server, you will also need:
59
60 <ul>
61 <li><a href="http://www.gnu.org/software/libmicrohttpd/">GNU libmicrohttpd</a> &gt;= 0.9.3</li>
62 </ul>
63
64 <p>Or, as a CGI, you will need:
65
66 <ul>
67 <li><a href="http://www.newbreedsoftware.com/cgi-util/download/">cgi-util</a> &gt;= 2.2.1</li>
68 </ul>
69
70 <h3><a name="configuring">Configuring</a></h3>
71
72 <p>I know I'm gonna get shit for not using an autoconf-based system, but
73 I really didn't want to waste time figuring it out.  You should edit
74 libs.mk and put in the paths where you can find headers and libraries
75 for the above requirements.
76
77 <p>Also, further apologies to BSD folks &mdash; I've probably committed
78 several unconscious Linux-isms.  It would not surprise me if the
79 makefile refuses to work with BSD make.  If you have patches or
80 suggestions on how to make Blërg more portable, I'd be happy to hear
81 them.
82
83 <h3><a name="building">Building</a></h3>
84
85 <p>At this point, it should be gravy.  Type 'make' and in a few seconds,
86 you should have <code>http_blerg</code>, <code>cgi_blerg</code>,
87 <code>rss</code>, and <code>blergtool</code>.  Each of those can be made
88 individually as well, if you, for example, don't want to install the
89 prerequisites for <code>http_blerg</code> or <code>cgi_blerg</code>.
90
91 <h3><a name="installing">Installing</a></h3>
92
93 <p>While it's not required, Blërg will be easier to set up if you
94 configure it to work from the root of your website.  For this reason,
95 it's better to use a subdomain (i.e., blerg.yoursite.com is easier than
96 yoursite.com/blerg/).  If you do want to put it in a subdirectory, you
97 will have to modify www/js/blerg.js and change baseURL at the top.  The
98 CGI version should work fine this way, but the HTTP version will require
99 the request to be rewritten, as it expects to be serving from the root.
100
101 <h4>For the standalone web server:</h4>
102
103 <p>Right now, http_blerg doesn't serve any static assets, so you're
104 going to have to put it behind a real webserver like apache, lighttpd,
105 nginx, or similar.  Set the document root to the www directory, then
106 proxy /info, /create, /login, /logout, /get, /tag, and /put to
107 http_blerg.
108
109 <h4>For the CGI version:</h4>
110
111 <p>Copy the files in www to the root of your web server.  Copy cgi_blerg
112 to blerg.cgi somewhere on your web server.  Included in www-configs is a
113 .htaccess file for apache that will rewrite the URLs.  If you need to
114 call cgi_blerg something other than blerg.cgi, the .htaccess file will
115 need to be modified.
116
117 <h4>The extra RSS CGI</h4>
118
119 <p>There is an optional RSS cgi (called simply rss) that will serve RSS
120 feeds for users.  Install this like the CGI version above (on my server,
121 it's at /rss.cgi).
122
123
124 <h2><a name="design">Design</a></h2>
125
126 <h3><a name="motivation">Motivation</a></h3>
127
128 <p>Blërg was created as the result of a thought experiment: "What if
129 Twitter didn't need thousands of servers? What if its millions of users
130 could be handled by a single highly efficient server?"  This is probably
131 an unreachable goal due to the sheer amount of I/O, but we could
132 certainly do better.  Blërg was thus designed as a system with very
133 simple requirements:
134
135 <ol>
136 <li>Store and fetch small chunks of text efficiently</li>
137 <li>Create fast indexes for hash tags and @ mentions</li>
138 <li>Provide a HTTP interface web apps can use</li>
139 </ol>
140
141 <p>And to further simplify, I didn't bother handling deletes, full text
142 search, or more complicated tag searches.  Blërg only does the basics.
143
144 <h3><a name="web_app_stack">Web App Stack</a></h3>
145
146 <table class="pizzapie">
147 <tr><th>Classical model</th></tr>
148 <tr>
149   <td style="background-color: blue; color: white"><b>Client App</b><br>HTML/Javascript</td>
150 </tr>
151 <tr>
152   <td style="background-color: #9F0000; color: white"><b>Webserver</b><br>Apache, lighttpd, nginx, etc.</td>
153 </tr>
154 <tr>
155   <td style="background-color: #009F00; color: white"><b>Server App</b><br>Python, Perl, Ruby, etc.</td>
156 </tr>
157 <tr>
158   <td style="background-color: #404040; color: white"><b>Database</b><br>MySQL, PostgreSQL, MongoDB, CouchDB, etc.</td>
159 </tr>
160 </table>
161
162 <p>Modern web applications have at least a four-layer approach.  You
163 have the client-side browser app written in HTML and Javascript, the web
164 server, the server-side application typically written in some scripting
165 language (or, if it's high-performance, ASP/Java/C/C++), and the
166 database (usually SQL, but newer web apps seem to love object-oriented
167 DBs).
168
169 <table class="pizzapie">
170 <tr><th>Blërg model</th></tr>
171 <tr>
172   <td style="background-color: blue; color: white"><b>Blërg Client App</b><br>HTML/Javascript</td>
173 </tr>
174 <tr>
175   <td style="background-color: #404040; color: white"><b>Blërg Database</b><br></td>
176 </tr>
177 </table>
178
179 <p>Blërg compresses the last two or three layers into one application.
180 Blërg can be run as either a standalone web server, or as a CGI (FastCGI
181 support is planned, but I just don't care right now).  Less waste, more
182 throughput.  As a consequence of this, the entirety of the application
183 logic that the user sees is implemented in the client app in Javascript.
184 That's why all the URLs have #'s &mdash; the page is loaded once and
185 switched on the fly to show different views, further reducing load on
186 the server.  Even parsing hash tags and URLs are done in client JS.
187
188 <p>The API is simple and pragmatic.  It's not entirely RESTful, but is
189 rather designed to work well with web-based front-ends.  Client data is
190 always POSTed with the usual application/x-www-form-urlencoded encoding,
191 and server data is always returned in JSON format.
192
193 <p>The HTTP interface to the database idea has already been done by <a
194 href="http://couchdb.apache.org/">CouchDB</a>, though I didn't know that
195 until after I wrote Blërg. :)
196
197 <h3><a name="database">Database</a></h3>
198
199 <p>Early in the design process, I decided to blatantly copy <a
200 href="http://www.varnish-cache.org/">varnish</a> and rely heavily on
201 mmap for I/O.  Each user in Blërg has their own database, which consists
202 of one or more data and index files, and a metadata file.  When a
203 database is opened, only the metadata is actually read (currently a
204 single 64-bit integer keeping track of the last record id).  The data
205 and index files are memory mapped, which hopefully makes things more
206 efficient by letting the OS handle when to read from disk.  The index
207 files are preallocated because I believe it's more efficient than
208 writing to it 40 bytes at a time as records are added.  Here's some info
209 on the database's limitations:
210
211 <table class="statistics">
212 <tr><td>maximum record size</td><td>65535 bytes</td></tr>
213 <tr><td>maximum number of records per database</td><td>2<sup>64</sup> - 1 bytes</td></tr>
214 <tr><td>maximum number of tags per record</td><td>1024</td></tr>
215 <table>
216
217 <p>To provide support for
218 32-bit machines, and to not create grossly huge and unwieldy data files,
219 the database layer splits data and index files into many "segments"
220 containing at most 64K entries each.  Those of you doing some quick math
221 in your heads may note that this could cause a problem on 32-bit
222 machines &mdash; if a full segment contains entries of the maximum
223 length, you'll have to mmap 4GB (32-bit Linux gives each process only
224 3GB of virtual memory addressing).  Right now, 32-bit users should
225 change <code>RECORDS_PER_SEGMENT</code> in <code>config.h</code> to
226 something lower like 32768.  In the future, I might do something smart
227 like not mmaping the whole fracking file.
228
229 <p>A record is stored by first appending the data to the data file, then
230 writing an index entry containing the offset and length of the data, as
231 well as the timestamp, to the index file.  Since each index entry is
232 fixed length, we can find the index entry simply by multiplying the
233 record number we want by the size of the index entry.  Upshot:
234 constant-time random-access reads and constant-time writes.  As an added
235 bonus, because we're using append-only files, we get lockless reads.
236
237 <p>Tags are handled by a separate set of indices, one per tag.  Each
238 index record simply stores the user and record number.  Tags are
239 searched by opening the tag file, reading the last 50 entries or so, and
240 then reading all the records listed.  Voila, fast tag lookups.
241
242 <p>At this point, you're probably thinking, "Is that it?"  Yep, that's
243 it.  Blërg isn't revolutionary, it's just a system whose requirements
244 were pared down until the implementation could be made dead simple.
245
246 <p>Also, keeping with the style of modern object databases, I haven't
247 implemented any data safety (har har).  Blërg does not sync anything to
248 disk before returning success.  This should make Blërg extremely fast,
249 and totally unreliable in a crash.  But that's the way you want it,
250 right? :]
251
252 <h3><a name="problems_and_future_work">Problems and Future Work</a></h3>
253
254 <p>Blërg probably doesn't actually work like Twitter because I've never
255 actually had a Twitter account.
256
257 <p>I couldn't find a really good fast HTTP server library.
258 Libmicrohttpd is small, but it's focused on embedded applications, so it
259 often eschews speed for small memory footprint.  This is especially
260 apparent when you watch it chew through a POST request 300 bytes at a
261 time even though you've specified a buffer size of 256K.  Http_blerg is
262 still pretty fast this way (on my 2GHz Opteron 246, <a
263 href="http://www.joedog.org/index/siege-home">siege</a> says it serves a
264 690-byte /get request at about 945 transactions per second, average
265 response time 0.05 seconds, with 100 concurrent accesses), but a
266 high-efficiency HTTP server implementation could knock this out of the
267 park.
268
269 <p>Libmicrohttpd is also really difficult to work with.  If you look at
270 the code, http_blerg.c is about 70% longer than cgi_blerg.c simply
271 because of all the iterator hoops I had to jump through to process POST
272 requests.  And if you can believe it, I wrote http_blerg.c first. If
273 I'd done it the other way around, I probably would have given up on
274 libmicrohttpd. :-/
275
276 <p>The data structures written to disk are dependent on the size and
277 endianness of the primitive data types on your architecture and OS.
278 This means that the databases are not portable.  A dump/import tool is
279 probably the easiest way to handle this.
280
281 <p>I do want to make a FastCGI version eventually, and this will
282 probably be a rather simple modification of cgi_blerg.
283
284 <p>Implementing deletes will be... interesting.  There is room in the
285 record index for a 'deleted' flag, but the problem is deleting any tags
286 referenced in the data.  This requires rescanning the record content and
287 putting a 'deleted' flag in the tag indices.  This will not be pretty,
288 so I'm just going to ignore it and hope nobody makes any mistakes. ;]
289
290 <p>Tag indices can grow arbitrarily large, which will cause problems for
291 32-bit machines around the 3GB mark.  Still, that's something like 80
292 million tags, so maybe it's not something to worry about.
293
294 <p>The API currently requires the client to transmit the user's password
295 in the clear.  A digest-based authentication scheme would be better,
296 though for real security, the app should run over HTTPS.
297
298 </body>
299 </html>