4 ms·
I think this analysis is not quite as cut-and-dried as the author thinks it is. 1. Latency. Yes, going out-of-process, even to localhost, is very expensive, an
by hythloday 15y ago
I think this analysis is not quite as cut-and-dried as the author thinks it is.
1. Latency. Yes, going out-of-process, even to localhost, is very expensive, and the person the author was responding to should realize that. On the other hand, synchronization is also very expensive. How do they compare? I have no idea[0], and I'm not about to guess. The author shouldn't either.
2. Concurrency. Paul Tyma specifically talks about a "synchronized LinkedHashMap" as the implementation of a cache, so I'm going to take him at his word, understanding it might be a simplification. A synchronized Map is a poor implementation for a cache, because reads will block writes when they don't need to. A better implementation would be a ReentrantReadWriteLock protecting an unsynchronized LinkedHashMap. Redis gives you this behaviour for free (even if you don't know of the existence of ReadWriteLock).
3. Memory usage. Let's be honest--Java is a pig for memory[2] compared to C++, and this is nowhere more apparent than indexing and caching the guaranteed 8-bit strings you'd find in an email. If your whole purpose is to fit more lines into your cache it's genuinely worth considering breaking out of the JVM to exploit the smaller memory footprint of C++ strings (and this really only holds for caches).
Was using a LinkedHashMap a good idea for Mailinator? Probably, I definitely don't have any evidence or suspicion to the contrary. Is it sensible to say "COGS BAD! IN-PROCESS GOOD" for every use-case? Not really.
[0] If I had to guess I would imagine that going out-of-process is 1-2 orders of magnitude slower than contended synchronization. Anyone got any figures?
[1] http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html http://docs.oracle.com/javase/6/docs/api/java/util/concurren...
[2] I would be very surprised if Redis was not more than twice as memory-efficient for this task as a LinkedHashMap. I'm pretty sure that one could implement a C++ solution that would be 3-4x as efficient. This is really only of paramount concern in a caching context, but in that context, it's paramount, because the cost of missing the cache is so phenomenal.
- eridius 15y agoIsn't a read-write lock only really useful if you have more readers than writers? I would not be surprised if the amount of incoming spam means mailinator has far more writing to do than reading.
- dblohm7 15y agoRead-write locks also have involve more overhead, so the work that must be done while holding the lock had best be significantly more costly than what's required to take the lock itself.
- hythloday 15y agoMaybe! My point isn't that Mailinator made the wrong choice (in fact I said they probably made the right one)--it's that the decision for a situation we know nothing about is more complex than "Cogs bad".
- groby_b 15y agoI'm not aware that simply going out-of-proc solves any synchronization issues :) You still contend with those. It might be by simply letting the OS serializing the requests, but going oop is always overhead you pay in addition to synchronization.
- hythloday 15y agoYou're quite right, but paying the costs of synchronization at a hardware/C level rather than at a Java one defrays the cost of going out of process.
- groby_b 15y agoJava synchronization is _that_ bad? (Not trolling - I just spend most of my days in C/C++). Any good performance studies you could point me to?
- willvarfar 15y agoPaul is the author of these slides: http://mailinator.blogspot.com/2011/05/notes-from-programming-leap-to.html http://mailinator.blogspot.com/2011/05/notes-from-programmin... (Its in the blog I told you to go read) So, think he understands how to do it right?
- eneveu 15y agoIf I were to replace the "synchronized LinkedHashMap", I'd look at Google Guava's in-memory cache: http://code.google.com/p/guava-libraries/wiki/CachesExplained http://code.google.com/p/guava-libraries/wiki/CachesExplaine... Here is a talk about it: http://www.infoq.com/presentations/Concurrent-Caching-at-Google http://www.infoq.com/presentations/Concurrent-Caching-at-Goo...