Posts

Showing posts from April, 2022

Trie Data Structure

Image
Trie is an efficient information reTrieval data structure. Using Trie, search complexities can be brought to optimal limit (key length). If we store keys in binary search tree, a well balanced BST will need time proportional to M * log N, where M is maximum string length and N is number of keys in tree. Using Trie, we can search the key in O(M) time. However the penalty is on Trie storage requirements. Every node of Trie consists of multiple branches. Each branch represents a possible character of keys. We need to mark the last node of every key as end of word node. A Trie node field isEndOfWord is used to distinguish the node as end of word node. A simple structure to represent nodes of the English alphabet can be as following,  // Trie node  struct TrieNode  {       struct TrieNode *children[ALPHABET_SIZE];      // isEndOfWord is true if the node       // represents end of a word       bool isEndOfWord;  };  Inserting a key into Trie is a simple approach. Every character of the inpu

AWS Fundamentals

Image
What is Cloud Computing? Cloud computing is the on-demand delivery of compute power, database storage, applications, and other IT resources through a cloud services platform via the internet with pay-as-you-go pricing. AWS Shared Responsibility Model AWS responsibility “Security of the Cloud” - AWS is responsible for protecting the infrastructure that runs all of the services offered in the AWS Cloud. This infrastructure is composed of the hardware, software, networking, and facilities that run AWS Cloud services. Customer responsibility “Security in the Cloud” - Customer is responsible for everything that goes IN the cloud (data, encryption, IAM, software, etc.) It is crucially important to understand which components and parts of AWS are your responsibility for each service. For example, with EC2 you maintain control over the OS, whereas with RDS, AWS is responsible for the OS. The Shared Responsibility model will show up heavily on the exam. Additional details are available on the A

GIT Basic Commands

 git version git config --global user.name "Abe Lincoln" git config --global user.email "mrabe@git.training" git config --global --list git clone github-https-url # paste in your GitHub HTTPS clone URL ls cd github-demo ls git status echo "Test Git Quick Start demo" >> start.txt ls cat start.txt git status git add start.txt git status git commit -m "Adding start text file" git status git push origin master

Distributed Tracing in Micoservices using Zipkin, Sleuth and ELK Stack.

Image
  What is Distributed Tracing ? One of the major challenges in microservices is the ability to debug issues and monitor them. A simple action can trigger a chain of microservice calls and it would be tedious to trace these actions across the invoked microservices. This is because each microservice runs in an environment isolated from other microservices so they don’t share resources such as databases or log files. In addition to that, we might also want to track down why a certain microservice call is taking so much time in a given business flow. The Distributed Tracing pattern addresses the above challenges developers face while building microservices. There are some helpful open-source tools that can be used for distributed tracing, when creating microservices with Spring Boot and Spring Cloud frameworks. This blog walks through the installation steps and implementations of these tools. Spring Cloud Sleuth: A Spring Cloud library that lets you track the progress of subsequent microse

Orchestration vs Choreography

Image
 Microservices architecture — a software design paradigm in which an application and business use case is broken up into a set of composable services — promise many technical benefits to enterprise organizations. First, they’re small, lightweight, and easy to implement. Second, they enable reusability that reduces the cost of developing or changing applications, ensures the efficient use of resources, and makes it easy to scale applications on demand. At a high level, there are two approaches to getting microservices to work together toward a common goal: orchestration and choreography. Orchestration entails actively controlling all elements and interactions like a conductor directs the musicians of an orchestra, while choreography entails establishing a pattern or routine that microservices follow as the music plays, without requiring supervision and instructions. How Orchestration Can Kill Microservices and Create a Distributed Monolith In an orchestra, each musician is awaiting comm

Deadlock Program

public class TestThread { public static Object Lock1 = new Object(); public static Object Lock2 = new Object(); public static void main(String args[]) { ThreadDemo1 T1 = new ThreadDemo1(); ThreadDemo2 T2 = new ThreadDemo2(); T1.start(); T2.start(); } private static class ThreadDemo1 extends Thread { public void run() { synchronized (Lock1) { System.out.println("Thread 1: Holding lock 1..."); try { Thread.sleep(10); } catch (InterruptedException e) {} System.out.println("Thread 1: Waiting for lock 2..."); synchronized (Lock2) { System.out.println("Thread 1: Holding lock 1 & 2..."); } } } } private static class ThreadDemo2 extends Thread { public void run() { synchronized (Lock2) { System.out.println("Thread 2: Holding lock 2...&q

Can we have more than 1 SpringBootAppliction Class

Hibernate N+1 Problem

Image
The N+1 query problem is said to occur when an ORM, like hibernate, executes 1 query to retrieve the parent entity and N queries to retrieve the child entities. As the number of entities in the database increases, the queries being executed separately can easily affect the performance of the application. The relation between user_details and address is a one-many mapping from user_details(id) to address(user_id). That means a user can have many addresses. Public class UserDetails{   --- -.... @OneToMany(cascade = CascadeType.ALL, mappedBy = "userDetails") private List<Address> addresses; } Public class Address { --- ..... @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name = "user_id", referencedColumnName = "id") private UserDetails userDetails; } Now let us query for the name contains on user_details table with repository function. @Repository public interface UserDetailsRepository extends JpaRepository<UserDet

Junit Mockito and Power Mockito

Mock vs Spy Both can be used to mock methods or fields. The difference is that  in mock, you are creating a complete mock or fake object while in spy, there is the real object and you just spying or stubbing specific methods of it . Mock vs InjectMock @Mock  creates a mock.  @InjectMocks  creates an instance of the class and injects the mocks that are created with the  @Mock  (or  @Spy ) annotations into this instance. Unit Testing Void Methods with Mockito and JUnit How to Test Void Methods  As we already know that our aim is to test void methods in a class. But it is also really important to understand why we test void methods.  Whenever we write unit test cases for any method, we expect a return value from the method. Generally, we use assert for checking if the method returns the value that we expect it to return, but in the case of void methods, they do not return any value. So how do we check if our method is functioning properly? Let’s see using an example: In this example, we a