Saturday, November 12, 2016

Why NoSQL (vs SQL)

NoSQL databases that enable storing unstructured and heterogeneous data at scale have gained in popularity. For most developers, relational databases are the default or go-to option because a table structure is easy to understand and is familiar, but there are many reasons to explore beyond relational databases. E.g. Increased need to process higher volumes, velocities, and varieties of data at a rapid rate has altered the nature of data storage needs for application developers.
NoSQL is a category of databases distinctly different from SQL databases. NoSQL is often used to refer to data management systems that are “Not SQL” or an approach to data management that includes “Not only SQL". There are a number of technologies in the NoSQL category, including document databases, key value stores, column family stores, and graph databases, which are popular with gaming, social, and IoT apps.










When to use NOSQL:
Let's imagine you're building a new social engagement site. Users can create posts and add pictures, videos and music to them. Other users can comment on the posts and give points (likes) to rate the posts. The landing page will have a feed of posts that users can share and interact with.
So how do you store this data? If you're familiar with SQL, you might start drawing something like this:




Here you see to s show the post and the associated images, audio, video, comments, points, and user info on a website or application, you'd have to perform a query with eight table joins just to retrieve the content. Now imagine a stream of posts that dynamically load and appear on the screen and you can easily predict that it's going to require thousands of queries and many joins to complete the task.

In such situation NoSQL option that simplifies the approach for this specific scenario. By using a single document like the following and storing it in DocumentDB, an Azure NoSQL document database service, you can increase performance and retrieve the whole post with one query and no joins. It's a simpler, more straightforward, and more performant result.
{
    "id":"ew12-res2-234e-544f",
    "title":"post title",
    "date":"2016-01-01",
    "body":"this is an awesome post stored on NoSQL",
    "createdBy":User,
    "images":["http://myfirstimage.png","http://mysecondimage.png"],
    "videos":[
        {"url":"http://myfirstvideo.mp4", "title":"The first video"},
        {"url":"http://mysecondvideo.mp4", "title":"The second video"}
    ],
    "audios":[
        {"url":"http://myfirstaudio.mp3", "title":"The first audio"},
        {"url":"http://mysecondaudio.mp3", "title":"The second audio"}
    ]
}
In addition, this data can be partitioned by post id allowing the data to scale out naturally and take advantage of NoSQL scale characteristics. Also NoSQL systems allow developers to loosen consistency and offer highly available apps with low-latency. Finally, this solution does not require developers to define, manage and maintain schema in the data tier allowing for rapid iteration.

Note: In case you still advocate SQL, you could use a relational solution like SQL Server to store the data and query it using joins, as SQL supports dynamic data formatted as JSON.

The following table compares the main differences between NoSQL and SQL.























Hope this helps.

Few links: http://highscalability.com/blog/2010/12/21/sql-nosql-yes.html

Arun Manglick

Thursday, November 10, 2016

Microservices (& SOA)

Service Oriented Architecture:

- Check this http://martinfowler.com/bliki/ServiceOrientedAmbiguity.html
- A way of structuring many related applications to work together, rather than trying to solve all problems in one application.
- SOA means so many different things to different people
    - For some SOA is about exposing software through web services and  expect the various WS-* standards and those that will accept any form of XML over http
    - For some SOA implies an architecture where applications disappear.
      Instead core services  supply business functionality and data separated by UI aggregators - That apply presentations that aggregate together the stuff that core services provide.
     - For some SOA is about allowing systems to communicate over some form of standard structure (usually XML based) with other applications - like CORBA
     - For some SOA is all about using (mostly) asynchronous messaging to transfer documents between different systems.
     Essentially this is EAI without all the expensive EAI (Enterprise Application Integration))vendors locking you in.                 

  - ESB (Enterprise Service Bus)
                - Actually a communication system between mutually interacting software applications in a service-oriented architecture (SOA).
     - https://en.wikipedia.org/wiki/Enterprise_service_bus
     - Primary duties of an ESB are:
          - Monitor and control routing of message exchange between services
          - Resolve contention between communicating service components
          - Control deployment and Versioning of services
          - Marshal use of redundant services
          - Cater for commodity services like
                    - Event handling,
                    - Data transformation and mapping,
                    - Message and event queuing and sequencing,
                    - Security or exception handling,
                    - Protocol conversion and
                    - Enforcing proper quality of communication service               

Understanding Microservice Architecture:

In short,
 - microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own 
   process and communicating with lightweight mechanisms, often an HTTP resource API. 
 - These services are built around business capabilities and independently deployable by fully automated deployment machinery. 
 - There is a bare minimum of centralised management of these services, which may be written in different programming languages and use different data storage technologies.

