5

I've been working on a system that transmits xml over sockets for a while now. And i never really understood whats the real advantage of the choice of xml over sockets instead of a custom protocol.

But i do see a lot of developers(specially originally web-developers) setting up this sort of implementation(xml over sockets).

I do understand that is more "human-readable" (that's what i keep hearing).

But,

  • Xml carries an awful amount of characters, leading to huge messages when in fact the content is really small and simple.

  • Message size varies, therefore you need to guarantee that you terminate your message with specific character or string pattern.

  • There is more overhead when parsing xml

For all this reasons i remain skeptic about considering XML over Sockets for new systems when i could set my systems using a custom protocol using fixed-size messages. Avoiding huge messages being transmitted and performance hits parsing xml on the client size.

Am i wrong to think as such ? What's "best" in terms of system-architecture ?

Regards

1
  • 1
    As a very successful version of this I'd like to mention XMPP. In fact if you think about doing XML-over-sockets, I strongly suggest you consider using XMPP directly (or at least look at its design for inspiration). May 21, 2012 at 9:02

7 Answers 7

6

The main reason that XML has traditionally been used when transmitting data across a network is because it's extensible. It scales your application and turns it into a potential platform for other applications to build upon yours, or it enables other applications to more easily integrate with you, without having to solve a problem that has already been solved.

After all, that's what developers are paid to do. Solve new problems. Not create them.

Ryan Tomayko talked about this in his blog article on How I Explained REST to My Wife, when he highlighted that using a standard meant that individual systems all over the world could be networked to form massive, cohesive, yet individual, systems.

When you use your own, custom protocol, you basically limit your application to only communicating in it's own little world. Then, when the time comes to glue it together with another system, a lot more integration work is required on both ends.

With that said, JSON is starting to gain lots of traction as a replacement to XML. It's more lightweight, is understood by multiple platforms, and is in fact native to the language of the Web, JavaScript.

Either choice is great in that you're using something that all experienced developers will easily understand, and that every system is capable of consuming.

4

There is no "best" decision here, it's all about your requirements. If you need very high performance, then you should use some custom binary protocol with small messages. However, this also carries drawbacks. The main advantage of XML is that it is a very standarized format which is easy to read from a wide range of platforms and application. For example, with a cusom protocol you will need to implement serialization and deserialization of your messages. Additionally, XML is a lot more extendible than a custom format.

So to sum up, it really depends on the requirements of your system.

4

Using a custom protocol will not be good idea as it would mean a lot of efforts in development and testing which is unnecessary. Also, it would be a specific implementation unless you put in lots of efforts to design it in a generic way. With XML, the transport itself would not be tied up with your application. Since XML is flexible and extensible, it allows you to change your structure and content without affecting your transport.

Though, using XML has its own set of disadvantages, like you pointed out. It is true that it involves unnecessary transfer of weight. But, alternatives like JSON would be a good idea to get around these problems.

3

Design decisions are all about trade-offs. You have enumerated what XML gives you - readability and self-description. It also comes with a description language (XSD), is extremely portable etc etc.

But these advantages come with disadvantages that you mention. So let's tackle them one by one:

Verbosity

Yes, XML is verbose, being both self-descriptive and text-based. This is only really a problem if performance is a concern. Is it? What about the positive tradeoffs?

Note that a reasonable alternative here is JSON, which is just as readable but far more efficient.

Varying Size

Yes, but this depends on the connectivity layer. If you do not have a persistent connection (for example, HTTP), or you are using a protocol that provides it's own 'framing' (such as AMQP or JMS) then this is not an issue - the transport layer takes care of it. If you are planning to reinvent this wheel, yes varying payloads makes the protocol harder. But the protocol (especially with all edge cases) is hard enough.

Parsing Overhead

This is related directly to verbosity.

3

The fact that the protocol runs over Sockets has no direct bearing on the question.

The reason so many people choose to use XML for the presentation layer of their protocols is primarily that it avoids the effort and cost of specifying and implementing your own presentation layer. Yes, if you want a different trade-off between message size, potential for change, cost of implementation, and other factors, then you may well be able to design a protocol that's optimized to those requirements better than XML is. Or, given that a lot of very good people were involved in the XML design, you might well design something that's worse (typically because your long-term requirements aren't what you thought they were - for example you might end up designing something with excellent performance but very poor potential for change). But whether you do a good job or a bad job of your design, it will cost you more than using something like XML or JSON that already exists.

1
  • 1
    Thanks for your answer Michael. I do understand that i would end-up designing something worse as a presentation layer than XML, but if i'm required to perform extremely fast and manage data traffic efficiently, don't you think that something else should be used ? Whoever designed XML, had persistent connections in mind ?
    – ehanoc
    May 21, 2012 at 12:06
2

Reinventing wheels is never a good idea. I would stick with standard HTTP protocol and use an interoperable data exchange format such as XML or JSON. You will find lots of more tools and support if you use industry standard protocols instead of reinventing custom protocols. That's in terms of best practices. Of course if you have some very specific requirements there might be good reasons to implement something more customized.

6
  • When i say "custom protocol" i don't mean a "new" HTTP or any other already existing standard protocol. But some system require their own set of rules when transmitting data. That's what i mean.
    – ehanoc
    May 21, 2012 at 7:39
  • 1
    @bmartins - Even with HTTP, it's a good idea to stick to an agreed-upon standard. If every developer reinvented his or her own means of transmitting data between servers, it would be more difficult for people to integrate with each other. May 21, 2012 at 7:47
  • 1
    @bmartins, in general standard protocols should be used. Now you mention something about custom set of rules. Well, tell us more about them so that we could discuss. Of course that there are situations where a custom protocol could be appropriate. But without us knowing more details about the systems that you are trying to communicate with it is impossible to answer your question. Not to mention that you are asking about best practices. How do you expect us giving you best practices without providing details for what kind of system you are looking best practices for. May 21, 2012 at 7:48
  • Thanks a lot for your insights. And Yeah, i do understand that. In my case i'm working on a closed/private/internal solution which absolutely required that data is transmitted as fast as possible. In this case then i believe that xml(and even json) is a bad idea. That's what i meant, sorry if i wasn't clear enough. I wanted to make the question as generic as possible because i think it's important to think about the most common/generic cases as well. For future reference. ;)
    – ehanoc
    May 21, 2012 at 7:56
  • "as fast as possible", really? or Fast enough? Your local network topology will likelty make a bigger difference than XML vs Binary encoded data. May 22, 2012 at 11:58
2

As you explained well XML has their own advantages and disadvantages. When you want alternatives to reduce network traffic then you can you consider http://code.google.com/p/protobuf/ which compresses the data and has its own marshaling techniques. And other one which is well known in web technologies is JSON.

What is best in terms of system architecture: Well we need to define whether it is Web or Enterprise System Integration and others. It is all depending on what system you are designing.

Not the answer you're looking for? Browse other questions tagged or ask your own question.