Wednesday, September 1, 2021

Performance optimization and load balancing

 

What is Scalability? 

Scalability is the ability of a system, network, or process to handle a growing amount of load by adding more resources. The adding of resource can be done in two ways 

  • Scaling Up 

This involves adding more resources to the existing nodes. For example, adding more RAM, Storage or processing power. 

  • Scaling Out 

This involves adding more nodes to support more users. 

Any of the approaches can be used for scaling up/out an application, however the cost of adding resources (per user) may change as the volume increases. If we add resources to the system It should increase the ability of application to take more load in a proportional manner of added resources. 
 
An ideal application should be able to serve high level of load in less resources. However, in practical, linearly scalable system may be the best option achievable.  
 
Poorly designed applications may have really high cost on scaling up/out since it will require more resources/user as the load increases. 


What is a Cluster? 

A cluster is group of computer machines that can individually run a software. Clusters are typically utilized to achieve high availability for a server software.  
 
Clustering is used in many types of servers for high availability. 

  • App Server Cluster 

An app server cluster is group of machines that can run a application server that can be reliably utilized with a minimum of down-time. 

  • Database Server Cluster 

A database server cluster is group of machines that can run a database server that can be reliably utilized with a minimum of down-time. 


Why do you need Clustering? 

Clustering is needed for achieving high availability for a server software. The main purpose of clustering is to achieve 100% availability or a zero down time in service.  
 
A typical server software can be running on one computer machine and it can serve as long as there is no hardware failure or some other failure. 
 
By creating a cluster of more than one machine, we can reduce the chances of our service going un-available in case one of the machine fails. 
 
Doing clustering does not always guarantee that service will be 100% available since there can still be a chance that all the machine in a cluster fail at the same time. However it in not very likely in case you have many machines and they are located at different location or supported by their own resources. 


What is MiddleTier Clustering? 

Middle tier clustering is just a cluster that is used for service the middle tier in an application. This is popular since many clients may be using middle tier and a lot of heavy load may also be served by middle tier that requires it be to highly available. 
 
Failure of middle tier can cause multiple clients and systems to fail, therefore it's one of the approaches to do clustering at the middle tier of an application. 
 
In java world, it is really common to have EJB server clusters that are used by many clients. In general any application that has a business logic that can be shared across multiple clients can use a middle tier cluster for high availability. 


What is Load Balancing? 

Load balancing is simple technique for distributing workloads across multiple machines or clusters.  
The most common and simple load balancing algorithm is Round Robin. In this type of load balancing the request is divided in circular order ensuring all machines get equal number of requests and no single machine is overloaded or underloaded 
 
The Purpose of load balancing is to 

  • Optimize resource usage (Avoid overload and under-load of any machines.) 

  • Achieve Maximum Throughput 

  • Minimize response time 

Most common load balancing techniques in web based applications are 

  1. Round robin 

  1. Session affinity or sticky session 

  1. IP Address affinity 

What is Session replication? 

Session replication is used in application server clusters to achieve session failover. A user session is replicated to other machines of a cluster, every time the session data changes. If a machine fails, the load balancer can simply send incoming requests to another server in the cluster.  The user can be sent to any server in the cluster since all machines in a cluster have copy of the session. 
 
Session replication may allow your application to have session failover but it may require you to have extra cost in terms of memory and network bandwidth. 


What is Sticky Session (session Affinity) Load Balancing? What do you mean by 'session Affinity'? 

Sticky session or a session affinity technique another popular load balancing technique that requires a user session to be always served by an allocated machine.  

Why Sticky Session? 

In a load balanced server application where user information is stored in session it will be required to keep the session data available to all machines. This can be avoided by always serving a particular user session request from one machine. 

How It Is Done? 

The machine is associated with a session as soon as the session is created. All the requests in a particular session are always redirected to the associated machine. This ensures the user data is only at one machine and load is also shared. 
 
In Java world, this is typically done by using jsessionid cookie. The cookie is sent to the client for the first request and every subsequent request by client must be containing that same cookie to identify the session. 

What Are The Issues With Sticky Session? 

There are few issues that you may face with this approach 

  • The client browser may not support cookies, and your load balancer will not be able to identify if a request belongs to a session. This may cause strange behavior for the users who use no cookie based browsers. 

  • In case one of the machine fails or goes down, the user information (served by that machine) will be lost and there will be no way to recover user session. 


What is IP Address Affinity technique for Load Balancing? 

