Select Page

# Data Structures and Algorithms

## Data Structures

The programmatic way of storing data, so that it can be used efficiently. Almost every enterprise application uses various types of data structures in one or the other way.

## Algorithms

Algorithms is a step-by-step   e programmatic way of storing data, so that it can be used efficiently. Almost every enterprise application uses various types of data structures in one or the other way.

### Why do we need to learn Data Structures and Algorithms?

Every day the complexity of applications are increasing which result in more complex and rich sets of data, due to the complexity we generally face the following 3 types of problems every day.

#### Problems faced by the large enterprise or corporate (service and data):

1. Data Searching: Consider users registered in social app is more than 1 million, if we want to search for one person in that 1 million, the search operation need to be performed on that 1 million user records. Thus the search operation slows down every time new user data is stored.
2. Processor capacity/speed: Capacity of processor need to be very high to handle such large sets of data. Failing to do so will cause poor performance and bad experience to the users.
3. Too-many requests: Everyone hold PC/Laptop, Mobile devices and IoT gadgets which connects to the service to perform some operation on the data. Thus the number of service request sent and received by the devices and the server are way many. Even the fastest server might fail to respond sometimes.

#### How to solve the above problems?

To solve the above 3 problems (Data Searching, Processor capacity/speed & Too-many requests) we can use the Data Structures.

Data can be organized in a way that data is categorized and sorted before searching, thus allowing us to search instantly.

### Exactly what is that we are going to do with Data structures and algorithms?

As we already know the Algorithms is step-by-step procedure for performing an operation. And that operations are used for manipulating the data. Let’s us see the operations or the categories of algorithms.

#### Categories of algorithms:

• Search
• Sort
• Insert
• Update
• Delete

In the next article in details about the algorithms and the data structures implementation with example.

LIKE | SHARE | FOLLOW

#### WeCanCode-Author

January 08, 2022

Senior Developer | Java & C#.NET | 10++ years of IT experience.

Planning to learn ReactJS or Angular or Flutter.!

# What are the Saga design pattern and types available?

Today we will see what are the Saga Design Pattern and types available.

Previously we saw about What is Microservice? and the principles followed while developing MSA and the available design patterns.

## What are the Saga design pattern and types available?

The Saga Pattern is a microservices architectural pattern to implement a transaction that extends multiple services.

A saga is a sequence of local transactions. Each service in a saga performs its own transaction and publishes an event. The other services listen to that event and perform the next local transaction. If one transaction fails for some reason, the saga also executes compensating transactions to undo the impact of the preceding transactions.

##### Example:

Consider a Food delivery service flow, When a user places an order, below could be the flow.

1. The food ordering service creates an order. At this point, the order is in PENDING status.
2. With the Saga pattern, it contacts the restaurant via the restaurant service.
3. The restaurant service attempts to place an order with the selected restaurant, Once the order is received by Restaurant it sends back an ORDER_ACCEPTED (if the order is accepted) or ORDER_REJECTED (if the order cannot be accepted) status.
4. The saga received the status ORDER_ACCEPTED or ORDER_REJECTED status and process the response to the customer accordingly.
5. The food order service then changes the status of the order, if the order is accepted then it displays an order confirmation message if the order is rejected it display an apologies message and suggest alternative restaurants.

### Types of Saga:

1. Orchestration-based saga
2. Choreography-based saga

In the next article, we will see in detail about the Type of Saga design patterns.

LIKE | SHARE | SUBSCRIBE

#### WeCanCode-Author

December 07, 2021

Senior Developer | Java & C#.NET | 10++ years of IT experience.

Planning to learn ReactJS or Angular or Flutter.!

# What is the Circuit breaker design pattern?

Today we will see what is the Circuit breaker Design Pattern is.

Previously we saw about What is Microservice? and the principles followed while developing MSA and the available design patterns.

## Circuit breaker design pattern?

As the name suggests the Circuit breaker design pattern is used to stop the process of request and return with the response if a service is not working.

This design pattern can improve the stability and flexibility of an application.

In a distributed environment, calls to remote services and resources can fail due to transient faults, such as slow network connection, timeouts, or the resource being over-committed or temporarily unavailable. These faults typically fix themselves after a short period of time.

However there can also be situations where faults are due to unforeseen events, that might take longer to fix. These faults vary from partial loss of connectivity to complete failure of a service. In these situations, it might be pointless for an application to keep retrying an operation that is unlikely to succeed, instead, the application should accept that the operation has failed and handled this failure accordingly.

A circuit breaker pattern can prevent an application from repeatedly trying to execute an operation that’s likely to fail. Allowing it to continue without waiting for the fault to be fixed or the utilization of resources does not kill the CPU and memory.

It also enables an application to detect whether the fault has been resolved.

### How does the Circuit breaker work?

A client should invoke a remote service via a proxy. When the number of consecutive failures crosses a threshold, the circuit breaker trips, and for the duration of a timeout period all attempts to invoke the remote service will fail immediately. After the timeout expires the circuit breaker allows a limited number of test requests to pass through. If those requests succeed the circuit breaker resumes normal operation. Otherwise, if there is a failure the timeout period begins again.