In Detail:
First,
Software built as microservices can, by definition, be broken down into multiple component services.
-   So that each of these services can be deployed, tweaked, and then redeployed independently without compromising the integrity of an application.
Its downsides, includes expensive remote calls (instead of in-process calls), coarser-grained remote APIs, and increased complexity when redistributing responsibilities between components.

Second,
- Microservices style is usually organized around business capabilities and priorities and not project
Unlike a traditional monolithic development approach — where different teams each have a specific focus on, say, UIs, databases, technology layers, or server-side logic— Microservice architecture utilizes cross-functional teams. 
The responsibilities of each team are to make specific products based on one or more individual services communicating via message bus.

Third,
Microservices act somewhat like the classical UNIX system: they receive requests, process them, and generate a response accordingly.
This is opposite to how many other products such as ESBs (Enterprise Service Buses) work, where high-tech systems for message routing, choreography, and applying business rules are utilized.  You could say that microservices have smart endpoints that process info and apply logic, and dumb pipes through which the info flows

Fourth,
Decentralized governance is favored since involvement of a variety of technologies and platforms, old-school centralized governance aren’t optimal. 
Microservice encourage developers to save time by always using code libraries established by others, while also giving them the freedom to flirt with alternative solutions when needed.
Monolithic systems use a single logical database across different applications.  In a microservice application, each service usually manages its unique database

Fifth,
Like a well-rounded child, microservices are designed to cope with failure.
Since several unique and diverse services are communicating together, it’s quite possible that a service could fail, for one reason or another.  In these instances, the client should allow its neighbouring services to function while it bows out in as graceful a manner as possible

Finally,
Microservices architecture is an evolutionary design and, again, is ideal for evolutionary systems where you can’t fully anticipate the types of devices that may one day be accessing your application.

 To sum up: Microservice architecture
  - Uses services to componentize and is usually organized around business capabilities;
  - Focuses on products instead of projects;
  - Has smart end points but not-so-smart info flow mechanisms;
  - Uses decentralized governance as well as decentralized data management;
  - Designed to accommodate service interruptions;
  - Last but not least, is an evolutionary model

Pros:
- Easily be broken down into multiple component services - I.E. Deployed, tweaked, and then redeployed independently 
- Easy integration and automatic deployment (using open-source continuous integration tools such as Jenkins, Hudson, etc.)
- Usually organized around business capabilities and priorities and not project 
- Better fault isolation: if one micro-service fails, the other will continue to work
- Can be developed by a fairly small team
- Code for different services can be written in different languages 

- Easy to scale and integrate with third-party services


Cons:
 - Partitioning the application into micro-services is very much an art
 - Additional effort into implementing communication between the services (ESB)
 - Integration and managing whole products can become complicated
 - Handling use cases that span more than one service is tough
 - Due to distributed deployment, testing can become complicated 

 - Additional complexity of a distributed system


SOA vs. Microservices
-          Some advocates reject the SOA tag altogether.
-          While others consider microservices to be simply an ideal, refined/special form of SOA.

   Note: SOA Tenents
-          Boundaries are explicit
-          Services are autonomous
-          Services share schema and contract, not class
-          Service compatibility is based on policy
               
- Microservices fulfill at least the first two tenets (with a real emphasis on the second), but it's questionable whether they fulfil the third & fourth.
   The reason the third tenet may not hold for microservices is that one of the characteristics of microservices is that they are generally exposed over a RESTful API, which, one could argue, does not expose contract and schema at all (over and above the regular HTTP verbiage).

- Few differences:
-          SOA usually has more dependent ESBs, with microservices using faster messaging mechanisms. 
-          SOA also focuses on imperative programming, whereas microservices architecture focuses on a responsive-actor programming style.
-          SOA models tend to have an outsized relational database, while microservices frequently use NoSQL or micro-SQL databases
-          Microservices is opposite to how many other products such as ESBs (Enterprise Service Buses) work, where high-tech systems for message routing,  choreography, and applying business rules are utilized.  You could say that microservices have smart endpoints that process info and apply logic, and dumb pipes through which the info flows

Well still the real difference has to do with the architecture methods used to arrive at an integrated set of services in the first place.

Hope this helps.

Reference: https://smartbear.com/learn/api-design/what-are-microservices/

Arun Manglick

















Thursday, July 28, 2016

MVC vs MVP vs MVVM





MVC –, 1979
MVP – Model-Model-View-ControllerView-Presenter, 90s
Modern MVVM – Model-View-ViewModel, 2005

General rules for when to use which?

 

MVP
  • Use in situations where binding via a datacontext is not possible.
  • Windows Forms is a perfect example of this.  
  • In order to separate the view from the model, a presenter is needed.  Since the view cannot directly bind to the presenter, information must be passed to it view an interface (IView).

MVVM
  • Use in situations where binding via a datacontext is possible.  Why?  The various IView interfaces for each view are removed which means less code to maintain.
  • Some examples where MVVM is possible include WPF and javascript projects using Knockout.
  • MVVM separates the presentation part and business logic. This leads to: 
  • Higher testability
  • Higher maintainability

