Microhyde and FrankenSoft

Microhyde and FrankenSoft

I've probably written this post a bunch of times over the years, some versions only in my head, others languish in my drafts folder.

This post is about Microsoft, specifically the painful lessons of their legacy and their marching into recreating a whole new set of painful legacy problems for the next generation.

Windows - not so smooth operator

I'm not really sure who is running the OS Division these days: Engineers or the Sales and Marketing departments. They appear to have been at war with their users for some time. Perhaps it's always been that way and I just didn't notice.

From the days of IE anti-trust lawsuits or of Bing's attempts to counter Google search, Cortana as a response to Siri, the envy of seeing other App Stores ecosystems thrive while MS Phone store withered and died (I loved my Win phones too).

Search market share

The rise of tablets and mobile computing, which recently saw Android become the most used operating system, has seen Windows make ever more user-hostile attempts to maintain its market share and relevance.

OS market share

The dirty tricks employed during the 'Upgrade to Windows 10' SaaS campaign defied belief at times, so hostile were they to those who simply didn't want it following the version 8 and 8.1 disasters.

The 'hints' that chrome is slow and "wouldn't you like edge instead" are a throwback to the IE-bundling behaviour that I think isn't really working out.

Brower market share

The latest round in the OS-war on users is the introduction of adverts in the disappearing/reappearing start menu. I cannot imagine this is what users actually want.

For the time being, however, windows is still by far the dominant desktop OS, but I do wonder if the Windows 10 stats would be quite as high if they hadn't tried every trick in the book to force users to upgrade.

Desktop OS Marketshare

I get it, though. I'm not their target demographic, a developer, a power user with no interest in an closed-ecosystem app store and no desire to use even a fraction of the 'features' like Cortana, Bing, Edge etc.

The problem with this thinking though, is that there are the millions of people like me who help their families with their computers, who advise businesses on future technology and who develop the ecosystem of non-MS produced applications that help make software ecosystems successful.

Windows OS Marketshare

The admittedly hilarious Ballmer chants of "Developers, Developers, Developers" ring somewhat hollow these days when it comes to the OS side of things. I'm sure their dominance on desktop still makes developing UWA's for the Windows 10 store financially appealing, but with cloud and mobile apps now dominating overall, there is more incentive than ever to spend your development dollars elsewhere.

Full disclosure here, if it wasn't already obvious: Having used windows since it came on 5 multi-coloured 5 1/2 inch floppy disks, last year, I have mostly ditched it for Linux Mint as my daily OS and I really don't miss it.

The only thing still keeping me on Windows is Visual Studio and my work (which is on Windows 7 with no plans to upgrade at present).

Windows just seems like a hostile environment these days because I choose not to use their 'features'.

Browsers & the law of unintended consequences

I remember that Internet Explorer as far back as version 3 produced IE-only features baked-in to the browser. Further, the browsers have consistently been baked-in to the operating systems.

Given the life spans of XP, Windows 7 and IE versions 6-9 with its platform dominance - doubly so in corporate environments - there were, and still are, a huge number of applications written as 'optimised' for IE (an oxymoron if ever there was one).

As a developer who worked on and off with SharePoint and CRM for many years, each new IE browser often crippled these applications and would start the 'compatibility-mode' bum-fight which is still continues to this day.

Rewriting any software that extended these systems every three years was a game that got really old, really quickly.

Worse still, are the silent mass of desktop applications that use the 'embedded browser' control with embedded content that is 'IE optimised'.

Further compounding these problems, the compatibility-mode renderer wasn't even consistent between later versions of IE.

The decision to bake in browsers to specific operating systems is arguably one of the worst, most backwards-compatibility-hostile acts of enforced obsolescence in the history of computing.

Consider how much time, effort and money must have gone into rewriting broken browser-based applications. It must surely be measured in billions of dollars.

Chrome, far and away the most popular browser on the planet, as well as Firefox, Opera all have cross platform versions. There is no reason for a browser to be baked-in to an operating system.

And now with Edge, only available on Windows 10, it's more of the same. Edge, at this time, feels more like an alpha product to me, some way behind all the major browsers.

Both IE and Edge still don't support Server Sent Events (SSE) - perhaps because they have an abstraction for the transport selection in SignalR - but it's a notable omission which has been under review for years now, along with a number of other features already supported by all the other major browsers.

CSS Grids recently provided an example of the difference between the major browser vendors' approaches.

CSS Grids

Edge nor IE was among them.

One of the most interesting and exciting features that has the potential to create a tectonic shift in web development is WebAssembly. Again, no Microsoft representation here either.

If you, like me, still to this day, have to cope with the issues surrounding a bunch of IE-optimised (embedded like a blood-sucking tick) applications in your environments, perhaps you understand where I'm coming from.

These compatibility issues cast an extremely, EXTREMELY long shadow.

Microsoft themselves must have viewed the IE situation as unrecoverable from a reputational point of view to have effectively tomb-stoned IE in favour of Edge on Windows 10. Yet they chose exactly the same approach of baking-in Edge to the OS.

They really haven't learned from their past here at all.

Appetite as a measure of success

When .Net first came out, there was a quick uptake. It was a clear improvement over classic ASP. The framework had plenty of pain-points but it shipped alongside tooling that let people write production-ready apps from day one.

When .Net MVC came out, there was a quick uptake and a viable path to launching production-ready apps. Again, it was a great improvement over WebForms by decoupling the markup from business logic. Improved testing, much better extensibility and streamlined execution pipeline were the motivators to switch.

However, again, there were pain-points for those creating large-scale apps baked into the MVC design.

Other .Net MVC frameworks arose in this time as a reaction to those pain points such as Nancy, FubuMVC and ServiceStack.

Many developers also left .Net altogether as alternatives like NodeJS or Ruby on Rails came out. Many '.Net is dead' 'Why I'm rage quitting MS' posts were written.

Let's be clear: this post may be one big gripe directed at MS, but I'm not quitting .Net. I think it has a bright future, in fact, more so than at any other time since it first came out.

MS tried to reinvent JQuery with their own JS DOM abstractions but after a number of years gave up and threw it under a bus. Same with their JSON deserializer. They introduced the service locator pattern!.

There is nothing wrong with reinvention as an attempt to compete and improve. May the best one win....but using the platform, defaults and tooling to create lock-in generates chronic long-term pain and a ton of legacy apps out there that will struggle to be refactored around the baked-in designs which require a more scorched-earth approach to update.

There is a larger issue here with the introduction of .Net Core, however.

How a cloud fought an emperor and x-plat was reborn

Azure runs on Linux, in fact most, if not all, the major cloud platforms run on Linux. SQL Server is going to run on Linux. Visual Studio for Mac shows a non-windows IDE is possible. So I expect sooner or later that it too will run on Linux (especially if Project Rider proves popular). Windows resurrected its POSIX/Linux subsystem and, again, it wouldn't surprise me if the future of windows is as a Linux distro.

So it's not that surprising that .Net Core was announced to much excitement and enthusiasm. I shared that enthusiasm and still do. It opens up choice and some genuine competition. People can choose the OS and development stack they want knowing that there won't be feature-disparity or lag between platforms. Creating cloud apps for Linux reduces licencing and hosting costs.

At NDC London last year, Scott Hanselman quipped that Microsoft didn't really care which platform you ran on, they just wanted your for loops on Azure.

It goes further than that, as you're not tied into Azure either. You can choose any cloud provider and that's really great for driving healthy competition.

Version 1.0 and 1.1 are out and 2.0 is on the way. It's a grand effort to re-imagine .Net cross-platform without the split effort of mono as well as fixing some mistakes of the past. I love that!

So, now, we are a year or two into .Net core but the landscape is very different from the original ASP.Net and MVC introductions.

They aren't releasing something new, so much as re-creating something that already exists, but cross-platform and without the framework installer dependency. From this huge undertaking, understandably, there's a lot of 'stuff' missing from the initial releases.

However, rather than focus on this missing stuff, the debates on GitHub revolve around reinventing established frameworks (including MVC), libraries, concepts and of 'baking-in' anaemic versions of popular full-fat .Net libraries, rather than focusing on feature parity, tooling and migration.

Their OSS efforts here are definitely an improvement. When you see the sausage factory in action though, it often isn't pretty.

But publishing your source code online isn't Open Source. Nor is steering your language, tooling and frameworks in ways that drive your parallel commercial interests.

There is a fundamental conflict of interest here that few other development communities have to deal with.

One of my main gripes here is not that they changed their minds numerous times throughout the process - that's entirely fair I think - but that they split the platform and tooling efforts.

In their honest desire to join the club of increased release-cadence, the split in tooling support has crippled many developers' efforts to port early versions of their established projects to .Net Core.

Even with two versions out and the third imminent, there are remarkably few libraries shipping with multi-targeting for .Net Core and I suspect even fewer production apps out there in the wild running on Core.

There simply isn't nearly the same uptake that happened for .Net and .Net MVC either.

Their packaging, tooling and versioning schemes have been confusing and disjointed. Their seeming willingness to throw established well-maintained OSS projects under the bus by baking-in the lowest common denominator abstractions is confusing as well.

The effect, even if they end up dominating the space, is that 'baked-in' is always expensive and painful for applications to 'bake-out'.

