Cloud-Native Design with 12 Factor App

There are many guidelines available for building a cloud-native application. One of the Industry accepted set of guidelines is 12 factors suggested on https://12factor.net/

In nutshell, here are the 12 factors to help your application build in a cloud native manner.

Codebase: One codebase tracked in revision control, many deploys
Dependencies: Explicitly declare and isolate dependencies
Config: Store config in the environment
Backing services: Treat backing services as attached resources
Build, release, run: Strictly separate build and run stages
Processes: Execute the app as one or more stateless processes
Port binding: Export services via port binding
Concurrency: Scale-out via the process model
Disposability: Maximize robustness with fast startup and graceful shutdown
Dev/prod parity: Keep development, staging, and production as similar as possible
Logs: Treat logs as event streams
Admin processes: Run admin/management tasks as one-off processes

A good tutorial on practical usage of these 12 factors in your application

WebSockets

A normal flow between client and server over HTTP connection is made of Requests and Responses.

HTTP communication

The client sends a response to the server and then the server sends back the response. Now there can be use cases where the server would have to send data to the client, for example, a chat application or a stock market tracker. In such scenarios, where the server needs to send data to the client at will, WebSocket communication protocol can solve the problem.

WebSocket provides full-duplex communication channels over a single TCP connection. Both HTTP and Websocket protocols are located at layer 7 in the OSI model and depend on TCP at layer 4.

Websocket Connections string looks like ws://some.example.com or for secured wss://some.example.com

To achieve the communication, the WebSocket handshake uses the HTTP Upgrade header to change from the HTTP protocol to the WebSocket protocol.

WebSocket handshake

  • The client sends a request for “upgrade”  as GET 1.1 upgrade
  • The server responds with 101- Switching protocols 

Once the connection is established, the communication is duplex, and both client and server and sent messages over the established connection.

Challenges with Websockets

  • Proxy is difficult at Layer 7. It will mean 2 levels of WebSocket connectivity, client to proxy and then proxy to backend.
  • Layer 7 load balancing is difficult due to the stateful nature of communication.
  • Scaling is tough due to its stateful nature. Moving the connection from one installed backend to another would mean resetting the connection.

Disclaimer: This post was originally posted by me in the cloud community –https://cloudyforsure.com/networking/websockets/

HTTP 1 vs 2 vs 3

For years, the Internet is powered by HTTP protocol helping millions of websites and applications deliver content. Let’s take a look at the journey of the HTTP protocol, its past, present, and future.

HTTP 1

The current version of the HTTP 1 protocol is actually HTTP 1.1. But let’s start with HTTP 1, which was a simple request-response protocol.

HTTP 1 flow

HTTP 1.1

As we can see in HTTP 1 implementation, one major problem was that connection needed to be established after each request. To solve this problem HTTP 1.1 came up with a keep-alive concept which helped to send multiple requests over a single connection. To achieve the speed, HTTP1.1 had 6 TCP connections behind the scenes instead of 1.

HTTP 2

Though HTTP 1.1 was much faster than HTTP 1, it had some problems, most importantly, it was not making use of TCP connection completely. Each connection was sending one request at a time. This problem was solved in HTTP 2 and multiple concurrent requests could be sent.

HTTP 2

To achieve this parallel request over a single HTTP connection, HTTP 2 uses the concept of streams. That is, each request being sent from the client has a unique stream id attached behind the scenes. This helps the client and server identify the calling and receiving endpoints. One can think of each stream as an independent channel for communication.

HTTP 3

One problem with HTTP 2 is that the streams we have defined are at the HTTP level. TCP is not aware of the concept and is just sending packets at a lower layer. So if there are 4 independent requests sent using 4 different streams, and even if a single packet for any of the requests is lost in the communication, the backend server will keep waiting for the packet and all 4 requests will wait.