IP address affinity is another popular way to do load balancing. In this approach, the client IP address is associated with a server node. All requests from a client IP address are served by one server node. 
 
This approach can be really easy to implement since IP address is always available in a HTTP request header and no additional settings need to be performed. 
 
This type of load balancing can be useful if you clients are likely to have disabled cookies. 
 
However there is a down side of this approach. If many of your users are behind a NATed IP address then all of them will end up using the same server node. This may cause uneven load on your server nodes. 
 
NATed IP address is really common, in fact anytime you are browsing from office network it's likely that you and all your coworkers are using same NATed IP address. 


What is Fail Over? 

Fail over means switching to another server when one of the server fails.  
 
Fail over is an important technique in achieving high availability. Typically a load balancer is configured to fail over to another machine when the main machine fails.  
 
To achieve least down time, most load balancer support a feature of heart beat check. This ensures that target machine is responding. As soon as a hear beat signal fails, load balancer stops sending request to that machine and redirects to other machines or cluster.

Web Distributed Systems Design


Principles of Web Distributed Systems Design 

  • Availability: The uptime of a website is absolutely critical to the reputation and functionality of many companies. For some of the larger online retail sites, being unavailable for even minutes can result in thousands or millions of dollars in lost revenue, so designing their systems to be constantly available and resilient to failure is both a fundamental business and a technology requirement. High availability in distributed systems requires the careful consideration of redundancy for key components, rapid recovery in the event of partial system failures, and graceful degradation when problems occur. 

  • Performance: Website performance has become an important consideration for most sites. The speed of a website affects usage and user satisfaction, as well as search engine rankings, a factor that directly correlates to revenue and retention. As a result, creating a system that is optimized for fast responses and low latency is key. 

  • Reliability: A system needs to be reliable, such that a request for data will consistently return the same data. In the event the data changes or is updated, then that same request should return the new data. Users need to know that if something is written to the system, or stored, it will persist and can be relied on to be in place for future retrieval. 

  • Scalability: When it comes to any large distributed system, size is just one aspect of scale that needs to be considered. Just as important is the effort required to increase capacity to handle greater amounts of load, commonly referred to as the scalability of the system. Scalability can refer to many different parameters of the system: how much additional traffic can it handle, how easy is it to add more storage capacity, or even how many more transactions can be processed. 

  • Manageability: Designing a system that is easy to operate is another important consideration. The manageability of the system equates to the scalability of operations: maintenance and updates. Things to consider for manageability are the ease of diagnosing and understanding problems when they occur, ease of making updates or modifications, and how simple the system is to operate. (i.e., does it routinely operate without failure or exceptions?) 

  • Cost: Cost is an important factor. This obviously can include hardware and software costs, but it is also important to consider other facets needed to deploy and maintain the system. The amount of developer time the system takes to build, the amount of operational effort required to run the system, and even the amount of training required should all be considered. Cost is the total cost of ownership. 


Monday, May 1, 2017

Designing Software Architecture basics



How an architecture is prepared


How does the Architecture world works or move 
[from Open Group]



Desired Architecture for today’s world



Architectural Patterns and Styles
An architectural style, sometimes called an architectural pattern, is a set of principles—a coarse grained pattern that provides an abstract framework for a family of systems. An architectural style improves partitioning and promotes design reuse by providing solutions to frequently recurring problems. You can think of architecture styles and patterns as sets of principles that shape an application.
An understanding of architectural styles provides several benefits. The most important benefit is that they provide a common language. They also provide opportunities for conversations that are technology agnostic. This facilitates a higher level of conversation that is inclusive of patterns and principles, without getting into specifics. For example, by using architecture styles, you can talk about client/server versus n-tier. Architectural styles can be organized by their key focus area. The following table lists the major areas of focus and the corresponding architectural styles.
Combining Architectural Styles
The architecture of a software system is almost never limited to a single architectural style, but is often a combination of architectural styles that make up the complete system. For example, you might have a SOA design composed of services developed using a layered architecture approach and an object-oriented architecture style.
A combination of architecture styles is also useful if you are building a public facing Web application, where you can achieve effective separation of concerns by using the layered architecture style. This will separate your presentation logic from your business logic and your data access logic. Your organization's security requirements might force you to deploy the application using either the 3-tier deployment approach, or a deployment of more than three tiers. The presentation tier may be deployed to the perimeter network, which sits between an organization's internal network and an external network. On your presentation tier, you may decide to use a separated presentation pattern (a type of layered design style), such as Model-View-Controller (MVC), for your interaction model. You might also choose a SOA architecture style, and implement message-based communication, between your Web server and application server.
If you are building a desktop application, you may have a client that sends requests to a program on the server. In this case, you might deploy the client and server using the client/server architecture style, and use the component-based architecture style to decompose the design further into independent components that expose the appropriate communication interfaces. Using the object-oriented design approach for these components will improve reuse, testability, and flexibility.
Many factors will influence the architectural styles you choose. These factors include the capacity of your organization for design and implementation; the capabilities and experience of your developers; and your infrastructure and organizational constraints. The following sections will help you to determine the appropriate styles for your applications.