It's a preference for frameworks over libraries that creeps in over time. A lack of using simple dependency-free components that when chained together become greater than the sum of their parts, i.e. the Unix philosophy in action.

Breaking compatibility with both tooling and frameworks ends up isolating and atrophying applications that become enslaved for those who can't expend the resources on yet another rewrite/upgrade.

It is the kind of scenario in which many experienced developers, so valuable to the ecosystem, just give up altogether for alternative languages and platforms.

It's hard for me not to view all this as wasted energy, of building monoliths, while they, along with the industry, preaches on the value of reducing complexity through building components with clearly-defined boundaries.

By not focusing on educating developers and decoupling their own products, aren't they just storing up the next round of future backwards-compatibility headaches and perpetuating the problem all over again?

The root cause, is a lack of educating, teaching, guiding.

You don't teach kids Maths by giving them a calculator. You first show them how to do it themselves. THEN and only then do you give them the shortcut tool.

Should it really be Microsoft's job to teach, however?

They are, in the end, a business, there to sell products and make money. They are not a charity or educational institution.

Their desire to give developers ready-made solutions to provide the quickest possible productivity-gains makes sense from an IDE perspective; but when this bleeds into the frameworks and tooling, I think it just perpetuates the industry-wide epidemic of failed software projects and quite frankly industrial-scale mountains of crappy designs and code.

My view is that in the long-term this approach does much more harm than good.

Whether they view it as their responsibility to improve the overall state of the industry or not, the effect of providing OOTB designs that developers don't properly understand and which are poorly implemented nor explained, often results in predictably poor applications.

Over time, the cumulative effect is that it's not the application developers who suffer but the language, platforms and tooling that get a bad reputation as being responsible for producing poor software.

So they ocean-boil and re-brand, now it's new, shiny and all better, you just have to rewrite all your apps again.

It's the law of unintended consequences again, and this thinking displays a lack a reverence for learning from past mistakes and looking at the underlying motivations for these repeated patterns of re-invention.

I know, it's easy to criticise the big guy, but I've read so many threads, thanks to the openness of the process on GitHub, where MS have been warned repeatedly about the choices they are making by well respected and experienced developers. In too many instances IMO, they have paid no heed.

Mac attack!

Scott Hanselman recently called me out on some hyperbole in a reply on GitHub.


He was absolutely right, of course. I have nothing but respect for Scott, always a voice of reason, a nice guy, a gentleman, and I did make a silly comparison in my first sentence of my response.

What is a shame, though, is that my tongue-in-cheek hyperbole undermined the rest of my point. He really didn't engage with what I was trying to get across about 'reinventing' and efforts towards tooling.

I've lost count of how many times I've seen tweets like this one in my timeline just today.
I see this a lot

I'm sure both Microsoft and other .Net developers will disagree with me on this, but I think they ARE wasting time re-inventing things; there HAS been a chronic lack of focus on the tooling and there IS more than an aroma of ocean-boiling going on in a few areas.

It's one thing to focus on shipping features in MVC, Razor, SignalR etc when you have the core stuff down.

No, my poorly-worded off-topic point, was that only when you have the basics right with .Net Core's tooling, should the focus then be on delivering the frameworks and framework-tooling to build on top of it.

Creating shims over yeoman, MSBuild, Nuget, test runners etc is not advancing the core areas, and it just looks like they are trying to copy the CLI's of others out of a fear of being left behind somehow. It brings nothing new to the table.

They really haven't got that part right at all.


Looking through the now open-sourced MSBuild code is like stepping into a decade or two of cruft and backward-compatibility bolt-ons.

Now, that isn't unusual, a lot of critical long-lived projects end up like this but I would far prefer that more of their considerable resources were focused on tidying this project up and improving it for the next generation of application development. There is so much more they could be doing here.

The MSBuild sourcecode is laden with feature-based pre-processor directives to cleave off platform-specific code. This IMO is code-smell that should have been be refactored and logically grouped into feature-specific abstractions and implementations.

It reeks of bolting on x-plat support in a hurry.

The logging and error handling is sprawling and hard to follow. The collections, immutability, async, scheduling and queuing are bespoke implementations that look like they haven't been updated using the advances made in those areas in the .Net framework. Maybe that's just my failure to grok the code though.

My biggest bugbear by far, however, is the project system.

Laden with Visual Studio and Windows-specific hooks for SDKs and options for things installed with Visual Studio, this is the cause of so much developer pain.

I personally have spent far too many hours of my life working around this stuff.

The build system is the bedrock of any stack, it is the core of organising, compiling, tooling and continuous integration. So to have heard so many times over the years the same old discussions around "install the IDE on a build server" for solving CI issues should be an obvious red flag for discomfort, like an infant-behind-you-screaming-continuously-on-a-12-hour-flight obvious.

