Friday, September 23, 2022

Scaling : Vertical and Horizontal Scaling

 Given architecture is an example of a client-server based system. In this, there is a client who sends requests to the server and then the client receives a response from the server accordingly but when the number of users/clients increases, the load on the server increases enormously which makes it difficult for the server to perform efficiently and hence becomes slow. Therefore, it is important to make the server scalable in a way such that the server capacity increases according to the increasing traffic without any sort of failure.

Scaling

It can be defined as a process to expand the existing configuration (servers/computers) to handle a large number of user requests or to manage the amount of load on the server. This process is called scalability. This can be done either by increasing the current system configuration (increasing RAM, number of servers) or adding more power to the configuration. Scalability plays a vital role in the designing of a system as it helps in responding to a large number of user requests more effectively and quickly.

There are two ways to do this : 

  1. Vertical Scaling 
  2. Horizontal Scaling 

Vertical Scaling

It is defined as the process of increasing the capacity of a single machine by adding more resources such as memory, storage, etc. to increase the throughput of the system. No new resource is added, rather the capability of the existing resources is made more efficient. This is called Vertical scaling. Vertical Scaling is also called the Scale-up approach. 

Example: MySQL 

Advantages of Vertical Scaling

It is easy to implement

Reduced software costs as no new resources are added

Fewer efforts required to maintain this single system

Disadvantages of Vertical Scaling

Single-point failure

Since when the system (server) fails, the downtime is high because we only have a single server

High risk of hardware failures

A Real-time Example of Vertical Scaling

When traffic increases, the server degrades in performance. The first possible solution that everyone has is to increase the power of their system. For instance, if earlier they used 8 GB RAM and 128 GB hard drive now with increasing traffic, the power of the system is affected. So a possible solution is to increase the existing RAM or hard drive storage, for e.g. the resources could be increased to 16 GB of RAM and 500 GB of a hard drive but this is not an ultimate solution as after a point of time, these capacities will reach a saturation point. 

Horizontal Scaling

It is defined as the process of adding more instances of the same type to the existing pool of resources and not increasing the capacity of existing resources like in vertical scaling. This kind of scaling also helps in decreasing the load on the server. This is called Horizontal Scaling 

Horizontal Scaling is also called the Scale-out approach. 

In this process, the number of servers is increased and not the individual capacity of the server. This is done with the help of a Load Balancer which basically routes the user requests to different servers according to the availability of the server. Thereby, increasing the overall performance of the system. In this way, the entire process is distributed among all servers rather than just depending on a single server. 

Example: NoSQL, Cassandra, and MongoDB 

Advantages of Horizontal Scaling

Fault Tolerance means that there is no single point of failure in this kind of scale because there are 5 servers here instead of 1 powerful server. So if anyone of the servers fails then there will be other servers for backup. Whereas, in Vertical Scaling there is single point failure i.e: if a server fails then the whole service is stopped.

Low Latency: Latency refers to how late or delayed our request is being processed.

Thursday, September 1, 2022

Daemon Threads and Non Daemon Thread (User Threads)

 In Java, there are two types of threads:

  • Daemon Thread
  • User Thread

Daemon threads are low priority threads which always run in background and user threads are high priority threads which always run in foreground. User Thread or Non-Daemon are designed to do specific or complex task where as daemon threads are used to perform supporting tasks.

Difference Between Daemon Threads And User Threads In Java

JVM doesn’t wait for daemon thread to finish but it waits for User Thread : First and foremost difference between daemon and user threads is that JVM will not wait for daemon thread to finish its task but it will wait for any active user thread.

For example, one might have noticed this behavior while running Java program in NetBeans that even if the main thread has finished, the top left down button is still red, showing that Java program is still running. This is due to any user thread spawned from the main thread, but with main thread one don’t see that red dot in NetBeans.

Thread Priority : The User threads are high priority as compare to daemon thread means they won’t get CPU as easily as a user thread can get.

Creation of Thread : User thread is usually created by the application for executing some task concurrently. On the other hand, daemon thread is mostly created by JVM like for some garbage collection job.

Termination of Thread : JVM will force daemon thread to terminate if all user threads have finished their execution but The user thread is closed by application or by itself. A user thread can keep running by the JVM running but a daemon thread cannot keep running by the JVM. This is the most critical difference between user thread and daemon thread.

Usage : The daemons threads are not used for any critical task. Any important task is done by user thread. A daemon thread is generally used for some background tasks which are not critical task.

The Major Difference between User and Daemon Threads:

User ThreadDaemon Thread
JVM wait until user threads to finish their work. It never exit until all user threads finish their work.The JVM will’t wait for daemon threads to finish their work. The JVM will exit as soon as all user threads finish their work.
JVM will not force to user threads for terminating, so JVM will wait for user threads to terminate themselves.If all user threads have finished their work JVM will force the daemon threads to terminate
User threads are created by the application.Mostly Daemon threads created by the JVM.
Mainly user threads are designed to do some specific task.Daemon threads are design as to support the user threads.
User threads are foreground threads.Daemon threads are background threads.
User threads are high priority threads.Daemon threads are low priority threads.
Its life independent.Its life depends on user threads.

Thread

Native Thread Demon Thread Non-Demon Thread Native Thread: - Any Method/Thread which is mapped to OS is called Native Thread or Method. Demo...