Maintain Multi Session on Multiple Tabs of browser


How do you maintain multiple sessions on different tabs of browser.


Yes this is an interesting topic.

I knew it was easy if we were using cookieless session, but ours is a cookie enabled session.

I was struggling all the day and then I finally got a hint that we can use Web.config to handle above issue.

Wondering how?

 <sessionState cookieless="UseUri" regenerateExpiredSessionId="true">
  </sessionState>



ASP.Net MVC in a flash for Experienced


Request is received by Routing table which creates RouteData Object [RouteCollection] and passes it to IRouteHandler.
IRouteHandler decides which handler to be invoked to handle the request, here in this case it would be MVC Handler.
MVC Handler looks at the url and passes the controller name to IController Factory which checks the controller presence and returns Controller instance to Handler. Now the Handler invokes the execute method of the returned Controller instance and passes the request to ActionSelector.
Action Selector has two ways to handle the action
  1. ActionName selector
  2. ActionMethod selectors
Action selector resolves the Action and request is passed to IActionInvoker. By default Action selector loads all the methods of the class and based on URL it calls the action method.
IActionInvoker calls the IActionFilter’s which runs in below sequence
  1. IAuthentication filter
  2. IAuthorization filter
  3. IAction filter
  4. IException filter
  5. IResult filter
The IAction filter makes use of IModelBinder which in turn takes help of ValueProvider to resolve the action parameter value(s) and the action method gets executed and the result is passed to IViewEngine.
If Result is View or PartialView then IViewEngine loads the
  • WebForm View Engine(.aspx)
  • Razor View Engine (.cshtml)

MVC does not support method overloading for Action methods, we can achieve this by using Action Name or by using Attribute [HTTPPost/ HTTPGet]
IModelBinder does 3 tasks
  1. Resolves value
  2. Conversion
  3. Validates
It takes help of Value Provider to resolve/ get actual value.
ValueProvider
| ChildActionValueProvider
| FormValueProvider
| JSONValueProvider
| RouteDataValueProvider
| QueryStringValueProvider
| HttpFileCollectionValueProvider
|JQueryFormValueProvider
We can create Custom ValueProvider if required. Eg:- To read from Cookie we can extend ValueProvider.

To post a form we have 3 ways in MVC.
  1. Using parameter with button name in action
  2. Use hidden field set value before post
  3. Use Action attribute of the button in HTML5 which would change URL in browser.
When we add layout page to a View bootstrap gets included in the project automatically.
Partial Views are used to send partial HTML & avoid layout settings.
There are three ways in which Partial View can be included in a page.
  1. @RenderPage 
  1. is faster because it writes to Response.
  2. Does not use lookup method
  3. Is used for static content
  1. @HTML.Partial - Used with strongly typed views.
  1. @HTML.RenderPartial   - Supports both strongly typed & non-static. Best suited for Ajaxified pages.

@Section are to be used for local style/ css or something page specific, these sections are declared in layout and in Views we can use these sections to embed Style/ css/ js files.
Browsers have limitation to download 3 files at a time.
The JSONResult action returns Microsoft JSON format, this JSON cannot be used with JQuery directly. JQuery understands NewtonSoft JSON, so this has to be converted to NewtonSoft JSON.

Ajax Helpers
Four steps should be followed to use Ajax helpers
  1. Install unobtrusive javascript
  2. Use it as Ajax.ActionLink
  3. Provider the Ajax options to method
  4. Include validate.js & unobtrusive.jquery.validation.js
To get the validation work
  1. Set config setting unobtrusive & client validation to true.
  2. Apply Data annotation attribute on properties.
  3. Only use strongly typed helpers or template helpers to generate UI.
  4. Refer jquery Validate & unobtrusive js files on page
  5. ModelState to be checked before operation.