Out of this pain arose an army of VS plugins, MSBuild shims and, in more recent times with the arrival of Nuget, packages that counteract this M.A.D. coupling between IDE and Build system.

Why should it be left to the VS EnvDTE to be able to manipulate a project, adding files or folder or changing configuration settings? It's awful stuff to work with.

At this time, there still isn't a set of MSBuild nuget packages that you can reference to load, parse and manipulate a project prior to compilation sans-Visual Studio.

MSBuild is being worked on and their roadmap shows that they are working towards a dependency-free x-plat nuget package, but there is no timeframe for this.

Given that we are nearly 3 versions in for .Net Core, it clearly hasn't been given the same prominence or resources as the 'shiny stuff'.

Microsoft won't back-port either the tooling for .Net Core projects to VS 2015. This, again, is likely because of coupling.

Yet, in the Roslyn and typescript compilers, they showed clearly that they understand why this separation is important and useful.

You can watch Ander's discuss the how and why in this great video on compiler construction and you might get a sense of why I think MSBuild is sorely lacking a VS-decoupled project as a service API.


Roslyn was a huge undertaking by Microsoft that genuinely did something unique in the compiler space no one had done before.

All compilers (that I know of...) before were black boxes where source code and settings went in and artifacts came out.

Every implementation of intellisense had to effectively recreate the lexical parser of a compiler, and often the semantic model too if you wanted things like type-checking or flow-analysis in your IDE.

Roslyn's remarkable abilities exposed these things as consumable services which allowed any IDE to leverage them. One implementation, provided directly by the compiler.

Anders discusses in that video above how the same approach drove them when they were building the Typescript compiler.

With Roslyn, the compiler black box is no more, it's a rich pipeline that when you tap into, can be leveraged in amazing and interesting ways.

One huge disappointment with Roslyn is that they stopped short when it came to adding compile-time hooks for sourcecode rewriting and generation to enable all sorts of interesting AOP-style projects to be born.

Roslyn has been RTM'd nearly two years now and the uptake for design-time code analyzers and fixes has been healthy, but there still isn't a good story around compile-time hooks.

It is a hugely difficult problem when you dig into it, but, it is the type of feature that can only really be delivered by the language teams, and not, as I have frequently read in response to requests for it, "we are waiting to see what the community comes up with".

IMO it should be prioritised over every other language feature for one reason.

It has the potential to create an entire ecosystem of libraries, tooling and innovation around it. Advances like pattern-matching and record types whilst important, do not.

The best option for compile-time code generation currently available for Roslyn seems to be Andrew Arnotts CodeGeneration.Roslyn. It's a great start at wiring up the build hooks for doing attribute targeted generation but it is still only capable of nibbling at the edges of this feature.

Zed's dead baby, Zed's dead

MVC vNext briefly created the ICompileModule with a weird folder/file hook convention for compiling MVC Razor views, a similar but again limited approach to source generation, but this was scrapped in favour of source generators.
A feature, as yet unreleased, that again looks somewhat stilted from the docs and with a demo project that references non-existent packages (publicly at least).

The opportunities in this space are immense, like eliminating lots of expensive run-time reflection or expression-heavy code.

The next object mapper could just generate left right code.
DI containers could achieve invocation performance parity with new Foo() without compiled expression tree trickery.
The next Postsharp or Fody AOP framework could just re-write the code instead of expensive ILWeaving.

It's disappointing that this hugely difficult but interesting problem hasn't been more of a priority so far.

S'all bout the bootstrapping y'all

Microsoft have talked a lot about wanting to streamline the process of on-boarding with .Net Core using CLIs, templates, etc.

Yet when fundamentals like the build and tooling dependencies are not explicit and instead powered by magic or coupled to an IDE, you end up with projects you cannot just clone, build and run. There ends up being steps to install this, configure that.

So when a new IDE version comes out and you're building on a cloud platform that doesn't yet have part of that IDE installed on the virtual machine images, well sorry, you're out of luck.

Now, they have made progress of sorts in this area. On the 3rd March they announced Visual Studio Build Tools as part of the VS installer. Its back to the installable-SDK approach for CI environments but it's still coupled to the VS installer for some reason. No other build tool does this kind of stuff. It's an archive file or a single binary usually.

There also recently released VSWhere but, again, is just a Band-Aid on the issue of not being explicit about the build tooling and resorting to 'find dependencies by magic'

They just seem to have an eternal blind spot when it comes to this stuff.

The build and tooling should be independent from the IDE. Period.

Going further I'd like to see the IDEs consume an MSBuild project service, much like intellisense and analysis with the Typescript and Roslyn compilers.

