Web Architecture 101 PDF
Web Architecture 101 PDF
Web Architecture 101 PDF
If you are a beginner just starting your career in software development, this
course will help you a lot. Designing software is like fitting the lego blocks
together. With this course, you’ll develop an insight into how to fit them
together and build cool stuff.
It will also help you with the software engineering interviews, especially for
the full stack developer positions.
This course does not contain any code, & has a thorough discussion on the
architectural concepts. It also contains a lot of illustrations to help you grasp
the concepts better.
By the end of the course, you’ll have a comprehensive insight into the web
application architecture. If you have a startup idea & you are asking yourself,
how do I implement my app? What technologies do I use? Where do I start?
This course will help you kickstart your entrepreneurial journey.
Also, this course will be continually updated & new content will be
added from time to time.
Course Author #
I am Shivang. I’ve been writing code from the past 8 years professionally & 14
years personally. In my career, I’ve gotten the opportunity to work on large
scale internet services for some of the industry giants on several different
domains such as E-commerce, Fintech, Telecom and others.
I’ve written applications from the bare bones, right from the idea to
production. I’ve maintained code, as well as worked in the production support
for systems receiving millions of hits every single day.
Via this course, I’ve tried my best to share the knowledge, insights and the
experience gained in my years of software development, with all of you
guys!!.
In this lesson, we'll cover the signi cance of web application & software architecture & the reasoning behind
learning it.
Building a web application is no different. The architecture is it’s base & has to
be carefully thought, to avoid any major design changes & code refactoring at
a later point in time.
Speaking with experience, you don’t want to delve into re-designing stuff. It
eats up your time like a black hole. It has the potential to push your shipping
date further down the calendar by months, if not longer. And I won’t even
bring up the wastage of engineering & financial resources which is caused due
to this. No, I won’t!!
Once we are done with the business requirements, we sit down & brainstorm
the use cases which we have to implement. This involves figuring out the
corner cases as early as possible & fitting the Lego blocks together.
Proof Of Concept #
After we pick the fitting tech stack, we start writing a POC (Proof of Concept)
Why a POC?
A POC helps us get a closer, more hands-on view of the technology & the basic
use case implementation. We get an insight into the pros and cons of the tech,
performance or other technical limitations if any.
It helps with the learning curve if we’re working with completely new tech,
also the non-technical people like the product owners, stakeholders have
something concrete to play with & to base their further decisions on.
Now, this is only for an industry scale product. If you are a solo indie
developer or a small group, you can always skip the POC part and start with
the main code.
So, we showcase the POC to the stakeholders & if everyone is satisfied, we
finally get down to creating the main repo & our first dev branch on GitHub,
or any other similar code hosting service which the business prefers.
Phew!!
So, by now you would have realized how important it is to get the architecture
right at the first time & the knowledge of web architecture to developers.
Course Design #
Hmmm…. Alright, this being said. Now speaking of this course. It is divided
into two parts. In the first, we will discuss the concepts & the architectural
components involved in designing web applications.
In the second part, we will go through some of the use cases of designing the
architecture for applications which we use in our day to day lives & are well
familiar with.
We will also understand how applications are designed from the bare bones.
What is the thought process of picking the right technology stack for our use
case & so forth?
This lesson gives an overview of the different topics we will cover in this chapter. Also, we will learn what is a Tier
& it’s components?
• What Is A Tier?
I’ll begin the course by discussing different tiers involved in the software
architectural landscape. This is like a bird’s eye view of the realm of software
architecture & is important to be understood well.
What is a Tier?
Why do software applications have different tiers? What is the need for
them?
How do I decide how many tiers should my application have?
What Is A Tier? #
Think of a tier as a logical separation of components in an application or a
service. And when I say separation, I mean physical separation at the
component level, not the code level.
Database
Backend application server
User interface
Messaging
Caching
These are the different components that make up a web service.
Now let’s have a look at the different types of tiers & their real-life examples.
Single Tier Applications
There are no data requests to the backend server every now and then, which
would make the user experience slow. In single-tier apps, the data is easily &
quickly available since it is located in the same machine.
Though it largely depends on how powerful the machine is & the hardware
requirements of the software, to gauge the real performance of a single-tier
app.
Also, the data of the user stays in his machine & doesn’t need to be transmitted
over a network. This ensures data safety at the highest level.
In the 90s due to this, if a game was shipped with buggy code, there was
nothing the studios could do. They would eventually have to face quite some
heat due to the buggy nature of the software. The testing of the product has to
be thorough, there is no room for any mistakes.
Also, the applications’ performance & the look and feel can get inconsistent as
it largely depends on the configuration of the user’s machine.
Two Tier Applications
Why the need for two-tier applications? Why not host the business logic on a
different machine & have control over it?
Also, again isn’t the application code vulnerable to being accessed by a third
person?
In these scenarios, it won’t cause the business significant harm, even if the
code is accessed by a third person. On the contrary, the upside is since the
code & the user interface reside in the same machine, there are fewer
network calls to the backend server which keeps the latency of the application
low.
The application makes a call to the database server, only when the user has
finished creating his to-do list & wants to persist the changes.
Another good example of this is the online browser & app-based games. The
game files are pretty heavy, they get downloaded on the client just once when
the user uses the application for the first time. Moreover, they make the
network calls only to keep the game state persistent.
Also, fewer server calls mean less money to be spent on the servers which is
naturally economical.
Though, it largely depends on our business requirements & the use case if we
want to pick this type of tier when writing our service.
We can either keep the user interface and the business logic on the client or
move the business logic to a dedicated backend server, which would make it a
three-tier application. Which I am going to discuss up next.
Three Tier Applications
Three-tier applications are pretty popular & largely used in the industry.
Almost all of the simple websites like blogs, news websites etc. are part of this
category.
So, if we take the example of a simple blog, the user interface would be
written using Html, JavaScript, CSS, the backend application logic would run
on a server like Apache & the database would be MySQL. A three-tier
architecture works best for simple use cases.
In this lesson, we will go over the N Tier applications and its components.
• N-Tier Application
• Why The Need For So Many Tiers?
• Single Responsibility Principle
• Separation Of Concerns
• Difference Between Layers & Tiers
N-Tier Application #
Cache
Message queues for asynchronous behaviour
Load balancers
Search servers for searching through massive amounts of data
Components involved in processing massive amounts of data
Components running heterogeneous tech commonly known as web services
etc.
All the social applications like Instagram, Facebook, large scale industry
services like Uber, Airbnb, online massive multiplayer games like Pokemon Go,
applications with fancy features are n-tier applications.
Note: There is another name for n-tier apps, the “distributed
applications”. But, I think it’s not safe to use the word “distributed” yet,
as the term distributed brings along a lot of complex stuff with it. It
would rather confuse us than help. Though I will discuss the distributed
architecture in this course, for now, we will just stick with the term N-
tier applications.
For instance, when upgrading a database server. Like when installing a new
OS or a patch, it wouldn’t impact the other components of the service running
& even if something amiss happens during the OS installation process, just the
database component would go down. The application as a whole would still be
up & would only impact the features requiring the database.
We can also have dedicated teams & code repositories for every component,
thus keeping things cleaner.
refactor the application code & squeeze in the stored procedure logic
somewhere?
A database should not hold business logic, it should only take care of
persisting the data. This is what the single responsibility principle is. And this
is why we have separate tiers for separate components.
Separation Of Concerns #
Separation of concerns kind of means the same thing, be concerned about
your work only & stop worrying about the rest of the stuff.
These principles act at all the levels of the service, be it at the tier level or the
code level.
Having loosely coupled components is the way to go. The approach makes
scaling the service easy in future when things grow beyond a certain level.
Note: Don’t confuse tiers with the layers of the application. Some prefer
to use them interchangeably. But in the industry layers of an application
typically means the user interface layer, business layer, service layer, or
the data access layer.
The layers mentioned in the illustration are at the code level. The difference
between layers and tiers is that the layers represent the organization of the
code and breaking it into components. Whereas, tiers involve physical
separation of components.
All these layers together can be used in any tiered application. Be it single,
two, three or N-tier. I’ll discuss these layers in detail in the course ahead.
Alright, now we have an understanding of tiers. Let’s zoom-in one notch &
focus on web architecture.
Different Tiers In Software Architecture Quiz
This lesson contains a quiz to test your understanding of tiers in software architecture.
1
What is a tier?
COMPLETED 0%
1 of 6
What Is Web Architecture?
In the introduction of this course, I already talked about why the knowledge
of web architecture is important to software engineers. Now we will explore it
further.
I’ll step by step go through every component starting with the client-server
architecture.
Client Server Architecture
A very small percent of the business websites and applications use the peer to
peer architecture, which is different from the client-server.
I will discuss that in ahead in the course. For now, let’s focus on the client.
Client
In this lesson, we will explore the Client component of the Client-Server Architecture.
• Client
• Technologies Used To Implement Clients In Web Applications
Client #
The client holds our user interface. The user interface is the presentation part
of the application. It’s written in Html, JavaScript, CSS and is responsible for
the look & feel of the application.
The user interface runs on the client. The client can be a mobile app, a
desktop or a tablet like an iPad. It can also be a web-based console, running
commands to interact with the backend server.
Technologies Used To Implement Clients In Web
Applications #
In very simple terms, a client is the window to our application. In the industry,
the open-source technologies popular for writing the web-based user interface
are ReactJS, AngularJS, VueJS, Jquery etc. All these libraries use JavaScript.
There are a plethora of other technologies for writing the front-end too, I have
just listed the popular ones for now.
If you are intrigued about the technologies popular in the industry have a look
at the developer survey run by StackOverflow for this year
Types of Client
In this lesson, we will learn about the two types of client: the Thin Client and the Thick Client (sometimes also
called the Fat Client).
• Thin Client
• Thick Client
1. Thin Client
2. Thick Client (sometimes also called the Fat client)
Thin Client #
Thin Client is the client which holds just the user interface of the application.
It has no business logic of any sort. For every action, the client sends a request
to the backend server. Just like in a three-tier application.
Thick Client #
On the contrary, the thick client holds all or some part of the business logic.
These are the two-tier applications. We’ve already gone through this if you
remember.
The typical examples of Fat clients are utility apps, online games etc.
Server
In this lesson, we will explore the Server component of the Client-Server Architecture.
The primary task of a web server is to receive the requests from the
client & provide the response after executing the business logic based on
the request parameters received from the client.
Every service, running online, needs a server to run. Servers running web
applications are commonly known as the application servers.
Besides the application servers, there are other kinds of servers too with
specific tasks assigned to them such as the:
Proxy server
Mail server
File server
Virtual server
The server configuration & the type can differ depending on the use case.
For simple use cases such as hosting websites, we would pick the Apache
HTTP Server.
Server-Side Rendering #
Often the developers use a server to render the user interface on the backend
& then send the rendered data to the client. The technique is known as server-
side rendering. I will discuss the pros & cons of client-side vs server-side
rendering further down the course.
Now we have a fundamental understanding of both the client & the server.
Let’s delve into some of the concepts involved in the communication between
them.
Communication Between the Client & the Server
In this lesson, we will learn how communication takes place between the Client and the Server.
• Request-Response Model
• HTTP Protocol
• REST API & API Endpoints
• Real World Example Of Using A REST API
Request-Response Model #
The client & the server have a request-response model. The client sends the
request & the server responds with the data.
HTTP Protocol #
The entire communication happens over the HTTP protocol. It is the protocol
for data exchange over the World Wide Web. HTTP protocol is a request-
response protocol that defines how information is transmitted across the web.
It’s a stateless protocol, every process over HTTP is executed independently &
has no knowledge of previous processes.
If you want to read more about the protocol, this is a good resource on it
Note: If you aren’t aware of the REST API & the API Endpoints, I have
discussed it in the next lesson in detail. I’ve brought up the terms in this
lesson, just to give you a heads up on how modern distributed web
applications communicate.
To implement this, the first step would be to get the data on the birthdays of
all our Facebook friends.
We would write a client which would hit the Facebook Social Graph API which
is a REST-API to get the data & then run our business logic on the data.
This lesson contains a quiz to test your understanding of the client, the server & the communication between
them.
1
Where does the user interface component of a web application runs?
COMPLETED 0%
1 of 5
What Is A REST API?
• WHAT IS REST?
• REST API
• REST Endpoint
• Decoupling Clients & the Backend Service
• Application Development Before the REST API
• API Gateway
WHAT IS REST? #
REST API #
A REST API is an API implementation that adheres to the REST architectural
constraints. It acts as an interface. The communication between the client &
the server happens over HTTP. A REST API takes advantage of the HTTP
methodologies to establish communication between the client and the server.
REST also enables servers to cache the response that improves the
performance of the application.
The communication between the client and the server is a stateless process.
And by that, I mean every communication between the client and the server is
like a new one.
With the implementation of a REST API the client gets the backend endpoints to
communicate with. This entirely decouples the backend & the client code.
REST Endpoint #
An API/REST/Backend endpoint means the url of a service. For example,
https://myservice.com/getuserdetails/{username} is a backend endpoint for
fetching the user details of a particular user from the service.
The REST-based service will expose this url to all its clients to fetch the user
details using the above stated url.
Decoupling Clients & the Backend Service #
With the availability of the endpoints, the backend service does not have to
worry about the client implementation. It just calls out to its multiple clients &
says “Hey everyone, here is the url address of the resource/information you
need. Hit it when you need it. Any client with the required authorization to
access a resource can access it”.
This means the clients and the backend service are decoupled.
We would always put business logic in the JSP tags. Which made code
refactoring & adding new features really difficult as the logic got spread
across different layers.
Also, in the same codebase, we had to write separate code/classes for handling
requests from different types of clients. A different servlet for a mobile client
and a different one for a web-based client.
After the REST APIs became widely used, there was no need to worry about
the type of the client. Just provide the endpoints & the response would contain
data generally in the JSON or any other standard data transport format. And
the client would handle the data in whatever way they would want.
This cut down a lot of unnecessary work for us. Also, adding new clients
became a lot easier. We could introduce multiple types of new clients without
considering the backend implementation.
API Gateway #
The REST-API acts as a gateway, as a single entry point into the system. It
encapsulates the business logic. Handles all the client requests, taking care of
the authorization, authentication, sanitizing the input data & other necessary
tasks before providing access to the application resources.
So, now we are aware of the client-server architecture, we know what a REST
API is. It acts as the interface & the communication between the client and the
server happens over HTTP.
Let’s look into the HTTP Pull & Push-based communication mechanism.
HTTP Push & Pull - Introduction
In this lesson, we will have an introduction to the HTTP Push & Pull mechanism.
• HTTP PULL
• HTTP PUSH
In this lesson, we will get an insight into the HTTP Push & Pull mechanism. We
know that the majority of the communication on the web happens over HTTP,
especially wherever the client-server architecture is involved.
There are two modes of data transfer between the client and the server. HTTP
PUSH & HTTP PULL. Let’s find out what they are & what they do.
HTTP PULL #
As I stated earlier, for every response, there has to be a request first. The
client sends the request & the server responds with the data. This is the
default mode of HTTP communication, called the HTTP PULL mechanism.
The client pulls the data from the server whenever it requires. And it keeps
doing it over and over to fetch the updated data.
An important thing to note here is that every request to the server and the
response to it consumes bandwidth. Every hit on the server costs the business
money & adds more load on the server.
What if there is no updated data available on the server, every time the client
sends a request?
The client doesn’t know that, so naturally, it would keep sending the requests
to the server over and over. This is not ideal & a waste of resources. Excessive
pulls by the clients have the potential to bring down the server.
HTTP PUSH #
To tackle this, we have the HTTP PUSH based mechanism. In this mechanism,
the client sends the request for particular information to the server, just for
the first time, & after that the server keeps pushing the new updates to the
client whenever they are available.
The client doesn’t have to worry about sending requests to the server, for
data, every now & then. This saves a lot of network bandwidth & cuts down
the load on the server by notches.
This is also known as a Callback. Client phones the server for information. The
server responds, Hey!! I don’t have the information right now but I’ll call you
back whenever it is available.
Clients use AJAX (Asynchronous JavaScript & XML) to send requests to the
server in the HTTP Pull based mechanism.
There are multiple technologies involved in the HTTP Push based mechanism
such as:
In this lesson, we will understand HTTP Pull, AJAX and how polling is done using AJAX.
The first is sending an HTTP GET request to the server manually by triggering
an event, like by clicking a button or any other element on the web page.
AJAX stands for asynchronous JavaScript & XML. The name says it all, it
is used for adding asynchronous behaviour to the web page.
As we can see in the illustration above, instead of requesting the data
manually every time with the click of a button. AJAX enables us to fetch the
updated data from the server by automatically sending the requests over and
over at stipulated intervals.
Upon receiving the updates, a particular section of the web page is updated
dynamically by the callback method. We see this behaviour all the time on
news & sports websites, where the updated event information is dynamically
displayed on the page without the need to reload it.
AJAX uses an XMLHttpRequest object for sending the requests to the server
which is built-in the browser and uses JavaScript to update the HTML DOM.
If the client doesn’t receive a response from the server within the TTL, the
browser kills the connection & the client has to re-send the request hoping it
would receive the data from the server before the TTL ends this time.
Open connections consume resources & there is a limit to the number of open
connections a server can handle at one point in time. If the connections don’t
close & new ones are being introduced, over time, the server will run out of
memory. Hence, the TTL is used in client-server communication.
But what if we are certain that the response will take more time than the TTL
set by the browser?
Persistent Connection #
In this case, we need a Persistent Connection between the client and the
server.
A persistent connection is a network connection between the client & the
server that remains open for further requests & the responses, as
opposed to being closed after a single communication.
Heartbeat Interceptors #
Now you might be wondering how is a persistent connection possible if the
browser kills the open connections to the server every X seconds?
The connection between the client and the server stays open with the help of
Heartbeat Interceptors.
These are just blank request responses between the client and the server
to prevent the browser from killing the connection.
It would be apt to establish a persistent connection between the client and the
server from a user experience standpoint.
• Web Sockets
• AJAX – Long Polling
• HTML5 Event Source API & Server Sent Events
• Streaming Over HTTP
• Summary
Web Sockets #
A Web Socket connection is ideally preferred when we need a persistent bi-
directional low latency data flow from the client to server & back.
With Web Sockets, we can keep the client-server connection open as long as
we want.
Have bi-directional data? Go ahead with Web Sockets. One more thing, Web
Sockets tech doesn’t work over HTTP. It runs over TCP. The server & the client
should both support web sockets or else it won’t work.
The WebSocket API & Introducing WebSockets – Bringing Sockets to the Web
are good resources for further reading on web sockets
The upside of using this technique is that there are quite a smaller number of
requests sent from the client to the server, in comparison to the regular
polling mechanism. This reduces a lot of network bandwidth consumption.
Long polling can be used in simple asynchronous data fetch use cases when
you do not want to poll the server every now & then.
Via this approach, the servers can initiate data transmission towards the
client once the client has established the connection with an initial request.
SSE is ideal for scenarios such as a real-time feed like that of Twitter,
displaying stock quotes on the UI, real-time notifications etc.
The technique is primarily used for streaming multimedia content, like large
images, videos etc, over HTTP.
To stream data, both the client & the server agree to conform to some
streaming settings. This helps them figure when the stream begins & ends
over an HTTP request-response model.
You can go through this resource for further reading on Stream API
Summary #
So, now we have an understanding of what HTTP Pull & Push is. We went
through different technologies which help us establish a persistent connection
between the client and the server.
Every tech has a specific use case, Ajax is used to dynamically update the web
page by polling the server at regular intervals.
Long polling has a connection open time slightly longer than the polling
mechanism.
Web Sockets have bi-directional data flow, whereas Server sent events
facilitate data flow from the server to the client.
What tech would fit best for our use cases depends on the kind of application
we intend to build.
Alright, let’s quickly gain an insight into the pros & cons of the client and the
server-side rendering.
Client-Side Vs Server-Side Rendering
In this lesson, we will learn about the client side and the server-side rendering & the use cases for both the
approaches.
Browser engine
Rendering engine
JavaScript interpreter
Networking & the UI backend
Data storage etc.
I won’t go into much detail but the browser has to do a lot of work to convert
the response from the server into an HTML page.
The rendering engine constructs the DOM tree, renders & paints the
construction. And naturally, all this activity needs a bit of time.
Server-Side Rendering #
To avoid all this rendering time on the client, developers often render the UI
on the server, generate HTML there & directly send the HTML page to the UI.
This technique is known as the Server-side rendering. It ensures faster
rendering of the UI, averting the UI loading time in the browser window since
the page is already created & the browser doesn’t have to do much assembling
& rendering work.
A big downside to this is once the number of concurrent users on the website
rises, it puts an unnecessary load on the server.
Though we can leverage a hybrid approach, to get the most out of both
techniques. We can use server-side rendering for the home page & for the
other static content on our website & use client-side rendering for the
dynamic pages.
Alright, before moving down to the database, message queue & the caching
components. It’s important for us to understand a few concepts such as:
Monolithic architecture
Micro-services
Scalability
High availability
Distributed systems
The clarity on these concepts will help us understand the rest of the web
components better. Let’s have a look one by one.
Web Architecture Quiz - Part 2
This lesson contains a quiz to test your understanding of the REST API & the HTTP mechanisms.
Let’s Test Your Understanding Of the REST API & the HTTP mechanisms
1
Why should we implement a REST API in our application? Which of the
following option(s) are correct?
COMPLETED 0%
1 of 8
What Is Scalability?
• What is Scalability?
• What Is Latency?
• Measuring Latency
• Network Latency
• Application Latency
• Why Is Low Latency So Important For Online Services?
This chapter is a deep dive into scalability. It covers all the frequently asked
questions on it such as: what does scalability mean in the context of web
applications, distributed systems or cloud computing? Etc.
What is Scalability? #
Scalability means the ability of the application to handle & withstand
increased workload without sacrificing the latency.
What Is Latency? #
Latency is the amount of time a system takes to respond to a user request.
Let’s say you send a request to an app to fetch an image & the system takes 2
seconds to respond to your request. The latency of the system is 2 seconds.
Minimum latency is what efficient software systems strive for. No matter how
much the traffic load on a system builds up, the latency should not go up. This
is what scalability is.
If the latency remains the same, we can say yeah, the application scaled well
with the increased load & is highly scalable.
A program with the complexity of O(n^2) where n is the size of the data set is
not scalable. As the size of the data set increases the system will need more
computational power to process the tasks.
So, how do we measure latency?
Measuring Latency #
Latency is measured as the time difference between the action that the user
takes on the website, it can be an event like the click of a button, & the system
response in reaction to that event.
1. Network Latency
2. Application Latency
Network Latency #
Network Latency is the amount of time that the network takes for sending a
data packet from point A to point B. The network should be efficient enough
to handle the increased traffic load on the website. To cut down the network
latency, businesses use CDN & try to deploy their servers across the globe as
close to the end-user as possible.
Application Latency #
Application Latency is the amount of time the application takes to process a
user request. There are more than a few ways to cut down the application
latency. The first step is to run stress & load tests on the application & scan for
the bottlenecks that slow down the system as a whole. I’ve talked more about
it in the upcoming lesson.
If the visitor gets the response within a stipulated time, great or he bounces
off to another website.
There are numerous market researches that present the fact that high latency
in applications is a big factor in customers bouncing off a website. If there is
money involved, zero latency is what businesses want, only if that was
possible.
We can realize the importance of low latency by the fact that Huawei &
Hibernia Atlantic in the year 2011 started laying a fibre-optic link cable across
the Atlantic Ocean between London & Newyork, that was estimated having a
cost of approx. $300M, just to save traders 6 milliseconds of latency.
Types Of Scalability
In this lesson, we will explore the two types of scaling: Vertical and Horizontal Scaling.
An application to scale well needs solid computing power. The servers should
be powerful enough to handle increased traffic loads.
1. Vertical Scaling
2. Horizontal Scaling
In this type of scaling we increase the power of the hardware running the
app. This is the simplest way to scale since it doesn’t require any code
refactoring, not making any complex configurations and stuff. I’ll discuss
further down the lesson, why code refactoring is required when we
horizontally scale the app.
But there is only so much we can do when scaling vertically. There is a limit to
the capacity we can augment for a single server.
Now is the time to build more buildings. This is where Horizontal Scalability
comes in.
When the traffic is just too much to be handled by single hardware, we bring
in more servers to work together.
What is Horizontal Scaling? #
Horizontal scaling, also known as scaling out, means adding more hardware
to the existing hardware resource pool. This increases the computational
power of the system as a whole.
Now the increased traffic influx can be easily dealt with the increased
computational capacity & there is literally no limit to how much we can scale
horizontally assuming we have infinite resources. We can keep adding servers
after servers, setting up data centres after data centres.
Cloud Elasticity #
The biggest reason why cloud computing got so popular in the industry is the
ability to scale up & down dynamically. The ability to use & pay only for the
resources required by the website became a trend for obvious reasons.
If the site has a heavy traffic influx more server nodes get added & when it
doesn’t the dynamically added nodes are removed.
This approach saves businesses bags of money every single day. The approach
is also known as cloud elasticity. It indicates the stretching & returning to the
original infrastructural computational capacity.
Having multiple server nodes on the backend also helps with the website
staying alive online all the time even if a few server nodes crash. This is
known as High Availability. We’ll get to that in the upcoming lessons.
Which Scalability Approach Is Right For Your App?
In this lesson, we will learn about which type of scaling is better for a given scenario.
No static instances in the class. Static instances hold application data & if a
particular server goes down all the static data/state is lost. The app is left in an
inconsistent state.
Rather, use a persistent memory like a key-value store to hold the data & to
remove all the state/static variable from the class. This is why functional
programming got so popular with distributed systems. The functions don’t
retain any state.
Always have a ballpark estimate on mind when designing your app. How
much traffic will it have to deal with?
I’ll discuss cloud, serverless and microservices in the upcoming lessons. So,
stay tuned.
If your app is a public-facing social app like a social network, a fitness app or
something similar, then the traffic is expected to spike exponentially in the
near future. Both high availability & horizontal scalability is important to you.
Build to deploy it on the cloud & always have horizontal scalability in mind
right from the start.
Primary Bottlenecks that Hurt the Scalability Of Our
Application
• Database
• Application Architecture
• Not Using Caching In the Application Wisely
• Inef cient Con guration & Setup of Load Balancers
• Adding Business Logic to the Database
• Not Picking the Right Database
• At the Code Level
There are several points in a web application which can become a bottleneck
& can hurt the scalability of our application. Let’s have a look at them.
Database #
Consider that, we have an application that appears to be well architected.
Everything looks good. The workload runs on multiple nodes & has the ability
to horizontally scale.
But the database is a poor single monolith, just one server been given the onus
of handling the data requests from all the server nodes of the workload.
This scenario is a bottleneck. The server nodes work well, handle millions of
requests at a point in time efficiently, still, the response time of these requests
& the latency of the application is very high due to the presence of a single
database. There is only so much it can handle.
Just like the workload scalability, the database needs to be scaled well.
Make wise use of database partitioning, sharding, use multiple database
servers to make the module efficient.
Application Architecture #
A poorly designed application’s architecture can become a major bottleneck as
a whole.
For instance, if a user uploads a document on the portal, tasks such as sending
a confirmation email to the user, sending a notification to all of the
subscribers/listeners to the upload event should be done asynchronously.
Trying to pull off things with a not so suitable tech always has a profound
impact on the latency of the entire application in negative ways.
In this lesson, if a few of the things are not clear to you such as Strong
consistency, how message queue provides an asynchronous behaviour, how to
pick the right database. I’ll discuss all that in the upcoming lessons, stay
tuned.
How To Improve & Test the Scalability Of Our
Application?
In this lesson, we will learn how we can improve & test the scalability of our application.
Here are some of the common & the best strategies to fine-tune the
performance of our web application. If the application is performance-
optimized it can withstand more traffic load with less resource consumption
as opposed to an application that is not optimized for performance.
Profile the hell out. Run application profiler, code profiler. See which processes
are taking too long, eating up too much resources. Find out the bottlenecks.
Get rid of them.
Profiling is the dynamic analysis of our code. It helps us measure the space
and the time complexity of our code & enables us to figure out issues like
concurrency errors, memory errors & robustness & safety of the program.
This Wikipedia resource contains a good list of performance analysis tools
used in the industry
Caching #
Cache wisely. Cache everywhere. Cache all the static content. Hit the database
only when it is really required. Try to serve all the read requests from the
cache. Use a write-through cache.
Data Compression #
Compress data. Use apt compression algorithms to compress data. Store data
in the compressed form. As compressed data consumes less bandwidth,
consequently, the download speed of the data on the client will be faster.
These are a few of the things we should keep in mind in context to the
performance of the application.
During the scalability testing, different system parameters are taken into
account such as the CPU usage, network bandwidth consumption, throughput,
the number of requests processed within a stipulated time, latency, memory
usage of the program, end-user experience when the system is under heavy load
etc.
In this testing phase, simulated traffic is routed to the system, to study how the
system behaves under the heavy load, how the application scales under the
heavy load. Contingencies are planned for unforeseen situations.
Several load & stress tests are run on the application. Tools like JMeter are
pretty popular for running concurrent user test on the application if you are
working on a Java ecosystem. There are a lot of cloud-based testing tools
available that help us simulate tests scenarios just with a few mouse clicks.
Businesses test for scalability all the time to get their systems ready to handle
the traffic surge. If it’s a sports website it would prepare itself for the sports
event day, if it’s an e-commerce website it would make itself ready for the
festival season.
Also, how Hotstar a video streaming service scaled with over 10 million
concurrent users
In the industry tech like Cadvisor, Prometheus and Grafana are pretty popular
for tracking the system via web-based dashboards.
I’ve written an article on it in case you want to read more about the pre-
production monitoring.
Scalability Quiz
1
Which of the following statements is true in context to latency &
scalability?
COMPLETED 0%
1 of 5
What Is High Availability?
In this lesson, we will learn about high availability and its importance in online services.
This lesson is an insight into high availability. It covers all the frequently asked
questions about it such as:
What is it?
Why is it so important to businesses?
What is a highly available cluster?
How do cloud platforms ensure high availability of the services running
on them?
What is fault tolerance & redundancy? How are they related to high
availability?
High availability ensures the uptime of the service much more than the
normal time. It improves the reliability of the system, ensures minimum
downtime.
The sole mission of highly available systems is to stay online & stay connected.
A very basic example of this is having back-up generators to ensure
continuous power supply in case of any power outages.
In this lesson, we will discuss the common reasons for system failure.
• Software Crashes
• Hardware Failures
• Human Errors
• Planned Downtime
Before delving into the HA system design, fault-tolerance and redundancy. I’ll
first talk about the common reasons why systems fail.
Software Crashes #
I am sure you are pretty familiar with software crashes. Applications crash all
the time, be it on a mobile phone or a desktop.
Corrupt software files. Remember the BSOD blue screen of death in windows?
OS crashing, memory-hogging unresponsive processes. Likewise, software
running on cloud nodes crash unpredictably, along with it they take down the
entire node.
Hardware Failures #
Another reason for system failure is hardware crashes. Overloaded CPU, RAM,
hard disk failures, nodes going down. Network outages.
Human Errors #
This is the biggest reason for system failures. Flawed configurations & stuff.
Google made a tiny network configuration error & it took down almost half of
the internet in Japan. This is an interesting read.
Planned Downtime #
Besides the unplanned crashes, there are planned down times which involve
routine maintenance operations, patching of software, hardware upgrades
etc.
These are the primary reasons for system failures, now let’s talk about how
HA systems are designed to overcome these scenarios of system downtime.
Achieving High Availability - Fault Tolerance
In this lesson, we will learn about fault tolerance & designing a HA fault tolerant service.
There are several approaches to achieve HA. The most important of them is to
make the system fault-tolerant.
Fault tolerance is the ability of the system to stay up despite taking hits.
A few of the instances/nodes, out of several, running the service go offline &
bounce back all the time. In case of these internal failures, the system could
work at a reduced level but it will not go down entirely.
There are many upsides of splitting a big monolith into several microservices,
as it provides:
Easier management
Easier development
Ease of adding new features
Ease of maintenance
High availability
So, even if a few services go down the application as a whole is still up.
Redundancy
There are systems like GPS, aircrafts, communication satellites which have
zero downtime. The availability of these systems is ensured by making the
components redundant.
When so many redundant nodes are deployed, there are no single points of
failure in the system. In case a node goes down redundant nodes take its
place. Thus, the system as a whole remains unimpacted.
Since the most common cause of failures is human error, automation helps
cut down failures to a big extent.
Replication
time.
This avoids the single point of failure thing in context to a data centre. Also,
the latency is reduced by quite an extent due to the proximity of data to the
user.
All the highly available fault-tolerant design decisions are subjective to how
critical the system is? What are the odds that the components will fail? Etc.
Now, that we have a clear understanding of high availability, let’s talk a bit
about the high availability cluster.
A High Availability cluster also known as the Fail-Over cluster contains a set of
nodes running in conjunction with each other that ensures high availability of
the service.
The nodes in the cluster are connected by a private network called the
Heartbeat network that continuously monitors the health and the status of
each node in the cluster.
A single state across all the nodes in a cluster is achieved with the help of a
shared distributed memory & a distributed co-ordination service like the
Zookeeper.
To ensure the availability, HA clusters use several techniques such as Disk
mirroring/RAID Redundant Array Of Independent Disks, redundant network
connections, redundant electrical power etc. The network connections are
made redundant so if the primary network goes down the backup network
takes over.
1
Which of the following statements is true in context to scalability & high
availability?
COMPLETED 0%
1 of 4
Understanding DNS – Part 1
In this lesson, we will understand what is DNS Domain Name System & how does it work
Every machine that is online & is a part of world wide web www has a unique IP
address that enables it to be contacted by other machines on the web using that
particular IP address.
IP stands for Internet Protocol. It’s a protocol that facilitates delivery of data
packets from one machine to the other using their IP addresses.
Now, naturally, it’s not viable to type in the IP address of the website, from our
memory, every time we visit a certain website. Even if we try to how many IP
addresses do you think you can remember?
Typing in domain names for instance amazon.com is a lot easier than working
directly with IP addresses. I think we can all agree on this;
amazon.com is a domain name that is mapped to its unique IP address by the DNS
so that we are not expected to type in the IP address of amazon.com into our
browsers every time we visit that website.
If you are intrigued and want to read more on IP addresses, you can visit this
Wikipedia resource on it.
There are four key components, i.e. a group of servers, that make up the DNS
infrastructure. Those are -
In the next lesson let’s understand how does the DNS query lookup process work
and what is the role of these servers in the lookup process.
Understanding DNS – Part 2
This lesson continues the discussion on the domain name system
In this lesson, we will have an insight into the complete DNS query lookup process
& we will also understand the role of different servers in the DNS infrastructure.
Lets’ begin…
So, when the user hits enter after typing in the domain name into their browser,
the browser sends a request to the DNS Recursive nameserver which is also known
as the DNS Resolver.
The role of DNS Resolver is to receive the client request and forward it to the Root
nameserver to get the address of the Top-Level domain nameserver.
These data centers contain clusters of servers that are optimized to process DNS
queries in minimal time that is in milliseconds.
So, once the DNS Resolver forwards the request to the Root nameserver, the Root
nameserver returns the address of the Top-Level domain nameserver in response.
As an example, the top-level domain for amazon.com is .com.
Once the DNS Resolver receives the address of the top-level domain nameserver, it
sends a request to it to fetch the details of the domain name. Top level domain
nameservers hold the data for domains using their top-level domains.
Since our domain is amazon.com the DNS Resolver will route the request to the
.com top-level domain name server.
Once the top-level domain name server receives the request from the Resolver, it
returns the IP address of amazon.com domain name server.
amazon.com domain name server is the last server in the DNS query lookup
process. It is the nameserver responsible for the amazon.com domain & is also
known as the Authoritative nameserver. This nameserver is owned by the owner of
the domain name.
DNS Resolver then fires a query to the authoritative nameserver & it then returns
the IP address of amazon.com website to the DNS Resolver. DNS Resolver caches the
data and forwards it to the client.
On receiving the response, the browser sends a request to the IP address of the
amazon.com website to fetch data from their servers.
Often all this DNS information is cached and the DNS servers don’t have to do so
much rerouting every time a client requests an IP of a certain website.
DNS information of websites that we visit also gets cached in our local machines
that is our browsing devices with a TTL Time To Live.
All the modern browsers do this automatically to cut down the DNS query lookup
time when revisiting a website.
And did I bring up scalability? To scale, we need to split things up. We need
to scale out when we can’t scale up further. Microservices architecture is
inherently designed to scale.
The diagram below represents a microservices architecture:
Every service ideally has a separate database, there are no single points of
failure & system bottlenecks.
When a large-scale service such as amazon.com runs, it needs way more than a
single machine to run its services. A service as big as amazon.com is deployed
across multiple data centers in different geographical locations across the globe.
To spread the user traffic across different clusters in different data centers. There
are different ways to setup load balancing. In this lesson, we will discuss DNS load
balancing that is setup at the DNS level on the authoritative server.
DNS load balancing enables the authoritative server to return different IP addresses
of a certain domain to the clients. Every time it receives a query for an IP, it
returns a list of IP addresses of a domain to the client.
With every request, the authoritative server changes the order of the IP addresses
in the list in a round-robin fashion.
As the client receives the list, it sends out a request to the first IP address on the list
to fetch the data from the website. The reason for returning a list of IP addresses to
the client is to enable it to use other IP addresses in the list in case the first doesn’t
return a response within a stipulated time.
When another client sends out a request for an IP address to the authoritative
server, it re-orders the list and puts another IP address on the top of the list
following the round-robin algorithm.
Also, when the client hits an IP it may not necessarily hit an application server, it
may hit another load balancer implemented at the data center level that manages
the clusters of application servers.
Limitations Of DNS Load Balancing #
Also, since these IP addresses are cached by the client’s machine and the DNS
Resolver, there is always a possibility of a request being routed to a machine that is
out of service.
DNS load balancing despite its limitations is preferred by companies because it’s an
easy and less expensive way of setting up load balancing on their service.
In this lesson, we will learn about the pros and cons of the Microservice Architecture & when should we pick it for
our project.
Polyglot persistence is using multiple databases types like SQL, NoSQL together
in an architecture. I’ll discuss it in detail in the database lesson.
No Strong Consistency #
Strong consistency is hard to guarantee in a distributed environment. Things
are Eventually consistent across the nodes. And this limitation is due to the
distributed design.
I’ll discuss both Strong and eventual consistency in the database chapter.
We’ve already discussed DNS load balancing in the former lesson. In this one, we
will discuss hardware and software load balancing.
If the business has an IT team & network specialists in house, they can take care of
these load balancers else the developers are expected to wrap their head around
how to set up these hardware load balancers with some assistance from the
vendor. This is the reason developers prefer working with software load
balancers.
When using hardware load balancers, we may also have to overprovision them to
deal with the peak traffic that is not the case with software load balancers.
You will also find several LBaaS Load Balancers as a Service services online that
enable you to directly plug in load balancers into your application without you
having to do any sort of setup.
Software load balancers are pretty advanced when compared to DNS load
balancing as they consider many parameters such as content that the servers host,
cookies, HTTP headers, CPU & memory utilization, load on the network & so on to
route traffic across the servers.
They also continually perform health checks on the servers to keep an updated list
of in-service machines.
Development teams prefer to work with software load balancers as hardware load
balancers require specialists to manage them.
HAProxy is one example of a software load balancer that is widely used by the big
guns in the industry to scale their systems such as GitHub, Reddit, Instagram, AWS,
Besides the Round Robin algorithm that the DNS Load balancers use, software load
balancers leverage several other algorithms to efficiently route traffic across the
machines. Let’s have an insight.
We have another approach known as the Weighted Round Robin where based on
the server’s compute & traffic handling capacity weights are assigned to them. And
then based on the server weights, traffic is routed to them using the Round Robin
algorithm.
With this approach, more traffic is converged to machines that can handle a higher
traffic load thus making efficient use of the resources.
This approach is pretty useful when the service is deployed in different data
centers having different compute capacities. More traffic can be directed to the
larger data centers containing more machines.
Least Connections #
When using this algorithm, the traffic is routed to the machine that has the least
open connections of all the machines in the cluster. There are two approaches to
implement this –
In the first, it is assumed that all the requests will consume an equal amount of
server resources & the traffic is routed to the machine, having the least open
connections, based on this assumption.
Now in this scenario, there is a possibility that the machine having the least open
connections might be already processing requests demanding most of its CPU
power. Routing more traffic to this machine wouldn t be such a good idea.
In the other approach, the CPU utilization & the request processing time of the
chosen machine is also taken into account before routing the traffic to it. Machines
with less request processing time, CPU utilization & simultaneously having the
least open connections are the right candidates to process the future client
requests.
The least connections approach comes in handy when the server has long opened
connections, for instance, persistent connections in a gaming application.
Random #
Following this approach, the traffic is randomly routed to the servers. The load
balancer may also find similar servers in terms of existing load, request processing
time and so on and then it randomly routes the traffic to these machines.
Hash #
In this approach, the source IP where the request is coming from and the request
URL are hashed to route the traffic to the backend servers.
Hashing the source IP ensures that the request of a client with a certain IP will
always be routed to the same server.
This facilitates better user experience as the server has already processed the
initial client requests and holds the client’s data in its local memory. There is no
need for it to fetch the client session data from the session memory of the cluster &
then process the request. This reduces latency.
Hashing the client IP also enables the client to re-establish the connection with the
same server, that was processing its request, in case the connection drops.
Hashing a url ensures that the request with that url always hits a certain cache that
already has data on it. This is to ensure that there is no cache miss.
This also averts the need for duplicating data in every cache and is thus a more
efficient way to implement caching.
Well, this is pretty much it on the fundamentals of load balancing. In the next
chapter, let’s understand monoliths and microservices.
Monolith & Microservices Quiz
This lesson contains a quiz to test your understanding of monoliths and microservices.
1
When should we use a monolithic architecture for our project? Which of
the following option(s) are correct?
COMPLETED 0%
1 of 4
Introduction & Types of Data
In this lesson, we will have an introduction to databases and the different types of data.
• What Is A Database?
• Structured Data
• Unstructured Data
• Semi-structured Data
• User state
What Is A Database? #
A database is a component required to persist data. Data can be of many
forms: structured, unstructured, semi-structured and user state data.
Let’s quickly have an insight into the classification of data before delving into
the databases.
Structured Data #
Structured data is the type of data which conforms to a certain structure,
typically stored in a database in a normalized fashion.
There is no need to run any sort of data preparation logic on structured data
before processing it. Direct interaction can be done with this kind of data.
So, with structured data, we know what we are dealing with. Since the
customer name is of String type, without much worry of errors or exceptions,
we can run String operations on it.
Unstructured Data #
Unstructured data has no definite structure. It is generally the heterogeneous
type of data comprising of text, image files, video, multimedia files, pdfs, Blob
objects, word documents, machine-generated data etc.
This kind of data is often encountered in data analytics. Here the data streams
in from multiple sources such as IoT devices, social networks, web portals,
industry sensors etc. into the analytics systems.
We cannot just directly process unstructured data. The initial data is pretty
raw, we have to make it flow through a data preparation stage which
segregates it based on some business logic & then runs the analytics
algorithms on it.
Semi-structured Data #
Semi-structured data is a mix of structured & unstructured data. Semi-
structured data is often stored in data transport formats such as XML or JSON
and is handled as per the business requirements.
User state #
The data containing the user state is the information of activity which the user
performs on the website.
For instance, when browsing through an e-commerce website, the user would
browse through several product categories, change the preferences, add a few
products to the reminder list for the price drops.
All this activity is the user state. So next time, when the user logs in, he can
continue from where he left off. It would not feel like that one is starting fresh
& all the previous activity is lost.
Storing user state improves the browsing experience & the conversion rate for
the business.
So, now we are clear on the different types of data. Let’s have a look into
different types of databases.
There are multiple different types of databases with specific use cases. We’ll
quickly go through each of them in order to have a bird’s eye view of the
database realm.
Load Balancing Quiz
This lesson contains a quiz to test your understanding of load balancing in web applications.
1
Why do we need load balancing in web applications?
2
Why do load balancers regularly need to perform the health checks of the
infrastructure?
Why does DNS load balancing make authoritative nameserver return a list of
IP addresses as opposed to just returning a single different IP every time?
5
What is true about DNS load balancing?
Retake Quiz
Relational Database
This is a one to many relationship. In the simplest of forms, one table will
contain the details of all the customers & another table will contain all the
products in the inventory.
One row in the customer table will correspond to multiple rows in the product
inventory table.
On pulling the user object with id C1 from the database we can easily find
what books C1 purchased via the relationship model.
Data Consistency #
Besides, the relationships, relational databases also ensure saving data in a
normalized fashion. In very simple terms, normalized data means a unique
entity occurs in only one place/table, in its simplest and atomic form and is
not spread throughout the database.
Had the data been spread throughout the database in different tables. We
would have to update the new value of an entity everywhere. This is
troublesome and things can get inconsistent.
ACID Transactions #
Besides normalization & consistency, relational databases also ensure ACID
transactions.
The system will have a new state after the transaction which is durable &
consistent. Or if anything, amiss happens during the transaction, say a minor
system failure, the entire operation is rolled back.
When a transaction happens, there is an initial state of the system State A &
then there is a final state of the system State B after the transaction. Both the
states are consistent and durable.
We have the Controller, then the Service Layer interface, Class implementations of
the interface, the business logic goes in the Object Domain model, a bit in the
Service, Business and the Repository/DAO [Data Access Object] classes.
Monolithic apps are simple to build, test & deploy in comparison to a microservices
architecture.
There are times during the initial stages of the business when teams chose to move
forward with the monolithic architecture & then later intend to branch out into the
distributed, microservices architecture.
Well, this decision has several trade-offs. And there is no standard solution to this.
In the present computing landscape, the applications are being built & deployed on
the cloud. A wise decision would be to pick the loosely coupled stateless
microservices architecture right from the start if you expect things to grow at quite
a pace in the future.
Because re-writing stuff has its costs. Stripping down things in a tightly coupled
architecture & re writing stuff demands a lot of resources & time.
On the flip side, if your requirements are simple why bother writing a
microservices architecture? Running different modules in conjunction with each
other isn’t a walk in the park.
The downside of this is that we need a thorough regression testing of the entire
application after the deployment is done as the layers are tightly coupled with each
other. A change in one layer impacts other layers significantly.
Scalability Issues #
Flexibility and scalability are a challenge in monolith apps as a change in one layer
often needs a change and testing in all the layers. As the code size increases, things
might get a bit tricky to manage.
It’s tricky to use Java & NodeJS together in a single codebase, & when I say tricky, I
am being generous. I am not sure if it’s even possible to do that.
These are the use cases where the business is certain that there won’t be an
exponential growth in the user base and the traffic over time.
There are also instances where the dev teams decide to start with a monolithic
architecture and later scale out to a distributed microservices architecture.
This helps them deal with the complexity of the application step by step as and
when required. This is exactly what LinkedIn did.
In this lesson, we will discuss when to choose a relational database for our project.
If you are writing a stock trading, banking or a Finance-based app or you need
to store a lot of relationships, for instance, when writing a social network like
Facebook. Then you should pick a relational database. Here is why –
Relational DBs shine when it comes to transactions & data consistency. They
comply with the ACID rule, have been around for ages & are battle-tested.
Large Community #
Also, they have a larger community. Seasoned engineers on the tech are easily
available, you don’t have to go too far looking for them.
Storing Relationships #
If your data has a lot of relationships like which friends of yours live in a
particular city? Which of your friend already ate at the restaurant you plan to
visit today? etc. There is nothing better than a relational database for storing
this kind of data.
Relational databases are built to store relationships. They have been tried &
tested & are used by big guns in the industry like Facebook as the main user-
facing database.
In this lesson, we will get an insight into NoSQL databases and how they are different from Relational databases.
They are built for high frequency read & writes, typically required in social
applications like Twitter, LIVE real-time sports apps, online massive multi-
player games etc.
Why the need for NoSQL databases when relational databases were doing
fine, were battle-tested, well adopted by the industry & had no major
persistence issues?
Scalability #
Well, one big limitation with SQL based relational databases is Scalability.
Scaling relational databases is something which is not trivial. They have to be
Sharded or Replicated to make them run smoothly on a cluster. In short, this
requires careful thought and human intervention.
On the contrary, NoSQL databases have the ability to add new server nodes on
the fly & continue the work, without any human intervention, just with a snap
of the fingers.
Today’s websites need fast read writes. There are millions, if not billions of
users connected with each other on social networks.
Clustering #
NoSQL databases are designed to run intelligently on clusters. And when I say
intelligently, I mean with minimal human intervention.
Today, the server nodes even have self-healing capabilities. That’s pretty
smooth. The infrastructure is intelligent enough to self-recover from faults.
Though all this innovation does not mean old school relational databases
aren’t good enough & we don’t need them anymore.
Relational databases still work like a charm & are still in demand. They have a
specific use-case. We have already gone through this in When to pick a
relational database lesson. Remember? 😊
So, this obviously means NoSQL databases aren’t the silver bullet. And it’s
completely alright, we don’t need silver bullets. We aren’t hunting
werewolves, we are upto a much harder task connecting the world online.
I’ll talk about the underlying design of NoSQL databases in much detail and
why they have to sacrifice Strong consistency and Transactions in the
upcoming lessons.
Remember the Single Responsibility & the Separation of Concerns principles? Both
the principles are applied in a microservices architecture.
This particular architecture facilitates easier & cleaner app maintenance, feature
development, testing & deployment in comparison to a monolithic architecture.
And did I bring up scalability? To scale, we need to split things up. We need to
scale out when we can’t scale up further. Microservices architecture is inherently
designed to scale.
Let’s go through some of the pros and cons of using a microservices architecture.
Features Of NoSQL Databases
In the introduction we learned that the NoSQL databases are built to run on
clusters in a distributed environment, powering Web 2.0 websites.
Schemaless #
One needs to be pretty focused when designing the schema of a relational
database to avoid running into any issues in the future.
There are no strict enforced schemas, work with the data as you want. You
can always change stuff, spread things around. Entities have no relationships.
Thus, things are flexible & you can do stuff your way.
Wonderful Right?
Not always!! This flexibility is good and bad at the same time. Being so
flexible, developer-friendly, having no joins and relationships etc. makes it
good.
Failing to do so, makes the entity inconsistent. This is not a problem with
relational databases since they keep the data normalized. An entity resides at
one place only.
An upside I felt was that we don’t have to be a pro in database design to write
an application. Things were comparatively simpler, as there was no stress of
managing joins, relationships, n+1 query issues etc.
Just fetch the data with its Key. You can also call it the id of the entity. This is a
constant O(1) operation, which makes NoSQL Dbs really fast.
It’s alright if we need to make a few extra calls to the backend to fetch data in
separate calls that doesn’t make much of a difference. We can always cache
the frequently accessed data to overcome that.
So, I guess, by now we have a pretty good idea of what NoSQL databases are.
Let’s have a look at some of the use cases which fit best with them.
When Should You Pick A Microservices Architecture?
In this lesson, we will learn about the pros and cons of the Microservice Architecture & when
should we pick it for our project.
Polyglot persistence is using multiple databases types like SQL, NoSQL together in
an architecture. I’ll discuss it in detail in the database lesson.
No Strong Consistency #
Strong consistency is hard to guarantee in a distributed environment. Things are
Eventually consistent across the nodes. And this limitation is due to the distributed
design.
I’ll discuss both Strong and eventual consistency in the database chapter.
Writing every feature in a single codebase would take no time in becoming a mess.
So, by now, in the context of monolithic and microservices, we have gone through
three approaches:
3. Starting with a monolithic architecture and then later scale out into a
microservice architecture.
• Fault Isolation
• Development Team Autonomy
• Segment – From Monolith To Microservices And Back Again To Monolith
Fault Isolation #
When we have a microservices architecture in place it’s easy for us to isolate faults
and debug them. When a glitch occurs in a particular service, we just have to fix
the issue in that service without the need to scan the entire codebase in order to
locate and fix that issue. This is also known as fault isolation.
Even if the service goes down due to the fault, the other services are up & running.
This has a minimal impact on the system.
In this scenario, things become a little tricky to manage. First off, as the size of the
codebase increases, the compile-time & the time required to run the tests increases
too. Since, in a monolith architecture, the entire codebase has to be compiled as
opposed to just compiling the module we work on.
A code change made, in the codebase, by any other team has a direct impact on the
features we develop. It may even break the functionality of our feature. Due to this
a thorough regression testing is required every time anyone pushes new code or
an update to production.
Also, as the code is pushed to production, we need all the teams to stop working on
the codebase until the change is pushed to production.
The code pushed by a certain team may also require approval from other teams in
the organization working on the same codebase. This process is a bottleneck in the
system.
So, if you need to move fast, launch a lot of features quick to the market and scale.
Moving forward with microservices architecture is a good bet.
With the microservices architecture comes along the need to set up distributed
logging, monitoring, inter-service communication, service discovery, alerts, tracing,
build & release pipelines, health checks & so on. You may even have to write a lot of
custom tooling from scratch for yourself.
So, I think you get the idea. There are always trade-offs involved there is no perfect
or the best solution. We need to be crystal on our use case and see what
architecture suits our needs best.
Let’s understand this further with the help of a real-world example of a company
called Segment that started with a monolith architecture, moved to microservices
and then moved back again to the monolith architecture.
Segment From Monolith To Microservices And
Back Again To Monolith #
Segment is a customer data platform that originally started with a monolith and
then later split it into microservices. As their business gained traction, they again
decided to revert to the monolith architecture.
Segment engineering team split their monolith into microservices for fault
isolation & easy debugging of issues in the system.
Fault isolation with microservices helped them minimize the damage a fault
caused in the system. It was confined to a certain service as opposed to impacting,
even bringing down the entire system as a whole.
The original monolith architecture had low management overhead but had a
single point of failure. A glitch in a certain functionality had the potential to impact
the entire system.
Segment integrates data from many different data providers into their system. As
the business gained traction, they integrated more data providers into their system
creating a separate microservice for every data provider. The increase in the
number of microservices led to an increase in the complexity of their architecture
significantly, subsequently taking a toll on their productivity.
To tackle the issue, they made the decision to move back to monolith giving up on
fault isolation and other nice things that the microservices architecture brought
along.
They ended up with an architecture having a single code repository that they
called Centrifuge that handled billions of messages per day delivered to multiple
APIs.
Let’s have a further insight into their architecture in the lesson up next.
When To Pick A NoSQL Database?
In this lesson, we will discover when to choose a NoSQL Database over any other kind of database.
When there are a large number of read-write operations on your website &
when dealing with a large amount of data, NoSQL databases fit best in these
scenarios. Since they have the ability to add nodes on the fly, they can handle
more concurrent traffic & big amount of data with minimal latency.
around the world. Does it matter if the count of likes goes up or down a bit for
a short while?
The celebrity would definitely not care if instead of the actual 5 million 500
likes, the system shows the like count as 5 million 250 for a short while.
Until they reach a consensus, the value of the entity is inconsistent. The value
of the entity eventually gets consistent after a short while. This is what
Eventual Consistency is.
Though the inconsistency does not mean that there is any sort of data loss. It
just means that the data takes a short while to travel across the globe via the
internet cables under the ocean to reach a global consensus and become
consistent.
We experience this behaviour all the time. Especially on YouTube. Often you
would see a video with 10 views and 15 likes. How is this even possible?
It’s not. The actual views are already more than the likes. It’s just the count of
views is inconsistent and takes a short while to get updated. I will discuss
Eventual consistency in more detail further down the course.
There are dedicated databases for use cases like this such as Time-Series
databases, Wide-Column, Document Oriented etc. I’ll talk about each of them
further down the course.
Right now, let’s have an insight into the performance comparison of SQL and
NoSQL tech.
Is NoSQL More Performant than SQL?
In this lesson, we will learn if the NoSQL database is more performant than the SQL databases.
Is NoSQL more performant than SQL? This question is asked all the time. And I
have a one-word answer for this.
No!!
More than the technology, it’s how we design our systems using the
technology that affects the performance.
Both SQL & NoSQL tech have their use cases. We have already gone through
them in the lessons When to pick a relational database? & When to pick a
NoSQL database?
So, don’t get confused with all the hype. Understand your use case and then
pick the technology accordingly.
Well, most of the applications online have common use cases. And these tech
stacks have them covered. There are also commercial reasons behind this.
Now, there are a plethora of tutorials available online & a mass promotion of
popular tech stacks. With these resources, it’s easy for beginners to pick them
up and write their applications as opposed to running solo research on other
technologies.
Though, we don’t always need to stick with the popular stacks. We should pick
what fits best with our use case. There are no ground rules, pick what works
for you.
We have a separate lesson on how to pick the right tech stack for our app
further down the course. We will continue this discussion there.
Coming back to the performance, it entirely depends on the application & the
database design. If we are using more Joins with SQL. The response will
inevitably take more time.
If we remove all the relationships and joins, SQL becomes just like NoSQL.
Quora uses MySQL pretty efficiently by partitioning the data at the application
level. This is an interesting read on it.
The term for leveraging the power of multiple databases is called Polyglot
Persistence. Let’s know more about it in the next lesson.
Monolith & Microservices – Understanding The Trade-
Offs – Part 2
This lesson continues the discussion on the trade-offs involved when choosing between the
monolith and the microservices architecture
When they started with a monolith architecture. They had an API that ingested
events from different sources and those events were then forwarded to a
distributed message queue. The queue based on configuration and settings further
moved the event payload to different destination APIs.
If you aren’t aware of what a message queue, webhook & data ingestion is? No
worries, I’ve discussed these in detail in the latter part of this course. This
example that I am discussing right now is pretty straight forward nothing
complicated, so we can focus on this right now & can delve in detail into the rest
of the concepts later.
In the monolithic architecture, as all the events were moved into a single queue,
some of the events often failed to deliver to the destinations and were retried by
the queue after stipulated time intervals.
This made the queue contain both the new as well as the failed events waiting to
be retried. This eventually flooded the queue resulting in a delay of the delivery of
events to the destinations.
To tackle the queue flooding issue, the engineering team at Segment split the
monolith into microservices and created a separate microservice for every
destination.
Every service contained its own individual distributed message queue. This helped
cut down the load on a single queue & enabled the system to scale also increasing
the throughput.
In this scenario, even if a certain queue got flooded it didn’t impact the event
delivery of other services. This is how Segment leveraged fault isolation with the
microservices architecture.
Over time as the business gained traction additional destinations were added.
Every destination had a separate microservice and a queue. The increase in the
number of services led to an increase in the complexity of the architecture.
Separate services had separate event throughput & traffic load patterns. A single
scale policy couldn’t be applied on all the queues commonly. Every service and the
queue needed to be scaled differently based on its traffic load pattern. And this
process had to be done manually.
Auto-scaling was implemented in the infrastructure but every service had distinct
CPU & memory requirements. This needed manual tuning of the infrastructure.
This meant - more queues needed more resources for maintenance.
The info that I have provided on Segment architecture in this lesson is very high-
level. If you wish to go into more details, want to have a look at the Centrifuge
architecture. Do go through these resources -
1
What is the use of a database in web applications?
COMPLETED 0%
1 of 8
Introduction To Micro Frontends
In this lesson, we will understand what are micro frontends
What does applying the principles of microservices to the front end means?
Microservices provide complete autonomy to the teams developing them. They are
loosely coupled, provide fault isolation also offer the freedom to pick the desired
technology stack to the individual teams to develop a certain service.
Every slice is owned by a dedicated team. The team besides the backend devs also
includes the front-end developers who have the onus of developing the user
interface component only of that particular service.
Every team builds its own user interface component choosing their desired
technology and later all these components are integrated together forming the
complete user interface of the application. This micro frontend approach averts
the need for a dedicated centralized user interface team.
Let’s understand this further with the help of an example.
Alright, let’s imagine an online game store that home delivers the DVDs of all kinds
of video games for both desktops and consoles such as Xbox, Nintendo Switch, Play
Station & the related hardware.
Our online gaming store will have several different UI components. A few key
components out of those are –
The Search Component – This is a search bar on the top centre of the website that
enables the users to search games based on the keywords they enter.
Once the user runs a search the component then enables the user to filter their
search results based on several options such as the price range, type of console,
game genre and so on.
The Game Category Component – This component displays the popular and
widely searched games for different categories on the home page of the website.
Add To Cart & Checkout Component – This user interface component enables the
users to add the games of their liking to the cart and proceed with the checkout
filling in their address & other required information to make the final payment.
During the checkout, the website may recommend related games to the user as
upsells. Also, a user can apply coupons & gift cards if he has any.
The complete user interface of the application is rendered combining all these
different individual UI components, also called Micro Frontends.
Relational Database #
To store relationships like persisting friends of a user, friends of friends, what
rock band they like, what food preferences they have in common etc. we
would pick a relational database like MySQL.
We can use the same Key-value data store to store user sessions.
Now our app is already a big hit, it has got pretty popular and we have
millions of active users.
Graph Database #
Now to enhance the user experience of our application we have to start
recommending content to the users to keep them engaged. A Graph database
would fit best to implement a recommendation system.
Alright, by now, our application has multiple features, & everyone loves it.
How cool it would be if a user can run a search for other users, business pages
and stuff on our portal & could connect with them?
What?
But with the advent of multi-model databases, we have the ability to use
different data models in a single database system.
They also avert the need for managing multiple persistence technologies in a
single service. They reduce the operational complexity by notches. With multi-
model databases, we can leverage different data models via a single API.
Popular Multi-Model Databases #
Some of the popular multi-model databases are Arango DB, Cosmos DB, Orient
DB, Couchbase etc.
So, by now we are clear on what NoSQL databases are & when to pick them
and stuff. Now let’s understand concepts like Eventual Consistency, Strong
Consistency which are key to understanding distributed systems.
Eventual Consistency
So, we have many datastore nodes spread across the world which the micro-
blogging site uses for persisting data.
Since there are so many nodes running, there is no single point of failure. The
data store service is highly available. Even if a few nodes go down the
persistence service as a whole is still up.
Alright, now let’s say a celebrity makes a post on the website which everybody
starts liking around the world.
At a point in time, a user in Japan likes the post which increases the “Like”
count of the post from say 100 to 101. At the same point in time, a user in
America, in a different geographical zone clicks on the post and he sees the
“Like” count as 100, not 101.
Simply, because the new updated value of the Post “Like” counter needs some
time to move from Japan to America and update the server nodes running
there.
Though the value of the counter at that point in time was 101, the user in
America sees the old inconsistent value.
But when he refreshes his web page after a few seconds the “Like” counter
value shows as 101. So, the data was initially inconsistent but eventually got
consistent across the server nodes deployed around the world. This is what
eventual consistency is.
Let’s take it one step further, what if at the same point in time both the users
in Japan and America Like the post, and a user in another geographic zone say
Europe accesses the post.
All the nodes in different geographic zones have different post values. And
they will take some time to reach a consensus.
The upside of eventual consistency is that the system can add new nodes on
the fly without the need to block any of them, the nodes are available to the
end-users to make an update at all times.
Millions of users across the world can update the values at the same time
without having to wait for the system to reach a common final value across all
nodes before they make an update. This feature enables the system to be
highly available.
Eventual consistency is suitable for use cases where the accuracy of values
doesn’t matter much like in the above-discussed use case.
Other use cases of eventual consistency can be when keeping the count of
users watching a Live video stream online. When dealing with massive
amounts of analytics data. A couple of counts up and down won’t matter
much.
But there are use cases where the data has to be laser accurate like in
banking, stock markets. We just cannot have our systems to be Eventually
Consistent, we need Strong Consistency.
• What Is A Database?
• Structured Data
• Unstructured Data
• Semi-structured Data
• User state
What Is A Database? #
A database is a component required to persist data. Data can be of many forms:
structured, unstructured, semi-structured and user state data.
Let’s quickly have an insight into the classification of data before delving into the
databases.
Structured Data #
Structured data is the type of data which conforms to a certain structure, typically
stored in a database in a normalized fashion.
There is no need to run any sort of data preparation logic on structured data
before processing it. Direct interaction can be done with this kind of data.
So, with structured data, we know what we are dealing with. Since the customer
name is of String type, without much worry of errors or exceptions, we can run
String operations on it.
Unstructured Data #
Unstructured data has no definite structure. It is generally the heterogeneous type
of data comprising of text, image files, video, multimedia files, pdfs, Blob objects,
word documents, machine-generated data etc.
This kind of data is often encountered in data analytics. Here the data streams in
from multiple sources such as IoT devices, social networks, web portals, industry
sensors etc. into the analytics systems.
We cannot just directly process unstructured data. The initial data is pretty raw,
we have to make it flow through a data preparation stage which segregates it
based on some business logic & then runs the analytics algorithms on it.
Semi-structured Data #
Semi-structured data is a mix of structured & unstructured data. Semi-structured
data is often stored in data transport formats such as XML or JSON and is handled
as per the business requirements.
User state #
The data containing the user state is the information of activity which the user
performs on the website.
For instance, when browsing through an e-commerce website, the user would
browse through several product categories, change the preferences, add a few
products to the reminder list for the price drops.
All this activity is the user state. So next time, when the user logs in, he can
continue from where he left off. It would not feel like that one is starting fresh &
all the previous activity is lost.
Storing user state improves the browsing experience & the conversion rate for the
business.
So, now we are clear on the different types of data. Let’s have a look into different
types of databases.
There are multiple different types of databases with specific use cases. We’ll
quickly go through each of them in order to have a bird’s eye view of the database
realm.
Relational Database
In this lesson, we will discuss the relational databases.
MySQL is the most popular example of a relational database. Alright!! I get it but
what are relationships?
This is a one to many relationship. In the simplest of forms, one table will contain
the details of all the customers & another table will contain all the products in the
inventory.
One row in the customer table will correspond to multiple rows in the product
inventory table.
On pulling the user object with id C1 from the database we can easily find what
books C1 purchased via the relationship model.
Data Consistency #
Besides, the relationships, relational databases also ensure saving data in a
normalized fashion. In very simple terms, normalized data means a unique entity
occurs in only one place/table, in its simplest and atomic form and is not spread
throughout the database.
Had the data been spread throughout the database in different tables. We would
have to update the new value of an entity everywhere. This is troublesome and
things can get inconsistent.
ACID Transactions #
Besides normalization & consistency, relational databases also ensure ACID
transactions.
When a transaction happens, there is an initial state of the system State A & then
there is a final state of the system State B after the transaction. Both the states are
consistent and durable.
A relational database ensures that either the system is in State A or State B at all
times. There is no middle state. If anything fails, the system goes back to State A.
If the transaction is executed smoothly the system transitions from State A to State
B.
Database Quiz - Part 2
This lesson contains a quiz to test your understanding of different models of databases, eventual, strong
consistency & CAP theorem.
1
What does polyglot persistence mean?
COMPLETED 0%
1 of 5
NoSQL Databases - Introduction
In this lesson, we will get an insight into NoSQL databases and how they are different from
Relational databases.
They are built for high frequency read & writes, typically required in social
applications like Twitter, LIVE real-time sports apps, online massive multi-player
games etc.
Why the need for NoSQL databases when relational databases were doing
fine, were battle-tested, well adopted by the industry & had no major
persistence issues?
Scalability #
Well, one big limitation with SQL based relational databases is Scalability. Scaling
relational databases is something which is not trivial. They have to be Sharded or
Replicated to make them run smoothly on a cluster. In short, this requires careful
thought and human intervention.
On the contrary, NoSQL databases have the ability to add new server nodes on the
fly & continue the work, without any human intervention, just with a snap of the
fingers.
Today’s websites need fast read writes. There are millions, if not billions of users
connected with each other on social networks.
Clustering #
NoSQL databases are designed to run intelligently on clusters. And when I say
intelligently, I mean with minimal human intervention.
Today, the server nodes even have self-healing capabilities. That’s pretty smooth.
The infrastructure is intelligent enough to self-recover from faults.
Though all this innovation does not mean old school relational databases aren’t
good enough & we don’t need them anymore.
Relational databases still work like a charm & are still in demand. They have a
specific use-case. We have already gone through this in When to pick a relational
database lesson. Remember? 😊
Also, NoSQL databases had to sacrifice Strong consistency, ACID Transactions &
much more to scale horizontally over a cluster & across the data centres.
The data with NoSQL databases is more Eventually Consistent as opposed to being
Strongly Consistent.
So, this obviously means NoSQL databases aren’t the silver bullet. And it’s
completely alright, we don’t need silver bullets. We aren’t hunting werewolves, we
are upto a much harder task connecting the world online.
I’ll talk about the underlying design of NoSQL databases in much detail and why
they have to sacrifice Strong consistency and Transactions in the upcoming lessons.
Such as the:
Document-Oriented database
Key-value datastore
Wide-column database
Relational database
Graph database
Time-Series database
Databases dedicated to mobile apps
In the polyglot persistence lesson, we went through the need for different
types of databases. We have also covered relational databases in-depth when
to pick a relational one & stuff in the previous lessons.
Now we will have insights into the other remaining types of databases and the
use cases which fit them.
In this lesson, we will get to know about the Document Oriented database and when to choose it for our projects.
The data model is similar to the data model of our application code, so it’s
easier to store and query data for developers.
Real-time feeds
Live sports apps
Writing product catalogues
Inventory management
Storing user comments
Web-based multiplayer games
Coinbase scaled from 15k requests per min to 1.2 million requests per
minute with MongoDB
Features Of NoSQL Databases
In this lesson, we will discuss the features of NoSQL databases.
In the introduction we learned that the NoSQL databases are built to run on
clusters in a distributed environment, powering Web 2.0 websites.
Schemaless #
One needs to be pretty focused when designing the schema of a relational database
to avoid running into any issues in the future.
Think of relational databases as a strict headmaster. Everything has to be in place,
neat and tidy, things need to be consistent. But NoSQL databases are a bit of chilled
out & relaxed.
There are no strict enforced schemas, work with the data as you want. You can
always change stuff, spread things around. Entities have no relationships. Thus,
things are flexible & you can do stuff your way.
Wonderful Right?
Not always!! This flexibility is good and bad at the same time. Being so flexible,
developer-friendly, having no joins and relationships etc. makes it good.
Failing to do so, makes the entity inconsistent. This is not a problem with relational
databases since they keep the data normalized. An entity resides at one place only.
Conclusion #
My first experience with a NoSQL datastore was with the Google Cloud Datastore.
An upside I felt was that we don’t have to be a pro in database design to write an
application. Things were comparatively simpler, as there was no stress of
managing joins, relationships, n+1 query issues etc.
Just fetch the data with its Key. You can also call it the id of the entity. This is a
constant O(1) operation, which makes NoSQL Dbs really fast.
I have designed a lot of MySQL DB schemas in the past with complex relationships.
And I would say working with a NoSQL database is a lot easier than working with
relationships.
It’s alright if we need to make a few extra calls to the backend to fetch data in
separate calls that doesn’t make much of a difference. We can always cache the
frequently accessed data to overcome that.
So, I guess, by now we have a pretty good idea of what NoSQL databases are. Let’s
have a look at some of the use cases which fit best with them.
Graph Database
In this lesson, we will get to know about the Graph database and when to choose it for our projects
The second reason is the low latency. In graph databases, the relationships are
stored a bit differently from how the relational databases store relationships.
Graph databases are faster as the relationships in them are not calculated at
the query time, as it happens with the help of joins in the relational databases.
Rather the relationships here are persisted in the data store in the form of
edges and we just have to fetch them. No need to run any sort of computation
at the query time.
Now, if I have to look for roads between different cities, I don’t need joins to
figure out the relationship between the cities when I run the query. I just need
to fetch the edges which are already stored in the database.
Graph databases help us visualize our data with minimum latency. A popular
graph database used in the industry is Neo4J.
NASA uses Neo4J to store “lessons learned” data from their previous
missions to educate the scientists & engineers.
When To Pick A NoSQL Database?
In this lesson, we will discover when to choose a NoSQL Database over any other kind of
database.
When there are a large number of read-write operations on your website & when
dealing with a large amount of data, NoSQL databases fit best in these scenarios.
Since they have the ability to add nodes on the fly, they can handle more
concurrent traffic & big amount of data with minimal latency.
A good example of this is a social networking website like Twitter. When a tweet of
a celebrity blows up and everyone is liking and re-tweeting it from around the
world. Does it matter if the count of likes goes up or down a bit for a short while?
The celebrity would definitely not care if instead of the actual 5 million 500 likes,
the system shows the like count as 5 million 250 for a short while.
Until they reach a consensus, the value of the entity is inconsistent. The value of
the entity eventually gets consistent after a short while. This is what Eventual
Consistency is.
Though the inconsistency does not mean that there is any sort of data loss. It just
means that the data takes a short while to travel across the globe via the internet
cables under the ocean to reach a global consensus and become consistent.
We experience this behaviour all the time. Especially on YouTube. Often you would
see a video with 10 views and 15 likes. How is this even possible?
It’s not. The actual views are already more than the likes. It’s just the count of
views is inconsistent and takes a short while to get updated. I will discuss Eventual
consistency in more detail further down the course.
There are dedicated databases for use cases like this such as Time-Series databases,
Wide-Column, Document Oriented etc. I’ll talk about each of them further down the
course.
Right now, let’s have an insight into the performance comparison of SQL and
NoSQL tech.
Is NoSQL More Performant than SQL?
In this lesson, we will learn if the NoSQL database is more performant than the SQL databases.
Is NoSQL more performant than SQL? This question is asked all the time. And I
have a one-word answer for this.
No!!
More than the technology, it’s how we design our systems using the technology that
affects the performance.
Both SQL & NoSQL tech have their use cases. We have already gone through them
in the lessons When to pick a relational database? & When to pick a NoSQL
database?
So, don’t get confused with all the hype. Understand your use case and then pick
the technology accordingly.
Well, most of the applications online have common use cases. And these tech
stacks have them covered. There are also commercial reasons behind this.
Now, there are a plethora of tutorials available online & a mass promotion of
popular tech stacks. With these resources, it’s easy for beginners to pick them up
Though, we don’t always need to stick with the popular stacks. We should pick
what fits best with our use case. There are no ground rules, pick what works for
you.
We have a separate lesson on how to pick the right tech stack for our app further
down the course. We will continue this discussion there.
Coming back to the performance, it entirely depends on the application & the
database design. If we are using more Joins with SQL. The response will inevitably
take more time.
If we remove all the relationships and joins, SQL becomes just like NoSQL.
Quora uses MySQL pretty efficiently by partitioning the data at the application
level. This is an interesting read on it.
Note: A well-designed SQL data store will always be more performant than a
not so well-designed NoSQL store.
You can!! As a matter of fact, all the large-scale online services use a mix of both to
implement their systems and achieve the desired behaviour.
The term for leveraging the power of multiple databases is called Polyglot
Persistence. Let’s know more about it in the next lesson.
Key Value Database
In this lesson, we will get to know about the Key-Value database and when to choose it for our projects.
The Key serves as a unique identifier and has a value associated with it. The
value can be as simple as a block of text & can be as complex as an object
graph.
The data in Key-value databases can be fetched in constant time O(1), there is
no query language required to fetch the data. It’s just a simple no-brainer
fetch operation. This ensures minimum latency.
Key-value stores are pretty efficient in pulling off scenarios where super-fast
data fetch is the order of the day.
Caching
Persisting user state
Persisting user sessions
Managing real-time data
Implementing queues
Creating leaderboards in online games & web apps
Implementing a pub-sub system
1
What is the use of a database in web applications?
2
What is unstructured data?
3 Why do web applications store user state?
4
What are the key features of relational databases? Which of the following
option(s) are correct?
5
Why are database ACID transactions so important for financial systems?
6
When should we choose a relational database for our application? Which of
the following option(s) are correct?
7
Why do we need NoSQL databases when already have relational databases?
8
When should we pick a NoSQL database for our application?
Retake Quiz
Polyglot Persistence
In this lesson, we will understand what is meant by Polyglot Persistence.
Relational Database #
To store relationships like persisting friends of a user, friends of friends, what rock
band they like, what food preferences they have in common etc. we would pick a
relational database like MySQL.
Key Value Store #
For low latency access of all the frequently accessed data, we will implement a
cache using a Key-value store like Redis or Memcache.
We can use the same Key-value data store to store user sessions.
Now our app is already a big hit, it has got pretty popular and we have millions of
active users.
Graph Database #
Now to enhance the user experience of our application we have to start
recommending content to the users to keep them engaged. A Graph database
would fit best to implement a recommendation system.
Alright, by now, our application has multiple features, & everyone loves it. How
cool it would be if a user can run a search for other users, business pages and stuff
on our portal & could connect with them?
A lot of effort goes into building, managing and monitoring polyglot persistence
systems. What if there was something simpler? That would save us the pain of
putting together everything ourselves. Well, there is.
What?
This lesson contains a quiz to test your understanding of different types of databases.
1
What are the use cases for a document-oriented database? Which of the
following option(s) are correct?
COMPLETED 0%
1 of 5
Multi-Model Databases
In this lesson, we will talk about the multi-model databases.
But with the advent of multi-model databases, we have the ability to use different
data models in a single database system.
They also avert the need for managing multiple persistence technologies in a single
service. They reduce the operational complexity by notches. With multi-model
databases, we can leverage different data models via a single API.
Popular Multi-Model Databases #
Some of the popular multi-model databases are Arango DB, Cosmos DB, Orient DB,
Couchbase etc.
So, by now we are clear on what NoSQL databases are & when to pick them and
stuff. Now let’s understand concepts like Eventual Consistency, Strong Consistency
which are key to understanding distributed systems.
Eventual Consistency
In this lesson, we will discuss Eventual Consistency.
So, we have many datastore nodes spread across the world which the micro-
blogging site uses for persisting data.
Since there are so many nodes running, there is no single point of failure. The data
store service is highly available. Even if a few nodes go down the persistence
service as a whole is still up.
Alright, now let’s say a celebrity makes a post on the website which everybody
starts liking around the world.
At a point in time, a user in Japan likes the post which increases the “Like” count of
the post from say 100 to 101. At the same point in time, a user in America, in a
different geographical zone clicks on the post and he sees the “Like” count as 100,
not 101.
Simply, because the new updated value of the Post “Like” counter needs some time
to move from Japan to America and update the server nodes running there.
Though the value of the counter at that point in time was 101, the user in America
sees the old inconsistent value.
But when he refreshes his web page after a few seconds the “Like” counter value
shows as 101. So, the data was initially inconsistent but eventually got consistent
across the server nodes deployed around the world. This is what eventual
consistency is.
Let’s take it one step further, what if at the same point in time both the users in
Japan and America Like the post, and a user in another geographic zone say
Europe accesses the post.
All the nodes in different geographic zones have different post values. And they
will take some time to reach a consensus.
The upside of eventual consistency is that the system can add new nodes on the fly
without the need to block any of them, the nodes are available to the end-users to
Millions of users across the world can update the values at the same time without
having to wait for the system to reach a common final value across all nodes
before they make an update. This feature enables the system to be highly available.
Eventual consistency is suitable for use cases where the accuracy of values doesn’t
matter much like in the above-discussed use case.
Other use cases of eventual consistency can be when keeping the count of users
watching a Live video stream online. When dealing with massive amounts of
analytics data. A couple of counts up and down won’t matter much.
But there are use cases where the data has to be laser accurate like in banking,
stock markets. We just cannot have our systems to be Eventually Consistent, we
need Strong Consistency.
In this lesson, we will get introduced to the concept of caching and why it is important for performance.
• What Is Caching?
• Caching Dynamic Data
• Caching Static Data
Hmmm… before beginning with this lesson, I want to ask you a question.
When you visit a website and request certain data from the server. How long
do you wait for the response?
And then you finally bounce off & visit another website for your answer. We
are impatient creatures; we need our answers quick. This makes caching vital
to applications to prevent users from bouncing off to other websites, all the
time.
What Is Caching? #
In the next lesson, let’s understand how do we figure if we really need a cache
in our applications?
How Do I figure If I Need A Cache In My Application?
In this lesson, we will discuss how to tell if we need caching in our application.
First up, it’s always a good idea to use a cache as opposed to not using it. It
doesn’t do any harm. It can be used at any layer of the application & there are
no ground rules as to where it can and cannot be applied.
The cache then returns all the frequently accessed data. Thus, cutting down
the load on the database by notches.
Besides these places, I would suggest you to look for patterns. We can always
cache the frequently accessed content on our website, be it from any
component. There is no need to compute stuff over and over when it can be
cached.
Think of Joins in relational databases. They are notorious for making the
response slow. More Joins means more latency. A cache can avert the need for
running joins every time just by storing the data in demand. Now imagine
how much would this mechanism speed up our application.
Also, even if the database goes down for a while. The users won’t notice it as
the cache would continue to serve the data requests.
Caching is also the core of the HTTP protocol. This is a good resource to read
more about it.
Key-value data stores via caching are also widely used in in-memory data
stream processing and running analytics.
Strong Consistency
In this lesson, we will discuss Strong Consistency.
This means at one point in time, only one user can update the post “Like” counter
value.
So, once the user in Japan updates the “Like” counter from 100 to 101. The value
gets replicated globally across all nodes. Once all the nodes reach a consensus, the
locks get lifted.
Now, other users can Like the post. If the nodes take a while to reach a consensus,
they have to wait until then.
Well, this is surely not desired in case of social applications. But think of a stock
market application where the users are seeing different prices of the same stock at
one point in time and updating it concurrently. This would create chaos.
Queuing all the requests is one good way of making a system Strongly Consistent.
Well, the implementation is beyond the scope of this course. Though we will
discuss a theorem called the CAP theorem which is key to implementing these
consistency models.
So, by now I am sure you would have realized that picking the Strong Consistency
model hits the capability of the system to be Highly Available.
The system while being updated by one user does not allow other users to perform
concurrent updates. This is how strongly consistent ACID transactions are
implemented.
So, NoSQL databases don’t support ACID transactions and those that claim to, have
terms and conditions applied to them.
Well, this is pretty much about Strong Consistency. Now let’s take a look into the
CAP Theorem