Apache Spark: 5 Pitfalls You MUST Solve Before Changing Your Architecture
The top 5 things you need to know before moving to Apache Spark.
Join the DZone community and get the full member experience.Join For Free
it seems like everyone’s only talking about the new hottest tech and neglect what it actually means to adopt it. but it’s only natural, right? the new features and promises outshine everything else and the tough challenges and decisions are swept aside.
java / scala developer? takipi replaces logging in production jvms and lets you see the variable state that caused each log error and exception.
not this time. software architecture is hard, and trade offs are the name of the game.
— takipi (@takipid) august 17, 2015
in this post, we wanted to take a step back and see what it actually means to execute the decision of moving to spark from the ground up. a huge thanks goes out to tzach zohar , developer and system architect at kenshoo , who shared his experience with us for this post.
why bother moving at all?
if you’re starting with a whole new project that would benefit from distributed data analysis, be it batch analysis or streamlined analysis, spark has already pretty much established its supremacy as the best implementation of mapreduce. mostly because of the way it uses in-memory processing. otherwise, if you’re getting the throughput you need with a single server, and the data you’re using isn’t expected to outgrow it, you’re probably better off avoiding the added complexity of going distributed. notice how we didn’t say big data even once. oh . in addition, spark has an awesome and easy to use machine learning library.
spark vs. hadoop
it’s more likely though that your starting point is an existing solution you already have, and this is where things can get extra hairy. we’ll put the focus of the post on that. migrating from hadoop or a home grown solution on top of databases that are struggling with scale. the performance boost can eventually cut down your hardware cost, increase productivity, or just really be the only way to get away with what is it that you’re trying to do.
the biggest benefit comes from the batch analysis angle, so if that’s your use case – upgrading your cluster can be even more urgent. in kenshoo’s case, a single-server mysql solution was once more than enough. but as the company grew and years have passed, this was no longer enough – tens and millions of records going in every day, hundreds of tables, over a billion of records on the bigger ones, and terabytes of data. it’s not kansas anymore . there comes a point when all the optimizations you throw at it and even high-performance storage engines like tokudb just won’t do. what you do end up with is a mutant mysql on steroids.
on the other side of the shore there’s spark, solving all sorts of issues, new, but implementing longstanding principles, and gaining fast adoption and lots of positive signals from the community.
1. hdfs vs. cassandra vs. s3
your choice of a storage server for apache spark should reflect what you value most for your system. the 3 common options here are hadoop’s hdfs, apache cassandra, and amazon’s s3. s3 fits very specific use cases, when data locality isn’t critical. like jobs that run once per day for example, or anything really that doesn’t require the data and processing power to share a machine. jobs with no urgency. as for the hdfs vs. cassandra issue, hardware costs for running hdfs are lower, since it was designed to solve simpler use cases. how low? as far as 10x. the main difference comes from hdfs solving the issue of running of a distributed file system, while cassandra was specifically designed to be a high-throughput key-value store.
although the higher costs, cassandra has the upper hand when it comes to interactive, streaming data analysis – opposed to running batch jobs. you could say that hdfs loves big files, while cassandra doesn’t have to load all of the data, use only what it needs and reach
– non urgent batch jobs.
cassandra – perfect for streaming data analysis and an overkill for batch jobs.
hdfs – great fit for batch jobs without compromising on data locality.
2. greenfield vs. refactoring
alright, so you’ve decided to move to spark, now, should you start off fresh with a greenfield project or refactor based on your current application? each has their own caveats, and kenshoo decided to let go of the greenfield path in favor of refactoring their current system. this decision narrows down to 4 factors:
1. avoiding a moving target scenario – building a new system from scratch takes time, months of development. and during that time the legacy system is also changing, so your spec is literally a moving target that changes over time.
2. zero diff tolerance – the new system should reach the same results as the legacy one, right? what sounds like a straightforward process, is a problem in disguise. with years in development, all kinds of quirks and customizations for specific analysis processes have been hardcoded into the older application. certain assumptions, rounding results, and requests from individual clients for instance – have created a complex analysis process that’s hard to recreate from scratch.
3. code is the only spec – documentation is most likely… nonexistent. and if it does exist, it most likely that doesn’t reflect the current state of the system. here’s one example that you can probably relate to, those dark corners in the code:
stuff that “shouldn’t” happen, but does it happen?
4. test reuse – your current tests are coupled with the older implementation, and assume a different setup. another task here is to rewrite them to match the new implementation.
bottom line: in this case, refactoring, rather than starting up completely fresh – made the most sense.
3. refactoring challenges
choosing the refactoring path also has its challenges, untested legacy code, tight coupling with other system components, and the paradigm shift for a new architecture. switching from a similar hadoop architecture would be easier than getting on the distributed system path after being on a single node application. there are new skills to learn, processes to adjust, and there’s lots of friction. greenfield or not, it’s a hard task, but if you’ve determined that it’s worth it – there’s a light at the end of this tunnel.
in kenshoo’s case, their mission was to free a bottleneck aggregator component from a huge 8 year old system. the aggregator performs occasional batch processing on the data and groups it by different keys.
bottom line: know your weak spots in advance before moving, and make sure you have solution approaches for the critical paths in your new implementation.
4. solution approaches
4.1. core business rules first
one of the main benefits of refactoring is of course code reuse. the first step for building the new system was to go for the core business rules first and create a stand-alone jar from them. the methods were refactored to java static methods to avoid serialization issues in spark.
4.2. dropwizard metrics and untangling legacy code
moving on up, remember that “shouldn’t happen” example? kenshoo rigged it up with a dropwizard metrics counter:
and what do you know. it does happen quite a lot:
occurrences of….. “this shouldn’t happen”
bottom line: using metrics to measure the unknowns in the legacy code proved to be a powerful tool, which allowed turning “hidden” features into explicit, well-documented, and well tested features.
4.3. local mode testing
to take a stab at the testing challenges, kenshoo made use and took inspiration from spark’s local mode – creating an embedded like instance of spark inside the new aggregation component. moreover, they then took this new component, and embedded it in the legacy system, reusing the older tests and make sure the new system meet all the requirements:
4.4. graphite the “diffrecorder”
the final frontier, beyond the local mode testing, is to test on real data in production and see if the spark results match those of the legacy system. for this purpose, a “diffrecorder” hooked up with graphite visualizations was implemented. the diff recorder represents every real input for which the two versions differed as a graphite metric, pin-pointing the exact inputs for which the new implementation isn’t consistent.
and the resulting data helped understand what needs to be further tweaked to match the older system (or… uncovering hidden faults in the system). btw, to learn more about graphite you can check out this post about choosing the best graphite architecture for your system .
kenshoo’s graphite dashboard
5. spark monitoring
spark has a great integration with graphite where you can plot any kind of graph that you have in mind. beyond that, the second go to tool here would the spark web ui for viewing your jobs and performance metrics. any serious deployment of spark requires putting in lots of thought into performance and monitoring. this can become a really thorny issue and you need to be familiar with internals to tune the system. writing code for spark is easy, but performance adds another layer of complexity. in that sense, it’s easy to go wrong here and produce bad code.
recommended resources for getting started with spark
storage, refactoring techniques, monitoring, test reuse, and consistent results – we hope you’ve found the provided solutions useful and know how to apply them when needed. transitions to new technologies are tough. in addition to the learning curve, they make you more vulnerable to errors (and also make you more likely to receive calls in the middle of the night to fix some critical production issue). for these kind of situations, we launched takipi’s error analysis for spark .
we’d like to say thanks again to tzach zohar from kenshoo for sharing his experience with us for this post!
java / scala developer?
replaces logging in production jvms and lets you see the variable state that caused each log error and exception.
Published at DZone with permission of Alex Zhitnitsky, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.