HTTP 3 plans to solve this problem by implementing HTTP over QUIC instead of TCP. QUIC too has the concept of streams inbuilt, so in the above-mentioned scenario, when one packet is lost for one request out of 4, only one stream is impacted and response for the other 3 requests will be successfully served.

Disclaimer: This post was originally posted by me in the cloud community –https://cloudyforsure.com/networking/http-1-vs-2-vs-3/

Corporate Finance: Value Maximization

Value Pentagon

image source: https://soni-sourabh.blogspot.com/2013/10/value-pentagon-shareholder-value.html

Company as-is value is the value of the company without any restructuring or change.

The company’s optimum value is the value that can be achieved after the restructuring is done.

Internal restructuring: Find out redenancies, wastages, remove bottlenecks.

External Restructuring: Merger, demerger, acquisition, etc.

Financial restructuring: Writing down useless assets, debt restructuring etc.

Shareholder vs Stakeholder value

When we talk about the value of the company, they normally have two approaches, increase shareholder value or stakeholder value. Both approaches have their pros and cons. The shareholder value approach is easy to track, as you can look at the numbers and figure out if the shareholder value has increased. But at the same time, this approach can be myopic and focus on short-term goals.

The stakeholder value approach has a broader view, where it talks about customers, employees, society, shareholders, and other stakeholders. The problem here it is hard to track as there is no direct way to track it. For example, giving better discounts and better salaries might help me keep my customers and employees happy, but might add to losses.

Measuring shareholder wealth creation

Market Value Addition or MVA is an important aspect to understand shareholder value. For example, there are two companies, A and B, both with a market cap of say 1000 crore. But the network of company A is 500 crore and company B is 250 crore. We can see MVA for company A is 500 crore whereas company B is 250 crores. In other words, the market view potential for growth in company B.

Corporate Restructuring

Corporate restructuring includes acquisitions, demergers, joint ventures, etc. For example, buying Corus helped Tata steel to jump from 55th ranked in steel revenue worldwide to 5th rank.

Corprate restructuring can be done by

Expansion: Absorption, Tender Offer, Asset acquisition, Joint venture, etc.

Contraction: Demerger – Spin off, split off, split up, Equity carve out etc.

Corporate Control: Going private, Equity buyback, leveraged buyout, etc.

Corporates can unlock value by demergers. Studies report that the observed value of the diversified firm is, on average, 15 percent less than the sum of the implied market value of its divisions, as compared to stand-alone market values of single-segment firms in those industries.

Factors behind diversification discount

Information hypothesis: the inability of markets to correctly evaluate conglomerate structures with unrelated businesses, leading to possible undervaluation.

Inefficient Management hypothesis: the inability of the managers to efficiently manage unrelated businesses.

Inefficient investment hypothesis: distortion of investment due to competition among units for resources.

Modes of asset disposition

Slump sale:  Slump sale means the transfer of one or more undertakings as a result of the sale for a lump sum consideration. For example, Ruchi Soya buying biscuit business from Patanjali Natural Biscuits Pvt Ltd (PNBPL) for 60 crores.

Spin-Off: A spinoff is the creation of an independent company through the sale or distribution of new shares of an existing business or division of a parent company. When a new company B is carved out of company A, mostly shareholders of company A will get some proportional shares of company B.

Spin-Off helps in

  • Unlocking hidden value: establish a public market valuation for undervalued assets.
  • Undiversification: divest non-core business and sharpen strategic focus
  • Institutional sponsorship: Promote equity research coverage
  • Public currency: the public currency for acquisition and stock-based compensation programs
  • Motivating Management
  • Eliminating dis-synergies
  • Corporate Defence: Divest “crown jewel” asset to make the takeover of parent company less attractive.

Challenges in spin-off: There are certain aspects that need to be managed, for example, if the parent company has debt, how this debt will be divided between parent and spin-off company. The lenders need to agree on the arrangement.

Split-Off: In a split-off, the parent company offers its shareholders the opportunity to exchange their parent-co shares. For example, a big shareholder can give up shares in the parent company to gain controlling stakes in the new company.