Circuit breaker design pattern

Example:

Consider we have Video Upload and View service when there is a failure in one of the services which triggers the Circuit open and enables the proxy service.

In the next article, we will see the Decomposition design pattern.

LIKE | SHARE | SUBSCRIBE

#### WeCanCode-Author

November 30, 2021

Senior Developer | Java & C#.NET | 10++ years of IT experience.

Planning to learn ReactJS or Angular or Flutter.!

# What is Branch or Branching design pattern?

Today we will see what is an What is Branch or Branching Design Pattern is.

Previously we saw about What is Microservice? and the principles followed while developing MSA and the available design patterns. also, we saw the Shared database design pattern.

## Branch or Branching design pattern?

Branch Design pattern extends the Aggregator design pattern and allows us to perform simultaneous response to be processed from two likely mutual exclusive chained services .

This pattern also be called as different chain or Single chain based on the business needs. This design pattern is also known as Branching design pattern.

Example:

Consider the client is booking the Cab. The Trip Service makes call to the Dispatcher service.

Dispatcher service then makes two calls to Passenger & Driver Service, Then these two service calls Notification service.

Business Flow starts like the Client books a Trip, Trip dispatches the request to driver and passenger, then Both Passenger and Driver are notified using the Notification service.

In the next article, we will see the CQRS (Command query responsibility segregator) design pattern.

LIKE | SHARE | SUBSCRIBE

#### WeCanCode-Author

November 23, 2021

Senior Developer | Java & C#.NET | 10++ years of IT experience.

Planning to learn ReactJS or Angular or Flutter.!

# Design Pattern?

Today we will see what is an Event sourcing Design Pattern is.

Previously we saw about What is Microservice? and the principles followed while developing MSA and the available design patterns. also, we saw the Shared database design pattern.

Before understanding what is Event sourcing design pattern is, let us see why & when do we use the Event sourcing design pattern.

All application uses data to maintain the current state of the data by updating it, In the traditional Create, Read, Update & Delete (CRUD) model a data is read, then make some modification to It, and update the current state of the data with the new values – often by using transactions to lock the data.

### CRUD has its own limitations, below are a few such limitations.

1. CRUD performs update operations directly against the data, which slowdowns the performance and limits the scalability.
2. When multiple users read and updates the data, more likely conflict occurs since the update operations take place on single data.
3. Unless there is a separate auditing mechanism that records the state of data as history or the state of the data is lost.

Now, this is why & when we need to use an Event sourcing design pattern.

### Event sourcing design pattern:

Event Sourcing gives us a new way of persisting application state as an ordered sequence of events. We can selectively query these events and reconstruct the state of the application at any point in time. Of course, to make this work, we need to reimage every change to the state of the application as events:

Event sourcing design pattern

Example:

Consider we have Order and Cart services, When the Client adds the item to the cart or remove the items from the cart, these are called events, These events must be stored in order to maintain the changes made to the cart.

The events are persisted and published into the Event queue. Where the other services will subscribe to those events to get the updated state of the cart.

Consider you add the item to cart from Mobile App, then you login to website via PC and make some changes to the cart, these are captured as events and maintaining the seamless experience when accessing the cart in different platform.

In the next article, we will see the Branch design pattern.

LIKE | SHARE | SUBSCRIBE

#### WeCanCode-Author

November 18, 2021

Senior Developer | Java & C#.NET | 10++ years of IT experience.

Planning to learn ReactJS or Angular or Flutter.!

# Design Pattern

Today we will see what is Shared Database Design Pattern is.

Previously we saw about What is Microservice? and the principles followed while developing MSA and the available design patterns. also, we saw Asynchronous Messaging Design pattern.

## What is Shared Database Design Pattern?

For every web service application, there is a massive amount of data present. So when the application is broken down from monolithic to microservice, It is necessary that each application have a sufficient amount of data to process the request.

So, we can have a database for each service or it can have a shared database.

Yes, we can use a database per service or shared database to solve many problems.

#### Below are a few of the problems,

• Data duplication and inconsistency
• Different application has different storage requirements and different infrastructure.
• Few transactions can query the data with multiple services.
• De-normalization of data

In order to solve the first 3 problems, we can go for database per service, as it will be accessed by the microservice API itself.

So each web service will have its own databased ID, which the other services do not have direct access to.

📌 Note: Limit the number of databases to 2-3 for the microservices; else, scaling up will be a problem.

Shared Database Design Pattern: Example

#### Example:

###### Example1: Partially having a shared database for the microservices.

In the above image, If you see the Contact & Student services shares a database but whereas Fees has its database. (While designing the MSA for the application we can decide about how our application can utilize the database).

###### Example 2: Partially having a shared database for the microservices.

In the next example, if you see the image, the entire microservices uses the one common shared database. this way the database is one and multiple services use it to persist or read data.

In the next article, we will see Asynchronous Messaging design pattern.

LIKE | SHARE | SUBSCRIBE

#### WeCanCode-Author

November 14, 2021

Senior Developer | Java & C#.NET | 10++ years of IT experience.

Planning to learn ReactJS or Angular or Flutter.!