If your application grows and you need to support more customers, there is the common wisdom to buy more hardware. Or solve performance problems with more hardware. But the simple math says there is a point where investing in software or OS optimization is much cheaper than buying more hardware. There are essentially three ways to optimize and scale your web application – assuming it can scale.
- Buy more hardware
- Optimize hardware – buy better optimized hardware
- Optimize your application – or your software stack
Suppose we have 10 servers and want to add 100% customers. Then we need to buy 10 more servers. Suppose we have 1000 servers and want to add 100% customers. Then, for the same growth, we need to add another 1000 servers. As this gets expensive very fast (exponentially)- not in the beginning of your startup but after some time – you need to look harder and harder into software optimizations. This is also the reason why Facebook benchmarks all it’s new hardware to squeeze out every last bit of performance. If you have 10 servers and you squeze out 1% of performance, this is equivalent to 0.1 servers. If you have 10000 servers and you squeze out 1% more performance that’s equivalent to 100 servers.
See the example for costs for 10% customer growth:
There are other factors to think of. Most often when talking about scaling, people talk about how their application scales linear. This correlates to your application having O(n) scaling. And in theory this is fine. In reality it’s probably f*O(n). And different architectures, although scaling with O(n), have different linear factors. It does matter if f is 2 or f is 20. What f does your web application have?
Actors are the new concurrency. They are everywhere. People make bold claims about actors, and while I do not agree with many of them, two in particular I regard as myths. Here they are:
- Actors are a shared nothing architecture
- Actors are easier to get right because of their shared nothing architecture
I know I’m alone with calling those myths, but here we go. Considering the first myth. If they share nothing, they could not collaborate on data. Sharing immutable messages does not help for getting data synced up again. So this can’t be the way to collaborate. Indeed actors do share everything! Each actor can be considered a data structure with state and a write lock (synchronized in Java).
The second myth. Actors are easier to get right because they share nothing and therefor do not have locks. As argued above they have locks, so this can’t be the reason why they are easier to get right. I consider two characteristics of actors to be the reason for them being easier:
- They only can hold one lock at a time (when using synchronized send messages). This is a common approach to prevent deadlocks in locking architectures.
- They mostly send asynchronous messages. This prevents the common problems that arise from holding locks and blocking. In locking architectures when accessing locks in an asynchronous way (e.g. with futures and timeouts) there are also no deadlocks for the calling thread.
I would like to learn something from this. So please keep your flaming minimal and help me understand possibles errors in my thinking instead.
Update: If you do not believe me concerning the shared-nothing part, read the example from James
Tail recursion seems to be an easy concept, but most people get it wrong – including me. Reading the latest German Java SPEKTRUM, I’ve found an article about parallel multicore development by Kornelius Fuhrer. One paragraph was about functional development and tail recursion. First he claims tail recursion makes functions 100% parallizeable (I guess broadly speaking all compositions h(g,f) of side effect free functions g,f are 100% parallizeable in f and g, nothing to do with tail recursion) then he claims his example functions are tail recursive:
Wikipedia says about tail recursion:
In computer science, tail recursion (or tail-end recursion) is a special case of recursion in which any last operation performed by the function is a recursive call, the tail call, or returns a (usually simple) value without recursion.
In all three examples the last operation performed is the multiplication (*), not the function call to itself. First the function itself is called, then the return value is multiplied by n. Stackoverflow has a lot to say about tail recursion too.
A tail recursive version of factorial might look like this:
So please, do not call all functions where the last function call in your source code is a call to itself, tail recursive. A function is tail recursive, if the last operation is a function call to itself.