Split-up: Division of a company into two or more publically traded companies. The difference here is that instead of the parent company and spun-off company, we have completely new companies into existence.

Equity Carve-out: Also known as IPO carve-out, the parent company sells a portion or all of its interests in a subsidiary to the public in an initial public offering.

Financial Restructuring

Cleaning up a balance sheet: writing off losses, writing down useless assets, can be done through asset restructuring and recapitalization.

Debt Restructuring

  • Strategy-Driven: Restructure debt by lowering the interest rates.
  • Crisis-Driven: When a company defaults, the company is forced to restructure debt.

Equity Restructuring

  • Special dividend: One-time dividend
  • Share buyback: reduces the shareholder base. As a regulatory requirement, the debt-equity ratio should be 2:1, after the buyback. Buyback can happen through the open markets, tender offers, and buyback from employees.
  • Stock Splits: helps with liquidity
  • Bonus Shares: When the company is growing fast but does not want to distribute cash in form of a dividend, a bonus share will help reward the shareholders.

Human Resource Management

Any system has the following core features – Inputs, Processes & Procedures, Output, and Feedback. When we think of HRM systems, we can look at these features as

  • Inputs- People with their Knowledge, Skills, Abilities, and personalities.
  • HRM Processes, Procedures, and Policies
  • Outcomes- Organizational Perspective and Employee Perspective
  • Feedback – Internal or External

Core objectives of any HRM system from Org side

  • Productivity or Performance (Ability * Motivation* Opportunity)
  • Job Satisfaction
  • Motivation or Engagement
  • Low Attrition

Objectives from Employee side

  • Employee Contract: When an employee joins a company, there is a formal contract between employee and company. It is HR’s responsibility to make sure terms are fulfilled (leaves, medical benefits, etc)
  • Psychological Contract: A more important aspect from the employee’s side is a psychological contract which is unwritten, for example, the firm will help employees to learn and grow.

HR system has following processes

  • Recruitment and Selection
  • Orientation
  • Performance Management -> Compensation (Increments & Incentives), Training and development
  • Exit Processes

HR Environment impacting policies

External

  • Economic
  • Product Market
  • Labor Market
  • Government regulations
  • Social environment

Internal

  • Organization Culture
  • Business strategy
  • Org Size
  • Leadership
  • Technology
  • Lifecycle stage

Any organization goes through various lifecycle stages like startup -> Growing -> Mature -> Decline. HR policy will be impacted by the current stage of the organization. For example, an org in the startup phase will have different policies to attract organizations like profit-sharing in terms of ESOPs.

HR Strategies

Innovative Strategy: Firms need employees to be innovative, risk-taking, develop new skills, and exchange ideas. Firms allow employees to become stockholders by providing stock options as part of pay.

Quality Enhancement Strategy: Firms that are looking to gain a competitive advantage by improving the quality of products and production. Mostly comes with a fixed job description but employees need to be flexible and adaptable to new technologies. Performance appraisals are mostly short-term and result-oriented.

Cost reduction strategy: Firms with relatively fixed and explicit job descriptions try to gain a competitive advantage by cost reduction strategy. Narrowly designed career paths encourage specializations, expertise, and efficiency. Appraisals are short-term and result-oriented.

Different organizations are looking for different aspects in an employee. For example, a startup would look for employees

  • Risk-taking
  • Ready to experiment
  • Tolerance for failure- fail fast and learn
  • Entrepreneurial
  • Problem solver
  • Handle ambiguity

Innovation = AMO (Ability * Motivation * Opportunity)

Land, Labor, and Climate- Nonmarket forces

When we talk about nonmarket strategy, there are many factors that impact a firm. Here we will talk about the three most important aspects that a firm needs to be thinking about are – land, labor, and climate. We already talked about the climate in the last post. We will discuss more labor and land here.

