Pages

Showing posts with label Technical. Show all posts
Showing posts with label Technical. Show all posts

Saturday, May 11, 2019

Problems of the 'new' keyword

How do we create an object in most languages? It's by using the 'new' keyword and there's a big problem lurking in there! Some languages of-course skip it, like Python, where you can create an instance of class just by doing 'ClassName()' instead of 'new ClassName()' but the underlying problem remains the same.

The new keyword causes our codes to get coupled.

Let's take this simple example:

interface IShape
{
  public string Name { get; }
}
class Square : IShape
{
  public string Name { get; private set; }
  public Square()
  {
    Name = "Square";
  }
}
class Circle : IShape
{
  public string Name { get; private set; }
  public Circle()
  {
    Name = "Circle";
  }
}
class ShapeHolder
{
  public IShape Shape { get; private set; }
  ShapeHolder()
  {
    shape = new Square();
  }
}

If we see the above example, even though we have used an interface 'IShape', our ShapeHolder is now tightly coupled to the Square instance. It can never ever hold an instance of a circle. This may seem fine but this causes two major problems:
  1. Unit Tests: There's no way to test ShapeHolder independently of Square class. Any test written for ShapeHolder would end up working with a concrete instance of Square class. If Square class was modified later to start using network access or a db call, unit tests of ShapeHolder may start failing or take up huge amounts of time, as the testing environment won't have the necessary setups.
  2. Extensibility: The above code doesn't show run time polymorphism. In future if a new shape comes in, which must work with the ShapeHolder class, we will have to modify the ShapeHolder class too. This may in-turn cause some other class to be modified. This is a violation of open closed principle.
The main way to solve this is to use Dependency Inversion. Instead of ShapeHolder being responsible for creating the Square class, it should be passed in to the class, either via the parameter of the constructor or using something like the Factory design pattern. Now at runtime we can change which IShape type is being held by the ShapeHolder. For unit tests, this can be a simple MockShape class.

Friday, May 10, 2019

Space Based Architecture





Like CQRS (Command Query Responsibility Segregation), this architecture is used for high scalability and performance.
In a typical setup there is usually a client, an application server and a database. Whenever load increases, we can increase the number servers handling the client requests, once we fix that we soon find that the application server handing all the business logic is now the bottleneck. Once we scale that up we are pass the bottleneck on to the database.
Unlike the previous layers, scaling up the database is very tough. Databases are not really meant to be scaled. Sure, there are solutions out there which try to address this, but it’s difficult and expensive.
This pattern tries to address that bottleneck – the database. In this architecture, there are 5 components:
  1. Processing Unit
  2. Middle ware
    1. Message grid
    2. Data Grid
    3. Processing Grid
    4. Deployment Manager
The processing unit is the component which houses the business application. Its complete with it’s own persistence store (the database) and is self sufficient.
Message Grid is responsible for orchestrating the various requests from client to the different processing units.
Data Grid: It’s responsible for keeping the data in sync within all the individual persistence stores in the processing units.
Processing Grid: Often the request is split between multiple processing units to get even better throughput. Processing grid is responsible for this.
Deployment Manager: As the load increases, it’s responsible for spawning up more processing units. Similarly when the load decreases, it’s responsible for getting the number of processing units down.
The reason why this architecture effectively solves the database bottleneck problem is that in this solution there is no common repository. Every processing unit has it’s persistence store. So the database never becomes a bottleneck here.
Also, as the load increases/decreases, the system scales accordingly.
So, both CQRS and Space Based Architecture support scalability by having multiple instances of the databases. The Space Based Architecture scales the system dynamically based on the load, however, with this additional power comes the additional complexity. If the load fluctuates a lot, then Space Based Architecture is the better option.

A More Beautiful Question

This is an amazing book and it’s all about how to innovate. The author raises a very interesting point that we are always running after answers however, to come up with something truly innovative, we must ask the ‘More Beautiful Question’.
So what does it mean to ask the more beautiful question? It can be broken down into 3 main steps:
  • Why?
    As we grow up, our minds become more rigid and accept a lot of things because that’s how it has always been. We stop asking this basic question – why? The application is slow, um, that’s because it’s a large system which needs to serve multiple requests concurrently. No, we need to step back and ask – why? Why is the application slow and come to the root cause.
  • What if?
    Now that we have identified the probable root cause, next question is ‘what-if’? In this step we must forget about all constraints and what’s practical and impractical. We should just come up with all solutions that we can think of, and as stated before these does not have to be practical. This is important to understand, as an absurd idea in itself may not be of much value but it can lead to more and more ideas some which may actually be viable.
  • How?
    Now, that we have brainstormed a lot of solutions to the given problem, now is the time to figure out what actually is practical of all the options mentioned. Which one of these can be really achieved given the current constraints and one or two may actually fit the bill.
What happened in this exercise is that we kept aside the practical aspects till the last stage and let our thoughts move freely beyond any restrictions. By taking a step back and not chasing after answers we have allowed ourselves to think beyond the obvious and come up some potentially unique solutions.

CQRS