Customising Routes
We customise routes only when URL parameters must be altered.
  1. Change parameter name [id to something(PId)].
  2. Change parameter order
  3. Update no. of parameters
  4. Change URL value to controller name, a kind of mapping to avoid the end client know the controller name and action name.
We can add constraints to route which would validate the value type using Regex.

Declarative Helpers
This feature helps us to avoid creating partial view for displaying content which have less text. For eg:- If we have to display DateTime on each page we can create cshtml and keep it inside the App_Code folder which gets complied to a class and at runtime is available to Views.
Cshtml would look like
@helper HelloWorld(string message)
{
<h1>Welcome @message</h1>
<h2>@DateTime.ToString() </h2>
}

In the Views we include Declarative helpers as @<FileName>. <METHOD_NAME>

Wednesday, March 2, 2016

Design Patterns for Agile world


Design patterns with Design principle for Agile world


What are Design patterns?
Design patterns are solutions to real world problems that pop up time and again, so instead of reinventing the wheel, we follow the design patterns that are well-proven, tested by others, follow as safe guidelines.

What are Software Design principles?
Software design principles represent a set of guidelines that helps us to avoid having a bad design. The design principles are associated to Robert Martin who gathered them in "Agile Software Development: Principles, Patterns, and Practices". According to Robert Martin there are 3 important characteristics of a bad design that should be avoided:

Rigidity - It is hard to change because every change affects too many other parts of the system.
Fragility - When you make a change, unexpected parts of the system break.
Immobility - It is hard to reuse in another application because it cannot be disentangled from the current application.

Design principles are important when going with Agile development where requirements keep changing, dependency on different modules, lack of technical expertise, etc.

This happens in our day to day life and all of us might have faced above mentioned problems. 

How to handle these problems

We have to stick to Design principles in all the phases of SDLC (software development life cycle), we have to follow the software principles but need to take a special consideration during analysis, design and coding phases. When requirement changes, we need to deliberately analyze the code and make sure basic software principle shouldn't be violated.

Design principles followed by many :
The Single Responsibilities Principle (SRP)
Open and Close Principle (OCP)
Liskov Substitution Principle (LSP)
Interface Segregation Principles (ISP)
Dependency Inversion Principle (DIP)
[Above 5 principle are SOLID]
DRY (Don't repeat yourself)
YAGNI (You aren't gonna need it)

Note: If you see any corrections or have suggestions please do let me know.

Tuesday, December 29, 2015

Database Server administration concepts for Architects/ Tech Leads/ Database developers

Most of us must have heard of Database replication/ mirroring but few of us would have tried to explore the details. This article explains about these concepts which would give you an overall understanding of Database server concepts which are used for large applications.


What is Database Mirroring?
Database mirroring is a primarily software solution for increasing database availability.
It maintains two copies of a single database that must reside on different server instances of SQL Server Database Engine.
What is Database Replication?
It is a set of technology for copying and distributing data and database objects from one database to another and then synchronizing between databases to maintain consistency. Using replication, you can distribute data to different locations and to remote or mobile users over local and wide area networks, dial-up connections, wireless connections, and the Internet.
Most of the development community think that above two mentioned questions are enough for database backups/ availability. There is yet another way which is most popular among the Database administrator's which is "Log Shipping".
So what is Log shipping?
It is sending transaction log backups from one database (Primary database) to another (Secondary database) on another server. An optional third server known as Monitor server records the history and status of the backups and restore operations. 
Server limitations for above mentioned techniques
Log Shipping --> It can be configured as One to Many. i.e one primary server and many                                secondary servers. 
                        Or
                        Secondary server can contain multiple Primary databases that are log                               shipped from multiple servers.
Mirroring      --> It's one to one. i.e., One principal server to one mirror server.
Replication  -->
  • Central publisher/distributor, multiple subscribers.
  • Central Distributor, multiple publishers, multiple subscribers.
  • Central Distributer, multiple publishers, single subscriber.
  • Mixed Topology.

Backup/ Restoration
Log Shipping -->This can be done manually or through Log Shipping options.
Mirroring      --> User take backup & restore manually.
Replication  --> User creates an empty database with the same name.



Note: If you see any corrections or have suggestions please do let me know.

Generative AI: Paving the way for Performance-Driven Enterprise Architecture

  Generative AI is not just reshaping the technological frontier; it's rapidly becoming an essential tool in optimizing enterprise archi...