Volkswagen Emission Scandal: An important case to understand the impact of non-market forces on a company is the case of Volkswagen which came into light in 2014-15. The company used a defeat device to cheat the emission tests giving false readings while the cars were being tested for emission against CCA emission standards. Volkswagen had ambitious revenue and sales goals, but at the same time, it suffered from high labor and manufacturing cost due to the way decision-making power had a big role in labor representations. Once the scandal was highlighted, Volkswagon came up with a strong nonmarket strategy under the new CEO where they invested heavily in future-oriented electric cars.

Land: Land is an important input for any business, to open factories, offices, storage units, etc. any firm needs land. Along with being important, the land is also a very complicated input to attain. Most land is owned by private owners or households. Obtaining a big chunk of land in a developing country like India which has a high population density can be a big challenge. The difficulty factor will vary based on factors like population density, type of land (land currently used for agriculture or residential purpose), connectivity (factories want easy access to roads, airports, shipping ports, etc.), and so on.

An interesting case study for land acquisition in India is the Tata motors Singur case in West Bengal, where the organization failed to set up the factory due to opposition from landowners. Without getting into political aspects, we will look at the Land acquisition act LARR which was the result of the Singur case.

image source: https://www.downtoearth.org.in/news/agriculture/-state-govts-acquire-land-by-subverting-rights-and-bending-the-law–62463

Rather than a forceful acquisition of land, consent-based ownership transferred needs to be in place. One solution is to go for an auction-based option given to landowners, where every owner can give their expected value of the land. The firms can choose the lowest bidders and an option of giving an alternate land can be given to people who are not ready to give up land for money. Another option that is in need of the hour is to rather than going horizontally, firms need to think more of going vertically when setting up new factories to help optimized usage of land. Also, profit-sharing options should be given to landowners.

Labor Laws: Labor is a necessary requirement for any kind of business. Any firm looking to set up a business at any location needs cost-effective skilled labor. A very simple calculation is how much investment of X per hour in labor is yielding in terms of outcome. Labor laws in any country will give directions for the discharge or dismissal of workers, wages, bonuses, lay-offs, retrenchment, and work conditions. For example, minimum wages laws help ensure that firms are paying a basic minimum wage so that workers can live a decent lifestyle.

Climate Change and Sustainable Value Framework

Climate change is an important factor in recent times which has been talked about in business and non-business environments. Every degree rise in temperature is going to have a long-lasting impact on our planet. Every business is a social entity, hence has a responsibility of making sure it is looking at growth keeping future generations in mind.

Projected Impacts of Climate Change
image source: https://public.wmo.int/en/resources/bulletin/water-security-changing-climate

Sustainable development meets the needs of the present without compromising the ability of future generations to meet their own needs.

To contain global warming to a 1.5 degrees C rise, global net human-caused emission of carbon dioxide CO2 would need to fall by 45 percent from 2010 levels by 2030, reaching net zero around 2050.

Firms need to change the way they conduct business over next 2-3 years.

Business Megatrends and Sustainability

Business Megatrend refers to societal and economic shifts such as globalization, the rise of the information society, and so on.

Current Megatrends: Rapid Urbanization, climate change, and resource scarcity, shift in global economic power, demographic and social changes, technical breakthroughs, etc.

Sustainability is an important megatrend. GM’s decline can be clearly traced to its failure to understand how quality considerations would transform the auto industry. Similarly, Kodak’s dominant position in photography eroded quickly as it missed the signals in digital technologies.

The interconnection of the elements of the Triple Bottom Line concept.
image source: https://www.researchgate.net/figure/The-interconnection-of-the-elements-of-the-Triple-Bottom-Line-concept_fig1_329185478

A sustainable business model is the one when a firm can keep a balance between the three factors – Profit, People, and Planet.

A firm cannot just focus on today’s profit but also need to think of tomorrow’s growth and sustainability.

Slide2
image source: https://leadingforwellness.typepad.com/betterfutures/2010/12/leading-the-way-relfections.html