Software architecture has evolved a long way from the standard 3 tier architecture. A lot of the previous architectures were optimized for the constraints present at that time like limited resources but with time such restrictions no longer apply.
The most promising of the new architecture is the Domain Centric Architecture which places domain right at the center of the architecture. But more on the domain driven design later.
Another interesting concept is CQRS – Command Query Responsibility Segregation. This is useful for creating a system optimized for performance. The principle is simple, keep the command and query separate.
Command is anything that changes the state of the system on the other hand query requests some information from the system. A query should never alter the state of the system and the command should ideally not return some information. Of-course, a strong distinction is not always possible, a command does often return whether it was successful or not but keeping them separate leads to a lot of benefits.
Code level:
CQRS should be even strived at a code level. A function responsible for fetching some information should never alter the state of the class. As the codebase grows, a public facing getter API can be called from different modules. It’s next to impossible to control that and other modules may not have enough information to identify when a simple get call ended up altering the state of system inadvertently.
Architecture level:
A lot of the features in the software are data-read heavy and usually a smaller set of features in the software actually update the data. If we keep them separate we can scale them independently. The ‘Query’ module having optimized for fast reads and ‘Command’ module optimized for writes. In a little more advanced version of CQRS, both the stacks can have independent databases. Every write into the main database is then synchronized with the database meant for ‘read’. For short duration these databases will be out of sync but this leads to highly performant systems and the performance won’t degrade as you scale the system.
There are downsides of it too. Implementing CQRS makes the architecture complex and should not be used if the performance and scalability are not the primary needs of the software. Also, the query and command stacks will have data which will be out of sync which leads to additional complexity in the system.

Technical Expertise

I was going through a course on software architecture and I came across an interesting concept. It’s nothing too uncommon but it’s something I haven’t paid attention to.
In the simplest terms it states that our knowledge is like a pyramid. What we know forms a small part on the top of the pyramid. What we know that we don’t know forms the next layer. But what really takes up the most chunk are stuff we don’t know that we don’t know about.

An example of this might be that I need to create an application which should run across iOS, Android and Windows phone. Given this requirement I might blindly start using a framework like ‘Xamarin’ and start implementation. What I did not know that there is another way – creating a ‘hybrid’ app using the ‘webview’ component present in all the three SDKs. While at the end of the day Xamarin might still be the better choice, based on the requirements, but now knowing that another approach exists, we missed out on an opportunity to create a simpler solution.
Also, as we grow in our careers we need to move things out of the lower triangle and at-least move it into the middle triangle, so that whenever an opportunity presents itself, we know that a way to tackle the problem already exists. This increases our ‘technical breadth’ which is more useful than ‘technical depth’ as we go higher up the technical career path.

WPF – Using Dispatcher vs SynchronizationContext

Every time I had to update the ui from a non-ui thread I always used the Application.Current.Dispatcher and never paid much attention to it. Just call a begin invoke from the other thread and you are done. It was only recently that I realized that that’s not the only way of doing things. There is an alternative way using the ‘SynchronizationContext’ which also gets the job done. So what’s the difference?
I couldn’t figure out anything obvious so I started reading up articles on it and finally found a comprehensive article here.
So basically SynchronizationContext adds another level of indirection. Unlike the dispatcher which is WPF specific, the SynchronizationContext has different implementations – WPF and WindowsForms being some of them. So, now your code is not dependent on any technology which is always great.
Though in theory the synchronization context can be used to communicate between any two threads, not all threads have a synchronization context. Only the UI thread has the required context and the plumbing code required for getting it to work.

Devs rejoice – LINUX dev environment on the CLOUD for FREE!

Okay, not exactly the entire OS that’s free, you cant just go there, open Libre office and do everything you would normally do on a PC from a web browser. But what you can do is run your NodeJS service, run an entire web application on the MEAN stack and much more without worrying about any setup. The complete dev environment is out there.
I had never played wih NodeJS before and wanted to try it out. So I began searching on how to work with NodeJS when I stumbled upon this gem of a website – CodeAnywhere!
This is a development environment hosted on the cloud and when you log in you are presented with a ‘Devbox’ which is virtually a small VM with 256 MB RAM and 2GB hard-disk space. You can choose between a Ubuntu setup or a CentOS setup along with the technology stack be it node, MEAN, Python etc. And there I was writing a service from my web browser which got hosted on a remote location accessible via an URL with no cost at all and with zero setup time. It was phenomenal. It’s simple to create a static web-page and see it working on web browser without any real effort. But to know that it’s as easy to create a service having access to MySQL/MongoDB exposing APIs to solve your business logic is very impressive.
Only catch is in the free version, your devbox runs only for the time you are logged in. So, once you log off your service hosted on the devbox is inaccessible. But nevertheless it is an excellent tool for learning.
On the downside, their app for iOS is really not upto the mark.

Sharing and Applying multiple Event Handlers – JavaScript / jQuery

This was back when I didn’t use Angular and used jQuery a lot for making my views interactive. A thing that I learned back then was that class can be used to not only share styles but functionality.
Unlike the ‘id’ which is unique to an element, same class name can be shared across multiple elements and helps in reusing styles using CSS. Or at-least that’s the extent to which I used them.
So, here I was creating a form and I realized that along with styles, there was also common functionality. A lot of the fields in the form shared validation logic and when using jQuery I realized that I can simply pass in a class name to the ‘$()’ and I am done!
I can attach the same event handler to all elements which had that class name. Also I can add multiple event handlers to the same element (of-course ensuring that the event handlers are not order dependent).
This simplified the code greatly as I could now add a class name and I would automatically get the relevant styles and the required event handling applied.
Sample Code:

<html>
  <head>
  </head>
  <body>
<form>
      Name: <input type= "Text" class="MustNotBeEmpty"/>
      Account Number: <input type= "Text" class="MustNotBeEmpty MaxSize6" />
    </form>
    <script>
      $(".MustNotBeEmpty").blur(function ()
      {
        if(this.value.length === 0)
        {
          alert("You need to have some value.");
        }
      });
      $(".MaxSize6").blur(function ()
      {
        if(this.value.length > 6)
        {
          alert("Value cannot exceed 6 characters.");
        }
      });          
    </script>
  </body>
</html>

So now when the validation occurs: