One Does Not Simply Scale into Real-Time
Join the DZone community and get the full member experience.Join For Free
one does not simply scale into real-time
real-time seems to be the next big thing in big data . map-reduced has shown how to perform big analyses on huge data sets in parallel, and the next challenge seems to be to find a similar kind of approach to real-time.
when you look around the web, there are two major approaches out there which try to building something which can scale to deal with twitter-firehose-scale amounts of data. one is starting with a mapreduce framework like hadoop and somehow finagle real-time or at least streaming capabilities on it. the other approach starts with some event-driven “streaming” computing architecture and makes it scale on cluster.
these are interesting and very cool projects, however from our own experience with retweet analysis at twimpact , i get the feeling that both approaches fall short of providing a definitive answer.
in short: one does not simply scale into real-time .
real-time stream analysis
so what is real-time stream analysis? (apart from the fact that i seem to be unable to decide whether to write it with a hyphen or as one word like donaudampfschifffahrtskapitänsmütze ).
basically, the idea is that you have some sort of event stream like twitter messages, or apache log data, or url expansion requests at bit.ly, which comes in at a high volume of several hundreds or even thousands of events per second. let’s just focus on twitter stream analysis for now.
typical applications are to compute some statistics from the stream which summarizes it in a nice fashion. for example
- what is the most frequently retweeted tweet?
- what is the most frequently mentioned url?
- what are the most influental users (in terms of mentions/retweets)
these are pretty basic counting tasks. very closely linked are questions like what the score of an arbitrary tweet or url is, or what the first few hundred most influental users are, and so on. you can also compute more complex scores based on more of these numbers. for example, our own twimpact score is based on all the counts of a user’s retweets, and something like the klout score is also based on a number of statistics (i’m only assuming here, of course).
since the data arrives in real-time, you typically also want to get the results in real-time (instead of a daily report which is already hours old when you get it). ideally, you get updated scores with each event, also i’d say everything is ok if a query always takes less than a second.
finally, you’d probably also want to be able to look at historical data to see how a user or retweet has performed in the past and to see whether its activity is going up or going down.
just to give you an idea of the amount of data involved: each tweet corresponds to about 1k of data (including all metadata). if we assume that we have about 1000 tweets per second (actually it’s probably more), then we get about 86.4 million tweets per day, or about 82.4gb of new data per day (about 30tb per year).
now let’s discuss how you would approach this problem in a database centric fashion and using a stream processing framework.
with “database approach” i try to cover a whole range including traditional relational databases, nosql databases and mapreduce frameworks. the common denominator is that you basically pipe your data into the database and use the built-in queries of the database to compute your statistics. depending on the type of nosql database you are using you’ll probably have to do the analysis online to precompute all your statistics because you the database doesn’t have sufficient query capabilities.
as i see it, there are two main problems with this approach: first of all, the size of your database grows at a non-trivial rate. unless you’re google and you’ve planned for exponential growths of your data centers anyway, you will eventually run out of space and performance. even if you assume that the reponse time will increase only logarithmically in your data size, your cluster will eventually become quite slow to deal with real-time data.
this will directly affect the time it will take for your queries to complete. however, as queries also put quite some load on your disks, this problem will only get worse over time.
at the same time, most of the data will likely be irrelevant for your current analysis. tweets stop being retweeted, urls fall out of fashion. in other word, there is a huge amount of historical baggage clogging up your servers. it is true that you still need this data in order to compute historical statistics, but note that the data doesn’t change anymore, so that it would make much more sense to analyse the data once and only put the results in some read-only storage.
you could of course try to keep the database size constant by periodically discarding old data. i think this is the right approach and i’ll discuss it in more detail below. note, however that many databases cannot really deal well with huge amount of deletions, as they need some form of more or less disruptive garbage collection (vacuum, compaction, etc.) to actually free up the space.
finally, one should also not forget that mapreduce doesn’t work with all kinds of problems, but only with problems which are already inherently easy to parallelize. actually, if you look into research in parallel algorithms, you will see that almost no problems scale linearly in the number of available processors (and you will also see that many of the efficient algorithms work with shared mutable state! ugh!)
so if storing all your data on slow disks to process it later is the wrong approach, you should probably try to process the data as it comes in through some form of pipeline. this is more or less the idea behind stream processing. two example frameworks are storm , originally developed by backtype (recently acquired by twitter), and s4 developed by yahoo.
if you look closer, these are basically quite sophisticated frameworks to scale an actor based approach to concurrency. if you’re not familiar with it, it’s the idea to structure some computation in terms of independent small pieces of code which do not share state and communicate with one another through messages.
frameworks like the ones above let you define a computation in terms of a number of processing nodes which may also run on different servers, with the ability to add more parallel workers of a certain kind on the fly to scale up processing resources where necessary.
this approach is essentially orthogonal to the database approach. in fact, stream processing frameworks usually don’t even deal with persistence, they only focus on the computation. therefore, there is also nothing specific to real-time stream processing in these frameworks. in essence, they deal with the question of how to split up a computation into small independent parts and how to scale such a network on a cluster.
to me, the basic problem with this approach (and with actor based concurrency) is that it doesn’t deal well with peak volumes which surpass the computation bandwith. in fact, it’s even not so simple to say given such a network what the maxium throughput is. conceptually, it is the throughput of the slowest component, but you also have to take the message routing topology into account.
now, once more messages need to be processed than possible, somewhere in the system messages queues are starting to fill up. this is a general problem with such systems, not specific to actor based concurrency. the book release it! contains some very entertaining and also frightening real-world war stories of what can go wrong with systems where you plug together components with quite different capacities.
another problem is actor based concurrency tends to break a simple function which may fit on a single screen into dozens of classes, but that is another problem.
in any case, the question is how you can guarantee that your system will run stably even for high peak volumes, apart from just adding more nodes? the easiest thing would be to randomly drop events (resulting in a more or less consistent subsample which at least has the same distribution as the original data stream), but is there something different you could do?
so to summarize: putting all your data into a database is problematic because the data steadily grows and computing statistics based on the data is too slow. you also don’t really need to keep all your data at hand to have an analysis of the current state of the stream.
stream processing, on the other hand, is a nice tool to scale your computations, but it doesn’t deal well with peak volumes, and depending on how you persist your data, you run into the same scaling issues as the database centric approach.
in other words, what we need is a way to separate the data we currently need for our analysis from the historic data and analyses, and we also need a way to limit the bandwidth of events while having as little distortion as possible on the statistics we’re interested in.
as it turns out, these kinds of problems are closely related to “frequent itemset” problems in data mining. the idea is to identify the top most frequent items in a data stream with limited resources, both in terms of memory and computation. such methods are discussed, for example, in chapter 4 of rajaraman and ullman’s upcoming book mining of massive datasets .
most of these methods work by keeping a fixed amount of counts and then replacing the least frequent ones when a new type of event occurs. they come with some sort of theoretical guarantee to identify the correct set of items, or at least some bound on the differences in the count. however, when analysing retweets you get quite good results because only a fraction of the retweets is retweeted more than once, so that most of the slots are occupied by retweets which do not reoccur and discarding them doesn’t hurt.
as you might have expected, this is exactly the approach we took with twimpact. all the demos you can see at our site are powered by our new trending backend (codename “trevor”), which is basically an in-memory trend database built using ideas from stream mining, together with read-only snapshots on disk for later historical analyses.
these ideas can of course also be combined with databases and stream processing, but already without a huge amount of parallelism, we’re able to process a few thousand tweets per second.
in summary: it’s not enough to just scale your database needs and your computational algorithms, to make your analysis framework stable against peak volumes and sustainable in terms of data growth, you also need to separate your analysis database from the historic data and add some ideas from stream mining to extract a statistically approximate subsample with capped bandwidth from your data.
Published at DZone with permission of Mikio Braun, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.