For sustainable growth, a firm needs to do well in all four quadrants. Firms need to reduce material consumption, reduce pollution, reduce waste generation. This can be achieved by a greater level of transparency and responsiveness

Some of the strategies including adopting green technologies and work towards inclusive wealth creation and distribution (reduce poverty), will help firms move towards sustainable growth.

The Sustainable Value Framework  
image source: https://www.researchgate.net/figure/The-Sustainable-Value-Framework_fig2_267154244

GraphQL- Apollo Federation

So far we have talked about the basics of GraphQL and how a simple client-server architecture will work. But when we talk about enterprise-level, we often deal with multiple backend services fulfilling different purposes. A federated GraphQL helps us implement GraphQL in a manner that queries can be executed on multiple backend services. Apollo federation is a set of tools to help us compose multiple GraphQL schemas declaratively into a single data graph.

image source- https://www.apollographql.com/docs/federation/

The image above how Apollo helps club data from different services to be served from a single gateway.

Before getting into details of Federation, let’s take a look at libraries provided by Apollo

  • @apollo/federation provides primitives that your implementing services use to make their individual GraphQL schemas composable.
  • @apollo/gateway enables you to set up an instance of Apollo Server as a gateway that distributes incoming GraphQL operations across one or more implementing services.

And of course, you need Apollo Server for gateway and each of the implementing services we have.

Let us understand some core concepts here

Entities

“In Apollo Federation, an entity is an object type that you define canonically in one implementing service and can then reference and extend in other implementing services. Entities are the core building block of a federated graph.”

Any object can be declared as an entity by adding @key, which defines the primary key for the entity.

type Product @key(fields: "upc") {  upc: String!
  name: String!
  price: Int
}

An entity defined in a service can then be referenced in another service.

type Review {
  product: Product
}

# This is a "stub" of the Product entity (see below)
extend type Product @key(fields: "upc") {
  upc: String! @external
}
  • Note that the “extend” keyword highlights that the entity is implemented somewhere else.
  • The @key directive indicates that Product uses the upc field as its primary key
  • The upc field must be included in the stub because it is part of the specified @key. It also must be annotated with the @external

Resolving

Now the review service needs to have a resolver for product.

{
  Review: {
    product(review) {
      return { __typename: "Product", upc: review.upc };
    }
  }
}

Resolver in review returns representation of product entity. A representation requires only an explicit __typename definition and values for the entity’s primary key fields.

Product service need to define a reference resolver

{
  Product: {
    __resolveReference(reference) {
      return fetchProductByUPC(reference.upc);
    }
  }
}

Extending

While referencing the entity from other services, this service can add fields to the entity. The original service need not be aware of added fields.

extend type Product @key(fields: "upc") {
  upc: String! @external
  reviews: [Review]
}

Whenever a service extends an entity with a new field, it is also responsible for resolving the field.

{
  Product: {
    reviews(product) {
      return fetchReviewsForProduct(product.upc);
    }
  }
}

Further readings: https://www.apollographql.com/docs/federation/

Disclaimer: This post was originally posted by me in the cloud community – https://cloudyforsure.com/graphql/graphql-apollo-federation/

Decision Making Under Certainty

There can be cases where we have all the needed data is available, and we need to make decisions such that a given objective is achieved in the best possible manner while satisfying conditions imposed.

To understand the concept let’s take the problem of optimizing resource utilization and maximizing profit, where we have all the details on how much resources are being used by the products. Say in a factory, we are building 2 products, Product A and B. The Factory has 4 units. Product A generates 30000 in profit and manufacturing needs 1 hr in unit 1, 2 hr in unit 2, and 2 hr in unit 3. For product-B, it generates 50000 in profit and its manufacturing needs 2 hr in unit 1, 2 hr in unit 2, and 3 hrs in unit 4. As given constraints, we know that unit 1 can operate 4000 hrs, unit 2 can operate 6000 hrs, unit 3 can operate 5000 hrs and unit 4 can operate 4500 hrs in a month.