Part of the reason for this coupling between IDE and building, I think, stems historically from TFS. A build server with all the same shared guts as Visual Studio and shipped together. It was easy for Microsoft to just recommend using both, and all those coupling issues were solved.

I'm old enough to remember when Microsoft charged enormous fees for TFS. In the face of rising competition from CI servers like TeamCity and Jenkins they basically started giving it away.

Now with the rise of cloud and online VCS providers like GitHub and BitBucket, services like AppVeyor are once again transforming the CI server landscape and TFS (or Visual Studio online) are further away than ever from dominating in a more competitive ecosystem.

Maybe this dual VS/TFS coupling was done to block competing IDEs and CI servers; perhaps it largely worked in a windows-centric, MS-only corporate environments before the cloud was even a spec on the horizon.

With the rise of cloud CI services and the imminent arrival of the first credible Visual Studio contender in JetBrain's Project Rider, there has never been a more compelling reason for Microsoft to ditch these old coupling tricks.

JetBrains IDE's

One only has to look at the foundations of Rider's IDE to understand that when you don't have a platform or ecosystem that locks in developers for visibility and market share, you cannot provide a substandard tightly-coupled product and still hope to succeed.

Jetbrains built their own lexical parsers for a wide range of languages that the ReSharper core uses, along with a unified plugin system that is powered by the same x-plat IDE.

This isn't an accident. It's a good business strategy that doesn't try to take customers hostage to up-sell them TeamCity or YouTrack.

Each has to live and die on its own merits.

The road to hell is paved with good intentions

Now I've been bashing Microsoft here a lot, but I'm not one who subscribes to the "Microsoft is evil" camp. I don't believe they are malicious or hell-bent on destruction or anything like that.

There are plenty of really great, hard-working, honest and sincere people working there who only want to do good.

They're a big corp, lumbering...yes, sometimes overbearing, misguided plenty and often confused as a result, I think any organisation that size suffers from the same syndromes.

For developers, since the introduction of .Net and with Scott Guthrie at the helm, things definitely improved a lot in a lot of areas over the years.

Creating the closed ecosystem is old Microsoft thinking.

I'm just not sure that new Microsoft thinking is any different when it comes to its tooling...


what do you think?

00011 : Service discovery, load balancing and routing

00011 : Service discovery, load balancing and routing

ServiceStack, a journey into the madness of microservices
  1. Context: the what and the why?
  2. Distributed debugging and logging
  3. Service discovery, load balancing and routing
  4. Service health, metrics and performance
  5. Configuration
  6. Documentation
  7. Versioning
  8. Security and access control
  9. Idempotency
  10. Fault-tolerance, Cascading failures
  11. Eventual consistency
  12. Caching
  13. Rate-limiting
  14. Deployment, provisioning and scaling
  15. Backups and Disaster Recovery
  16. Services Design
  17. Epilogue

In the previous post, I covered the challenges associated with debugging and logging RPC calls across distributed systems. Now let's turn our attention to how those RPC calls work in your services.

This boils down to the fact that services using RPC calls rely on services that are in another process.

As a system grows, and services are added or removed, keeping track of what services are available and where they are becomes an issue.

You could hard-code in each service the locations of the services it depends on, but that tends to break down once you have two services and need to add the third!

Once you need to run multiple instances of the same service, or use containers and elastic scaling, suddenly DNS propagates too slowly and you don't know where everything is.

Re-deploying your service every time another service on which it depends is updated or moved means you must decouple these type of dependencies between services.

It quickly becomes apparent that you need a more dynamic solution.

You need service discovery.

O Services, Services, wherefore art thou Services?

There are a number of tried-and-tested methods for discovery to be found in DHCP, Bonjour, uPnP, SSDL and DNS-SD. For web-based services, UDDI and WS-Discovery have come and - for the most part - gone.

Newer solutions like Zookeeper, Etcd and Consul have emerged to offer service discovery.

Gateways like NGINX also provide routing options which can be used for decoupling service-to-service calls.

Enterprise Service Bus systems like NServiceBus and MassTransit also can be used in a pub/sub messaging pattern to decouple service-to-service calls.

I've mentioned just a few but there are many more. You have a lot of options here, so how do you choose?

Let's first briefly cover some different patterns before I cover what we have chosen to use and why.

Centralised Registry vs. Self-Discovery

There are two common patterns that you find in solutions for Service Discovery.

The first is the service registry, a centralised database that stores the location of a service.

The second is self or auto-discovery where there is no central database and is often found in zero-configuration networking. Instead, clients use a variety of approaches to broadcast packets across a network to request a remote service and wait for the required service to respond with its location.

The service registry is another single point of failure (SPF) in your infrastructure but can provide more operational control. When used with server-side discovery, which is often found in gateways, it can completely decouple any discovery logic from the services.

