<?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>Mark Needham &#187; Messaging</title>
	<atom:link href="http://www.markhneedham.com/blog/category/messaging/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Mon, 13 Feb 2012 21:25:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C# Thrift Examples</title>
		<link>http://www.markhneedham.com/blog/2008/08/29/c-thrift-examples/</link>
		<comments>http://www.markhneedham.com/blog/2008/08/29/c-thrift-examples/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 00:39:52 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[examples]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[thrift]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=252</guid>
		<description><![CDATA[As I mentioned in my earlier post I have been working with Facebook&#8217;s Thrift messaging project. Unfortunately there are not currently any C# examples of how to use the Data Transfer Objects the Thrift compiler generates for us on the official wiki. We managed to figure out how to do it by following the Java [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned in my <a href="http://www.markhneedham.com/blog/2008/08/29/thrift-as-a-message-definition-layer/">earlier post</a> I have been working with Facebook&#8217;s <a href="http://incubator.apache.org/thrift/">Thrift</a> messaging project.</p>
<p>Unfortunately there are not currently any C# examples of how to use the Data Transfer Objects the Thrift compiler generates for us on the <a href="http://wiki.apache.org/thrift/ThriftUsage">official wiki</a>.</p>
<p>We managed to figure out how to do it by following the <a href="http://wiki.apache.org/thrift/ThriftUsageJava">Java instructions</a> and converting them into C# code. Before writing any code we need to import Thrift.dll into our Visual Studio project. </p>
<p>Assuming that we have the following Thrift definition file:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">namespace csharp Test.Thrift
&nbsp;
struct FooBarMessageThrift {
1: string Foo
2: string Bar
}</pre></div></div>

<p>When we run the <a href="http://wiki.apache.org/thrift/ThriftInstallationWin32">Thrift compiler</a> we will end up with the FooBarMessageThrift class. I won&#8217;t post this class here as it&#8217;s all codegen.</p>
<p>The easiest way to transport this class around is by converting it to a byte array and transporting that:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">var fooBarMessage <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FooBarMessageThrift <span style="color: #008000;">&#123;</span>Foo <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;foo&quot;</span>, Bar <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;bar&quot;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
var stream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
TProtocol tProtocol <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TBinaryProtocol<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> TStreamTransport<span style="color: #008000;">&#40;</span>stream, stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
fooBarMessage<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>tProtocol<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> content <span style="color: #008000;">=</span> stream<span style="color: #008000;">.</span><span style="color: #0000FF;">ToArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>To read the byte array back into FooBarMessageThrift we do this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">var stream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #008000;">&#40;</span>content<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TProtocol tProtocol <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TBinaryProtocol<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> TStreamTransport<span style="color: #008000;">&#40;</span>stream, stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var barFooMessageThrift <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BarFooMessageThrift<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
barFooMessageThrift<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span><span style="color: #008000;">&#40;</span>tProtocol<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>&#8216;content&#8217; in this example is the byte[] created in the first example, and that&#8217;s all there is to it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/08/29/c-thrift-examples/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Thrift as a message definition layer</title>
		<link>http://www.markhneedham.com/blog/2008/08/29/thrift-as-a-message-definition-layer/</link>
		<comments>http://www.markhneedham.com/blog/2008/08/29/thrift-as-a-message-definition-layer/#comments</comments>
		<pubDate>Thu, 28 Aug 2008 23:42:51 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Messaging]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[protocol-buffers]]></category>
		<category><![CDATA[thrift]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=249</guid>
		<description><![CDATA[Thrift is a Facebook released open source project for cross language serialisation and RPC communication. We made use of it for our message definition layer &#8211; when it comes to messaging I&#8217;m a fan of the event based approach so we left the RPC stuff well alone. Why Thrift? The reason we used Thrift in [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://incubator.apache.org/thrift/">Thrift</a> is a Facebook released open source project for cross language serialisation and RPC communication.</p>
<p>We made use of it for our message definition layer &#8211; when it comes to messaging I&#8217;m a fan of the event based approach so we left the <a href="http://www.25hoursaday.com/weblog/2008/07/10/TheRevengeOfRPCGoogleProtocolBuffersAndFacebookThrift.aspx">RPC stuff</a> well alone.</p>
<h3>Why Thrift?</h3>
<p>The reason we used Thrift in the first place was because we had a requirement to get interoperability between a Java and .NET application across a message bus and it provided an easy way to do this.</p>
<p>Google&#8217;s <a href="http://code.google.com/p/protobuf/">protocol buffers</a> offers an alternative solution in this area. I don&#8217;t think there is much difference between the two approaches &#8211; we just happened to come across Thrift first and it seemed to solve our problems so we went with it.</p>
<h3>How it works</h3>
<p>The idea behind it is that you can create Thrift definition files with the elements that you want to include in your message. You then <a href="http://wiki.apache.org/thrift/ThriftInstallationWin32">run a compiler</a> which generates code for Data Transfer Objects (DTO) in Java, C#, Ruby and various other languages.</p>
<h3>Issues we came across</h3>
<p>There were a few issues that we came across when using Thrift. They are not really Thrift specific but would always be the case when taking this approach to message definition:</p>
<h4>Data Mapping</h4>
<p>We wanted to have rich message objects in our code. This meant that we had to write a mapping layer which translated the Thrift DTO messages into our richer objects. This eventually ended up taking up quite a bit of our time &#8211; it once took 90 minutes to create the Thrift message, compile the Java/C# DTOs and write the mappers.</p>
<h4>Versioning</h4>
<p>The perennial problem of versioning was still something of an issue although several areas had been addressed.</p>
<p>Each property on a message had a corresponding boolean flag which determined whether or not that property has been set (i.e. whether it had a value). This meant that in theory it was possible to remove a property from the message definition cleanly, although it was still up to the client to check these flags each time they handled a message. </p>
<p>Using this mechanism would place the responsibility on the client to deal with potential problems &#8211; if the flags were ignored then things would break.</p>
<p>Re-ordering of properties was also interesting. In theory as long as the types of the properties are the same then it is possible. For example, if you had:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">struct FooBarMessageThrift {
1: string Foo
2: string Bar
}</pre></div></div>

<p>And you changed it to:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">struct FooBarMessageThrift {
1: string Bar
2: string Foo
}</pre></div></div>

<p>It would still work although you would end up with one client writing in to Foo and the other picking up the value of Bar for Foo. The reading and writing of the messages across the wire completely depends on the order in which they are specified in the thrift file.</p>
<p>If we try and reorder elements with different types even the above is not possible and we end up with corrupt data:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">struct FooBarMessageThrift {
1: i32 Foo
2: string Bar
}</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">struct FooBarMessageThrift {
1: string Bar
2: i32 Foo
}</pre></div></div>

<p>As you might imagine this can quickly get very confusing so we decided that the way forward was to not re-order and rather than remove a property, simply not assign a value to it if it is no longer in use.</p>
<p>After analysing the potential problems in these areas, a colleague came up with an interesting idea that when versioning messages we should treat it like an API &#8211; add but never remove.</p>
<p>There are certainly uses for Thrift and Google Protocol Buffers in the world of messaging but as with everything there are trade offs that we need to be aware of.</p>
<p>Thanks to <a href="http://markthomas.info/blog/">Mark Thomas</a> for working with me to write this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/08/29/thrift-as-a-message-definition-layer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