To solve this problem, we are going to use the Simplex Linear Programming method. This is available off the shelf in Microsoft Excel, so we will set up the data in an excel sheet.

Simplex LP

Let’s try to understand the data here before moving ahead. We have added data for Product A and B, Profit data for per unit, units manufactures is just a placeholder for now, and then we have given the number of hours spent in each unit by both the products.

Column E2 has total profit, i.e. number of units for product A * per unit profit product A + number of units for product B * per unit profit product B or =SUMPRODUCT(B2:C2, B3:C3)

Column E5 to E8 is also dynamically calculated. For example, E5 has Time spent by product A in unit 1 * units manufactured Product A + Time spent by product B in unit 1 * units manufactured Product B or B5 * B3+ C5 *C3. Similarly, E6,7 and 8 are calculated.

Once we have an excel setup, the next steps are easy. Go to Data -> Solver -> Object (choose column E2 where we calculate total profit) -> For “To”, let the default max be selected as we want to maximize profit -> For Changing variable cells choose B3 and C3 where we have units manufactured for A and B -> Add constraints by selecting Hours available cell reference i.e. from E5 to E8 is <= G5 to G8 (constraints can be added one by one or in one go when the comparison is same i.e. in this case <=) -> Choose Solving method as Simplex LP.

When you click on solve, you will get an optimal solution

The solution says that we should produce 2000 units of product A and 1000 units of product B with a maximized profit of 110000000.

Now there can be situations like due to some operational issue we lost 100 hrs in unit 1 or there is a way we can borrow 100 hours for unit 2 from another factory, what is the impact on our profit. Or say due to change in market dynamics product A can give a profit of 40K instead of 30 K. An valuable tool to look at all the related data is sensitivity analysis. When we clicked solve button on Solver, we are given an option to generate a sensitivity analysis report.

The generated report looks like

sensitivity analysis

The upper 2 rows here talk about 2 products. So coming back to our question, that if instead of 30K, we get a profit of 40K from product A, shall that change my product mix. The report says that there is no impact on product mix for increase by 20 K or decreases by 5K, or in other words, product A profit can range from 25K to 50K and current product mix remains valid. Similarly for Product B, the profit range is 30K to 60K. Any change beyond this will need us to recalculate the analysis.

Coming to Constraint data, shadow price indicates that each hour in the current unit has this much impact. For example, if we can increase unit one capacity by one hour, from 4000 to 4001, we can increase our profit by 20K, so getting extra 100 hours will result in 2000K, and reduction by 100 hours will have the same negative impact on profit. The range of increase and decrease of 500 each says that the calculation is valid till this range, so if we say unit one can get more than 500 hours, we will need to recalculate the values as the current calculation will no more hold good.

Hypothesis Testing for Decision Making

In the last post. when I talked about Sampling and Estimation, we discussed P-Value in regression analysis and how this should be less than our error threshold α (alpha). We will understand what is this α value and how we get this while understanding the hypothesis testing.

Hypothesis testing is all about coming up with a hypothesis and figure out if should reject or not. The two components we have here are

  • Null Hypothesis or H0
  • Alternate Hypothesis or H1

Conditiions

  1. Together the two hypotheses should cover all possible outcomes
  2. The two hypotheses should be mutually exclusive.

α is the tolerance level or level of accepting the error. so we can say

P-Value or Probability of current outcome <= α [Reject H0]
P-Value or Probability of current outcome > α [Do not Reject H0]

Reject H0Do not Reject H0
H0 is TrueType 1 ErrorOK
H0 is FalseOKType 2 Error
Hypothesis testing

α is Probability of Type 1 Error.

Let’s take an example, the judiciary system says “innocent till proven guilty”. So consider this as the null hypothesis

H0 Person is innocent (we need to reject this to prove the person is guilty)
H1 Person is guilty

Type 1 Error: Person is innocent but is treated guilty (we target to minimize this)
Type 2 Error: Person is guilty but is treated innocent