Zero-configuration networking can be generous on security within networks to permit devices to 'just work' but can be more challenging to secure as systems span networks. It is often more suitable for smaller networks (uPnP, Bonjour etc.).


There are four common types of service-to-service communication.


  1. Point-to-point : services talk directly to each other.

  2. Gateway : acts as the middleman, handling the routing of requests and responses between services.

  3. Gateway Request : the responding service replies directly to the calling service rather than return through the gateway.

  4. Message Queue: services publish messages to a queue, the responding service subscribes to the messages published and in turn publishes its response to the queue for the original service to subscribe to.

Point-to-point involves the shortest route so is often the quickest but requires each end-point to take a dependency on your discovery mechanism.

The gateway can decouple many concerns from your services, handling not just routing, but caching, front-end to back-end bridging with HTTPS termination, transport conversions like HTTP to TCP/IP, formats, aggregation and load-balancing, to name just a few.

The message-queue pub/sub model is slower and is more suited for longer running processes.


For service registries only, the registration can be handled by each client directly or by the server.

As with server-side discovery, server-side registration completely decouples registration from your services.

Further reading

I've only really scratched the surface on the above keeping the explanations as brief as possible, as I want to get on to some specifics, but you can find a much better, more detailed overview of Service Discovery in Chris Richardson's excellent post as part of his series on Microservices.

Chris also has many video talks and articles available online and speaks very eloquently on all matters relating to distributed design which I have greatly enjoyed during my own research. I highly recommend checking them out.

It's make your mind up time.

So this is the first critical point where we had a variety of choices to make in our design.

Do we want smart versus dumb pipes? How about decentralised control with auto-discovery? How does our communication behave? Who controls registration? Is one single approach for all scenarios even practical?

For us they are opinionated and deliberate choices.
Our approach that follows is not inherently better or worse, but each choice has consequences for many of the subsequent design decisions. In many cases, they can actually remove choice.

We will come back to reference these choices in the rest of this series.

It is also worth pointing out that I couldn't try out everything available, so our choice is not a reflection on other solutions out there, it is just the one I felt best fit ServiceStack and suited our needs.

And the winner is

Consul, let's cover the basics of Consul before we tackle how it fits in with ServiceStack.

Consul is a single binary executable that can run on Windows, Linux or iOS. It can run either as a Service, an Agent or for sending Commands to other Consul instances.

We use it as a service registry with client-side self-registration, client-side discovery and this enables point-to-point service RPCs.

Consul Datacenter

Consul, like all service registry patterns is a potential SPF, but is designed for High Availability in mind.

In production, you run an odd number of Server nodes which form a DataCenter (DC), typically three or five. You can scale Consul to connect multiple datacenters.

The odd number is because it implements a consensus protocol based on RAFT which holds leadership elections, and they need a deciding vote to elect a leader.

For the best possible resiliency, server nodes can be spread across physical hardware, network locations and operating systems. Running three instances allows a single node to fail while running five can tolerate two node failures.

Consul is actually a hybrid model of server and client-side, something also found in Netflix's Eureka. This approach avoids one typical drawback of client-side discovery and self-registration systems i.e. network availability and latency.

It avoids this by using local agents on a loopback address.

Consul DataCenter and Agent

Each service has access to an agent co-located on the same physical hardware. Consul uses a gossip protocol Serf for managing membership, failure detection and message broadcasting and RAFT logs to keep each agent's list of services synchronised.

This means lookups and registrations are local and fast with no network hops.

ServiceStack [enters stage left]

This is my discovery solution, there are many just like it, but this one is mine.

So now we've made our first design choices, let me introduce our next plugin.


There is a detailed readme on the project which, as in previous posts, I won't cover here, but the minimum code to configure discovery in your ServiceStack AppHost is as follows:

public override void Configure(Container container)  
    SetConfig(new HostConfig
        // the external url:port that other services will use to access this one
        WebHostUrl = "http://api.acme.com:1234",

    // Register the plugin, that's it!
    Plugins.Add(new ConsulFeature());

Your ServiceStack instances can now communicate with each other requiring nothing more than a copy of the DTO POCO. This is where ServiceStack and it's DTO message-driven style really shines.

You interact with local and remote services solely through simple DTO POCO message contracts.

For most service discovery solutions, you have to know first which service you want to call. Not so for our plugin.

The difference in calling a local or remote service is indistinguishable in your code.

public class MyService : Service  
    public void Any(RequestDTO dto)
        // The gateway will automatically use the DTO type to find the correct service
        var internalResponse = Gateway.Send(new InternalDTO { ... });
        var externalResponse = Gateway.Send(new ExternalDTO { ... });

This makes it easy to develop all your services in a single instance. You can then split them out as you need to scale, but your calling code remains exactly the same.

There are no references and no uris.

Just look at the code and let that all sink in for a second.... it's more ServiceStack magic and it's so simple, it has caused a few WTFs!

Behind the curtain, the wizard is revealed

So how does it work?


When the AppHost starts up, it registers itself with Consul. In doing so it passes a list of all the DTOs it is able to process.

Combined with ServiceStack's ability to export its DTO's and its native pre-defined-routes this makes it easy to move service methods between projects.

To call a remote method, the callee service only needs to have a copy of the DTO (the contract) with the correct name and structure as the remote service.

The gateway will recognise any DTO it cannot process itself and instead look up the correct service from Consul.

This allows our plugin, with Consul's help, to provide automatic and completely transparent DTO routing.

This also avoids the overheads of message-bus and gateway-style discovery by allowing point-to-point communication between services.

The verbiage on verbs

It is worth expanding slightly to cover how HTTP Verbs work in ServiceStack.

By default on the ServiceClient.Send() and Gateway.Send() or Gateway.SendAsync(), the verb will default to use POST.

There are two methods by which you can control this behaviour.

The first is to use the verb specific methods available on the ServiceClient:

var externalDto = new ExternalDTO();  
var client = new JsonServiceClient("http://myservice");


// HTTP PUT Async call


The second method, which is only available to the Gateway, and the one we therefore have to use, is the IVerb interface markers on the DTOs.

public class ExternalDTO : IGet, IReturn<ExternalDTOResponse>  

// Gateway.Send() + IGet is an alias for Gateway.Get()
Gateway.Send(new ExternalDTO());  

The approach also helps decouple the HTTP verb specifics of any external calls from your call site and instead makes the DTO responsible for defining how it is sent.

But wait, there's more...

In addition, Consul provides another piece of the infrastructure jigsaw which our plugin handles for you - service health which we will cover in our next topic.

The gateway will also select the correct format for retrieving the DTO. If your remote service only communicates in XML, it will transparently call it using XML but return you a POCO.

It will also automatically cache responses from a GET request according to the remote service's cache settings. In some cases, it will not even issue an RPC, instead returning you the DTO response straight from the cache.

Our future roadmap also includes configurable time-out, retry and cache fall-back policies.

Let's get down to brass-tacks, how much for the API..?

We think the simplicity and low-ceremony approach above is really compelling, but it doesn't come for free. There are opinionated choices we've made to allow it to work this way.

So this is where we cover the consequences of those decisions and the first one is a whopper.

We've thrown RESTful routing under a bus

Oh my!

Hiding from RESTafarians

Now we have reasons for this which I cover next in routing. It may be possible to make this work with Consul, but I don't yet see a way to make it robust nor elegant.

DTOs MUST be globally unique.

This one is actually part of the ServiceStack guidelines anyway so we don't feel bad about this at all.

The third is another whopper which I have a whole topic devoted to later on so for now, I won't clarify further but instead lob this like a grenade into the fire-pit.

You cannot EVER make a breaking-change to a DTO

Run Away!!! <Runs away>


Instead of REST and all the great custom and fallback routing options in ServiceStack, we have chosen to use only ServiceStack's pre-defined-routes.

Together with our second consequence of globally unique DTOs, this allows the RPC routing to just work with Consul.

So let me try and explain why we've not only ignored RESTful routing, but will actively seek to prevent it being used directly in our Services.

There are a few reasons behind this but first it might help to clarify that we plan to use services internally at first, but later on expose them externally using a Gateway to be built on top of Consul.

Internally, with ServiceStack's ServiceClient and the DTOs, you already have fully end-to-end typed API calls so never really need to see a URI, let alone care what they are, this isn't so bad for them.

We expect that most of the internal calls will use this typed approach.

You can use custom routes, and the service-to-service calls will even use them. This is not really the problem area though.

Any non-ServiceStack client that wants to consume the services would have to go via Consul to find the right service, and Consul doesn't know a thing about your custom routes.

This affects the few internal apps or services that do not use the ServiceStack client and probably the MOST important group, the external clients.

Friends don't let friends break contracts

Hey Bob,

thank you for being a loyal customer, you mean the world to us.

Because we love you so much Bob, were superduper excited to announce our brand new [feature] and tell you how it will change your life.

You'll literally forget your own name, that's how amazing it is!

Here is our super-secret incrementing beta code, just for our most special customers, like you Bob.

Code: 37,027,491

Thanks again Bob, you're so amazing!!!

p.s. [Feature] requires you re-write all existing integration before launch at 3pm EST tomorrow :)

$#c*$%g WHAT?!

In accessing any external resource, the last thing you want as a consumer, is for that contract to change.


It's painful, it involves additional work you can't plan for, work you don't have time to do.

In HTTP, these are contracts:

// Fragile, things which could change are both 'ordered' and 'embedded'

// Fragile, change requires running multiple endpoints and causes 'churn' for clients

// predefined route *never* changes, DTO is the contract and *will not change* 

In code, these are contracts

// Fragile, change to signature or return type, breaks clients (see WCF, WebAPI)
public string GetAccountOrders(int id, bool includeCompleted) { ... }

// message contract, any change to DTO, does not *have* to break clients
public AccountOrdersResponse Get(AccountOrders request) { ... }  

Contract stability is of paramount importance, but addendum's to contracts are OK.

So clumsily put, if we ensure our DTOs are backward-compatible, we have far more stability in our contracts. Contracts that can tolerate change. Contracts that instil confidence and the trust of consumers.

Another reason for avoiding custom routing in ServiceStack is the complexity of making it work correctly.

In what order do I add this service's routes to the routing table?

Will a fall-back or over-generous catchall route suddenly grab all other services requests?

Will the new dev/team remember to respect the guidelines?

As I mentioned previously, adding an external gateway is part of our future plans and we expect it to handle things like load-balancing, traffic shaping and SSL termination, all in one place, rather than in each service.

If in that future, we must have RESTful routing, it will be as a decoupled, globally managed concern in that gateway, carefully managing the mapping of routes to services. Even this though, by its nature, is static and prone to 'churn' in such a dynamic environment. (see schema changes in ORMs)

We are currently looking at a few options for Gateways so I'll simply mention one that stands out so far, Fabio

It looks to have great integration with Consul and avoids the need for more complex Consul-template solutions. Another one for the roadmap.


Finally, for this (not so micro)-post we come to load-balancing or
the ability to distribute requests between multiple instances of a service.

Definitely our weakest area of the three right now, we have some plans and ideas but they are still in their infancy.

Consul provides service-to service calls with a not-really load-balancing version of load-balancing.

It keeps track of round trip times (RTT) for its agents using network co-ordinates.

If you have multiple instances of a service available to process a DTO, Our plugin will sort these by the agent RTT, giving you the most responsive.

This isn't really load-balancing, more QoS, but it is useful nonetheless and worth mentioning.

Another thing Consul gives us is in how it maintains separate service catalogs per datacenter. Using this ability, we could locate datacenters and their services in different geographic regions to even out global traffic loads.

For true load-balancing though, we have to look for other solutions and they lie outside of each service.

A gateway is the most obvious candidate for this and Fabio allows you to split traffic between services based on rules, useful for things like canary deployments as well as more traditional load-balancing.

In the world of microservices however, we actually have all the ingredients we need to make something ourselves if we need to.

Having a service registry in Consul with RTT, Health and performance metrics information from logging for every service end-point opens up interesting possibilities for using that data. Combined with a good automated deployment pipeline, there are possibilities for elastic scaling. I'll explore this in more detail in the deployment topic.

Wrap it up, chuck!

There is a constant tension between how much 'smarts' you put into each service and how much is centrally managed. We are trying to find a good balance.

The service discovery and registry is a fundamental part of our overall design though. I think it allows us to decouple a lot of the other parts we will need on our journey to microservices.

Parts that can be independent, composable, infrastructure-centric microservices of their own because of this design.

So at last we come to the end of part III.

There was a lot to cover here and there are parts I feel I haven't explained as well as I could, and parts I have skimmed over or left out entirely.

Definitely a couple of things to divide opinions.

If I've missed anything, or you have your own great ideas or projects, let me know in the comments.

Also, we'd love others in the community to get involved with the plugins on Github so don't be shy.


so without further ado...NEXT!

Let's do microservices!

next up: Service health, metrics and performance [coming soon]

Parsing Enum Strings

Well I thought I would kick off the blog with this handy generic method to parse enum strings.

public static T ParseEnum<T>(string enumText)  
    return (T) Enum.Parse(typeof (T), enumText); 

So lets compare the usage generic vs. non generic

public enum Vehicles  

public void TestEnumParse  
    //Create a enum value
    Vehicles myVehicleEnum = Vehicles.Car;

    //Get the enum string
    string vehicleString = myVehicleEnum.ToString();

    //Convert the string to an enum using non generic method
    Vehicles nonGenericParse = (Vehicles) Enum.Parse(typeof (Vehicles), vehicleString);

    //Convert the string to an enum using the generic method
    Vehicles genericParse = Helpers.ParseEnum<Vehicles>(vehicleString);

You could create ParseEnum as a string extension method however I’m not sure its a good idea to have a parse enum method on every string created. I use the generic method from a static helper class and I think its a much cleaner way of converting the enum string.