<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>emphess .NET &#187; ASP.NET MVC</title>
	<atom:link href="http://www.emphess.net/tag/asp-net-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emphess.net</link>
	<description>Freshly Draught Code</description>
	<lastBuildDate>Fri, 11 Nov 2011 11:57:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>NoSQL Approaches: Trying to use db4o in the Real World</title>
		<link>http://www.emphess.net/2010/04/12/nosql-approaches-trying-to-use-db4o-in-the-real-world/</link>
		<comments>http://www.emphess.net/2010/04/12/nosql-approaches-trying-to-use-db4o-in-the-real-world/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 22:14:38 +0000</pubDate>
		<dc:creator>Christoph Menge</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[db4o]]></category>
		<category><![CDATA[Entrepreneurship]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[Startup]]></category>

		<guid isPermaLink="false">http://www.emphess.net/?p=138</guid>
		<description><![CDATA[We&#8217;ve been working a lot on db4o related and db4o based projects lately, and close to completion of the first and most simple product, we really hit a few roadblocks. UPDATE: Just after releasing this article, I found the bug in our code. It&#8217;s not db4o&#8217;s fault after all&#8230; Motivation One thing up front: We [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve been working a lot on <a href="http://www.db4o.com">db4o</a> related and db4o based projects lately, and close to completion of the first and most simple product, we really hit a few roadblocks.<br />
<strong>UPDATE: Just after releasing this article, I found the bug in our code. It&#8217;s not db4o&#8217;s fault after all&#8230;</strong></p>
<h2>Motivation</h2>
<p>One thing up front: We don&#8217;t need an <a href="http://en.wikipedia.org/wiki/Object_database">object database</a> for the simple tools we currently build, but we felt it was a good idea to get some acquaintance with the technology, because we will certainly need it for our (stealth) startup &#8220;pactas&#8221; soon. In pactas, the data structure is really complicated (with a fancy class hierarchy) and it probably will change very frequently.</p>
<p>Also, since I am such a big fan of reusability, we really develop a web engine &#8211; a framework that allows us to reuse a lot of the code for different projects and make sure most of our code has been tested thoroughly in the field. This proves to be a significant design decision when it comes to data modeling, and while I&#8217;m very happy with the decision, it certainly makes development harder.</p>
<h2>The Problem</h2>
<p>So, in time with the release of our simple <a href="http://www.backlink-tracker.net">backlink tracker</a>, one of our development database files started to show strange behaviour &#8211; a certain query (via LINQ) would not order objects anymore &#8211; the <code>orderby</code>-clause seemingly was completely ignored. Our live server even came up with an &#8220;Invalid DateTimeKind specified&#8221; exception when trying to perform the query! What&#8217;s worse: The problem kept occurring from time to time, but it was not reproducible! Byzantine errors are clearly my favourite&#8230; </p>
<p>We thought the issue might be related to the current development/unstable versions of db4o that we were using (7.12 and 7.13). Using the stable version of db4o (7.4) proved difficult, because the old LINQ provider falls back to LINQ to Objects very often, which requires to fetch all objects in question from the database &#8211; that is very, very slow for a lot of objects, so we had to abandon that. We clearly wanted to stick to LINQ for a number of reasons (compile-time checking, readability, reusability).</p>
<p>Obviously the problem is related to the <code>orderby</code> operation on <code>DateTime</code> fields. I tried to modify the query, removed grouping because I feared it might be unstable (<em>warning</em>: the <em>sort</em> operation is, in fact, unstable! Unstable grouping would be useless, but the grouping is stable so that&#8217;s fine), even debugged the db4o code, but I couldn&#8217;t find any problem in there. Since the code is rather complex and that was the first time I took a look at it, I was happy to find some of the relevant code at all. <font style="text-decoration: line-through;">Somewhere in the deeps of it, something screwed up.</font> I didn&#8217;t want to spend too much time on that since I had to chew some particle physics on the side. </p>
<h2>Solutions?</h2>
<p>At the time of writing this (in fact, yesterday) I had something like a hotfix, but it turned out it&#8217;s completely nonsense &#8211; it worked around our internal bug in a very peculiar way. No more, no less.</p>
<p>When talking about this, somebody mentioned that it wasn&#8217;t such a good idea to use <code>DateTime</code> at all, and we should store the ticks instead. That lead to two long discussions with my co-ed Christian. We concluded: First, the power of object databases is that they do not force you to hack around and find different representations for your data (which raises the bar for object databases). The one big shortcoming of SQL is that it forces you to find a second, equally good, but different representation of your data and you need to translate between these representations all the time. You need to synchronize them. And you lose a lot of fancy features (such as lists, generics, inheritance, etc.) on the way.</p>
<p>Secondly, Christian suggested that object databases suffer from <a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html">leaky abstractions</a> badly, in that they break encapsulation in a way that leaks out a hell of a lot of implementation details. As Joel puts it: &#8220;All non-trivial abstractions, to some degree, are leaky.&#8221; The point is: <font style="text-decoration: strikethrough">With the error we&#8217;re currently encountering, it&#8217;s becoming a <em>real problem</em>. This is not an <a href="http://www.codinghorror.com/blog/2009/06/all-abstractions-are-failed-abstractions.html">imperfect piece of architecture</a>, it&#8217;s an exception in a database query. It kills the app dead! Boom!</font></p>
<p>In order to get activation [depth] straight, db4o needs to know how .NET&#8217;s containers work internally &#8211; that is clearly a detail that should be hidden, but db4o knows about it. db4o also takes care of that, but it leads to some messy issues. There is special code in db4o that handles containers, non-trivial objects such as <code>strings</code>, <code>DateTime</code> (which are non-trivial because they use 62 bits for the actual ticks and 2 bits for the <a href="http://msdn.microsoft.com/en-us/library/shx7s921.aspx">DateTimeKind</a>) and Lists. This is an implementation detail of the .NET framework, and it might change over time. There&#8217;s <a href="http://tracker.db4o.com/browse/COR-1582">been a bug with <code>Map</code></a>, and I&#8217;m almost sure there is a bug with <code>DateTime</code>, too. Don&#8217;t get me wrong: The fact that <em>some kind of mapping</em> is needed is a somewhat generic (if not <em>the</em>) problem of serialization, it&#8217;s not really db4o-specific, and cannot be eliminated. In Hibernate, there is also a lot of code that handles the mapping of lists and the like but it doesn&#8217;t rely on implementation detail, thus it&#8217;s not (as) leaky.</p>
<h2>Back to SQL?</h2>
<p>Here&#8217;s the thing: I&#8217;d be beneficial to have a storage that is <em>independent of the actual implementation on top</em>, because it decouples the data store from the application which is <a href="http://developer.db4o.com/Forums/tabid/98/aff/4/aft/9847/afv/topic/Default.aspx">not what db4o does</a>. But wait, that is exactly what SQL is, right? Indeed: SQL forces you to map (or to cut down) your stuff to it&#8217;s internal features. A list becomes a foreign key on the other table, but that doesn&#8217;t play well with derived types, generics, etc&#8230; This is tricky as we all know, and <a href="http://www.ohloh.net/p/nhibernate/analyses/latest">you need lots of code to do that</a>. </p>
<p>Worse, SQL forces you do that mapping for everything, including your own classes, and it demands a schema for every type of object &#8211; this is not what I want. I&#8217;d like to see a set of base objects in an object database which are natively understood by the DB. These objects can be mapped from and to by a layer which may be part of the db, or can be added manually if you need something very special. Still, it should allow to store your object in the database with all it&#8217;s magic, only that known objects will be translated, e.g. a <code>DateTime</code> will be stored as <code>long Ticks</code> and a <code>DateTimeKind</code> flag separated, making comparison operations easier (note that the comparison only works on the ticks: Whether the time is local or UTC is not considered by .NET in comparisons). Lists will be unfolded into an internal tree representation if indexed, so they become much easier to cope with for the database. That would also make <a href="http://www.gamlor.info/wordpress/?p=1069">managing m:n relations</a> easier, since they could then be viewed as bilateral relationships &#8211; something you often need. Right now, they&#8217;re unilateral, thereby requiring some additional management on your behalf.</p>
<p>Migrating to SQL is clearly an option for this product, since data couldn&#8217;t fit SQL any better, but that doesn&#8217;t solve the issue for our pactas project, where we certainly will need schema-less data, complex object hierarchies, etc.</p>
<p>However, there are a few issues that <font style="text-decoration: line-through;">remain unresolved as of now and they do qualify as show stoppers</font> :</p>
<ul>
<li style="text-decoration: line-through;">There&#8217;s an irreproducible bug that potentially wrecks the db</li>
<li>There&#8217;s a <a href="http://www.emphess.net/2010/03/16/db4o-queries-on-large-datasets-and-a-bit-of-linq/">nasty performance issue with larger amounts of data</a></li>
</ul>
<h2>Alternatives</h2>
<p>I&#8217;m quite dissatisfied that we <font style="text-decoration: line-through;">have to abandon db4o at this stage</font>, because I believe it&#8217;s the best kind of serialization I&#8217;ve ever experienced. It&#8217;s <a href="http://blog.wekeroad.com/2010/02/06/nosql-a-practical-approach-part-1">perfectly simple</a>, it&#8217;s fast and most importantly, code-centric. If reusability is a concern, having the database structure and/or the ORM mapper dictate the objects is a major pain.</p>
<p><a href="http://en.wikipedia.org/wiki/ADO.NET_Entity_Framework">Entity Framework 4 (EF4)</a> promises to handle this a lot better through <a href="http://blogs.msdn.com/adonet/archive/2009/05/12/sneak-preview-model-first-in-the-entity-framework-4-0.aspx">&#8220;Model First&#8221;</a> and <a href="http://blogs.msdn.com/efdesign/archive/2009/06/10/code-only.aspx">&#8220;Code Only&#8221;</a>, but I am still a bit afraid of EF because it doesn&#8217;t appear to be anything near lightweight and we will need schemaless storage for our future products anyways.</p>
<p><a href="http://www.versant.com">Versant</a>, which bought db4o some time ago also offers its large-scale object database, which seems to suit large web-applications a lot better. First, it&#8217;s certainly made for huge amounts of data (unlike db4o, which comes from an embedded background and <a href="http://www.sdtimes.com/link/33117">is aimed at database sizes in the low GB area</a>, but <a href="https://developer.db4o.com/Documentation/Reference/db4o-7.4/java/reference/html/reference/tuning/performance_hints/increasing_the_maximum_database_file_size.html">supports up to 254 GB per file</a>) and <a href="http://developer.db4o.com/Forums/tabid/98/aff/4/aft/9855/afv/topic/Default.aspx">handles multi-threading better</a>. Also, since db4o <a href="http://www.itwire.com/sponsored-announcements/38149-versant-expands-db4o-open-source-licensin">now moved to v3 of the GPL</a>, it may not be freely usable in non-open source web applications anymore, so both solutions now have a price tag.</p>
<p>There is also an <a href="http://www.versant.com/en_US/solutions/oem_program/">ISV/OEM empowerment program</a> for Versant&#8217;s Object Database, which seems to make it affordable, but I haven&#8217;t looked at it in detail yet. Over the next weeks, I will have to evaluate a few of those other NoSQL solutions such as <a href="http://cassandra.apache.org/">Cassandra</a> and <a href="www.mongodb.org/">MongoDB</a>, just to name two totally different options. </p>
<h2>Aftermath</h2>
<p>So what was the issue, after all? db4o did not screw up, we did: Take a blend of local and UTC time based on a completely random criterion, add two spoons of daylight savings time changes, add some misconfigured timezone on the server, bake for two weeks at 500 &deg; C and pling! You got yourself some really strange issue cake. Lessons learned:</p>
<ul>
<li>Debugging issue with object databases is harder than with RDBMS because the information is not chopped up.</li>
<li>Do code reviews. Do code reviews.</li>
<li>There aren&#8217;t too many alternatives to db4o, after all</li>
<li>With the elaborated architecture and code-centric design we currently have, going back to SQL is a huge pain, and we won&#8217;t do it</li>
</ul>
<p>All serialization is painful.</p>
<div class="tweetthis" style="text-align:right;"><p> <a rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=NoSQL+Approaches%3A+Trying+to+use+db4o+in+the+Real+World+http%3A%2F%2Femphess.net%2F%3Fp%3D138" title="Post to Twitter"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter2.png" alt="Post to Twitter" /></a> <a rel="nofollow" class="tt" href="http://delicious.com/post?url=http://www.emphess.net/2010/04/12/nosql-approaches-trying-to-use-db4o-in-the-real-world/&amp;title=NoSQL+Approaches%3A+Trying+to+use+db4o+in+the+Real+World" title="Post to Delicious"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/en/delicious/tt-delicious.png" alt="Post to Delicious" /></a> <a rel="nofollow" class="tt" href="http://digg.com/submit?url=http://www.emphess.net/2010/04/12/nosql-approaches-trying-to-use-db4o-in-the-real-world/&amp;title=NoSQL+Approaches%3A+Trying+to+use+db4o+in+the+Real+World" title="Post to Digg"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/en/digg/tt-digg.png" alt="Post to Digg" /></a> <a rel="nofollow" class="tt" href="http://www.facebook.com/share.php?u=http://www.emphess.net/2010/04/12/nosql-approaches-trying-to-use-db4o-in-the-real-world/&amp;t=NoSQL+Approaches%3A+Trying+to+use+db4o+in+the+Real+World" title="Post to Facebook"><img class="nothumb" src="http://www.emphess.net/wp-content/plugins/tweet-this/icons/en/facebook/tt-facebook.png" alt="Post to Facebook" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.emphess.net/2010/04/12/nosql-approaches-trying-to-use-db4o-in-the-real-world/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