MVC
  • Use in situations where the connection between the view and the rest of the program is not always available (and you can't effectively employ MVVM or MVP).
  • This clearly describes the situation where a web API is separated from the data sent to the client browsers.  
  • Microsoft's ASP.NET MVC is a great tool for managing such situations and provides a very clear MVC framework


Model-View-Presenter

<![if !supportLists]>1.       <![endif]>In MVP, the Presenter contains the UI business logic for the View.
<![if !supportLists]>2.     <![endif]>All invocations from the View delegate directly to Presenter.
<![if !supportLists]>3.     <![endif]>The Presenter is also decoupled directly from the View and talks to it through an interface. This is to allow mocking of the View in a unit test. I.e. The Presenter is not responsible for determining which View is displayed in response to any action including when the application loads, instead specific view instance is passed to the Presenter on the load of the view.
<![if !supportLists]>4.     <![endif]>Here View has a reference to Presenter (But completely decoupled from each other's and communicate to each other's by an interface) but View has no reference to Model.


public partial class Views_add_category_view : System.Web.UI.UserControl, IAddCategoryView
{
  protected void Page_Load(object sender, EventArgs e)
  {
    presenter = new AddCategoryPresenter(this);
  }
}

One common attribute of MVP is that there has to be a lot of two-way dispatching. For example, when someone clicks the "Save" button, the event handler delegates to the Presenter's "OnSave" method. Once the save is completed, the Presenter will then call back the View through its interface so that the View can display that the save has completed.

Imp: MVP tends to be a very natural pattern for achieving separated presentation in Web Forms. The reason is that the View is always created first by the ASP.NET runtime.

Two primary variations of MVP


Passive View: The View is as dumb as possible and contains almost zero logic. The Presenter is a middle man that talks to the View and the Model. The View and Model are completely shielded from one another. +
The Model may raise events, but the Presenter subscribes to them for updating the View.
Similarly View exposes setter properties which the Presenter uses to set the data in the Model.
All state is managed in the Presenter and not the View.

  • Pro: maximum testability surface; clean separation of the View and Model
  • Con: more work (for example all the setter properties) as you are doing all the data binding yourself.

Supervising Controller: The Presenter handles user gestures. The View binds to the Model directly through data binding.
In this case it's the Presenter's job to pass off the Model to the View so that it can bind to it (Like in ASP.NET MVC). The Presenter will also contain logic for gestures like pressing a button, navigation, etc.

  • Pro: by leveraging databinding the amount of code is reduced.
  • Con: there's less testable surface (because of data binding), and there's less encapsulation in the View since it talks directly to the Model.


Model-View-Controller

<![if !supportLists]>1.       <![endif]>In MVC, the Controller contains the UI business logic for the View.
<![if !supportLists]>2.    <![endif]>All action in the View correlates with a call to a Controller along with an action.
<![if !supportLists]>3.     <![endif]>In the MVC, Controller is tightly coupled to the View. I.e. Controller is responsible for determining which View is displayed in response to any action including when the application loads. This differs from MVP where actions route through the View to the Presenter. Also in MVC, every action in the View correlates with a call to a Controller along with an action. In the web each action involves a call to a URL on the other side of which there is a Controller who responds. Once that Controller has completed its processing, it will return the correct View. The sequence continues in that manner throughout the life of the application:

    Action in the View
        -> Call to Controller
        -> Controller Logic
        -> Controller returns the View.

One other big difference In implementations of MVC with MVP, the View usually will not have any logic in the code behind.
This is contrary to MVP where it is absolutely necessary as if the View always has delegation code to the Presenter.

Another big difference - Unlike view and controller (In MVC), view and presenter (In MVP) are completely decoupled from each other's and communicate to each other's by an interface

Today, this pattern is used by many popular framework like as Ruby on Rails, Spring Framework, Apple iOS Development and ASP.NET MVC.

Presentation Model / Model-View-ViewModel.

One other pattern to look at is the Presentation Model pattern. In this pattern there is no Presenter. Instead the View binds directly to a Presentation Model. The Presentation Model is a Model crafted specifically for the View. This means this Model can expose properties that one would never put on a domain model as it would be a violation of separation-of-concerns. In this case, the Presentation Model binds to the domain model, and may subscribe to events coming from that Model. The View then subscribes to events coming from the Presentation Model and updates itself accordingly. The Presentation Model can expose commands which the view uses for invoking actions. The advantage of this approach is that you can essentially remove the code-behind altogether as the PM completely encapsulates all of the behaviour for the view. This pattern is a very strong candidate for use in WPF applications and is also called Model-View-ViewModel.


Reference:


Hope this helps.

Thanks & Regards,
Arun Manglick,