{{theTime}}

Search This Blog

Total Pageviews

Saga Design Pattern

The Saga design pattern is a way to manage distributed transactions in a microservices architecture. In a microservices environment, where different services communicate and perform actions independently, ensuring data consistency and maintaining the integrity of a distributed transaction can be challenging. The Saga pattern helps address this problem by breaking down a distributed transaction into a series of smaller, individual transactions that can be independently managed and rolled back if necessary.

Here's how the Saga pattern works:

Orchestration: A central component called the "Saga Orchestrator" or "Saga Coordinator" is responsible for managing the entire saga. It coordinates the sequence of individual service transactions and ensures that they are executed in the correct order.

Service Transactions: Each microservice involved in the distributed transaction is responsible for executing its part of the transaction. These individual transactions can involve database updates, message publishing, or any other operation specific to the service.

Compensating Transactions: To handle failures or rollbacks, each service defines a corresponding compensating transaction. A compensating transaction is designed to undo the effects of the original transaction. If a failure occurs at any point in the saga, the Saga Orchestrator invokes the compensating transactions for the services that have already executed.

Choreography or Orchestration: Sagas can be implemented using either choreography or orchestration.

Orchestration: In this approach, the Saga Orchestrator explicitly defines the order of service transactions and manages the execution and compensation of each step. It is a more centralized approach.

Choreography: In choreography, services themselves communicate with each other to determine the sequence and coordination of transactions. Services may publish events when their transactions are complete, and other services listen to these events to decide when to start their transactions. It is a more decentralized approach.

Compensation for Failures: If any service transaction in the saga fails, the Saga Orchestrator triggers the compensating transactions for previously executed services. These compensating transactions should be designed to undo any changes made by the failed transaction and restore the system to a consistent state.

Idempotence: To ensure that the compensation process can be safely executed multiple times without causing further issues, both the service transactions and the compensating transactions should be designed to be idempotent.

The Saga pattern allows for better fault tolerance and ensures that, even in the event of failures, the system can eventually reach a consistent state. It provides an alternative to the traditional two-phase commit (2PC) protocol, which can be more rigid and prone to blocking issues in a distributed environment.

Kubernetes Beginners Commands

Kubernetes Concepts:

  • Pod: The smallest deployable unit in Kubernetes. A pod can contain one or more containers.
  • Node: A physical or virtual machine in the cluster where pods are scheduled.
  • Deployment: A higher-level resource for managing pods. It provides updates and scaling capabilities.
  • Service: An abstraction to access a set of pods. There are different types, including ClusterIP, NodePort, and LoadBalancer.
  • Namespace: A way to partition resources in a cluster. Useful for multi-tenancy.
  • ReplicaSet: Ensures a specified number of pod replicas are running at all times.

Kubernetes Commands:

  • kubectl get pods: List all pods in the current namespace.
  • kubectl get nodes: List all nodes in the cluster.
  • kubectl get deployments: List all deployments.
  • kubectl get services: List all services.
  • kubectl get namespaces: List all namespaces.
  • kubectl describe pod <pod-name>: Get detailed information about a pod.
  • kubectl logs <pod-name>: Show logs for a specific pod.
  • kubectl exec -it <pod-name> -- /bin/sh: Start an interactive shell in a pod.
  • kubectl apply -f <filename>: Create or update resources from a YAML file.
  • kubectl delete pod <pod-name>: Delete a pod.
  • kubectl scale deployment <deployment-name> --replicas=<replica-count>: Scale the number of replicas in a deployment.

Pod Management:

  • kubectl create -f <filename>: Create a pod from a YAML file.
  • kubectl expose pod <pod-name> --type=NodePort --port=80: Expose a pod as a service on a specific port.
  • kubectl label pods <pod-name> <label-key>=<label-value>: Add labels to pods for easier management.
  • kubectl run <pod-name> --image=<image-name>: Create a pod with a specified container image.
  • kubectl delete pod <pod-name>: Delete a pod.

Configuration:

  • kubectl config view: View the current Kubernetes configuration.
  • kubectl config use-context <context-name>: Switch to a different context (cluster).
  • kubectl config set-context <context-name> --namespace=<namespace>: Set the default namespace for a context.

Troubleshooting:

  • kubectl describe pod <pod-name>: Get detailed information about a pod, including events.
  • kubectl logs <pod-name>: Show container logs for a specific pod.
  • kubectl exec -it <pod-name> -- /bin/sh: Start an interactive shell in a pod for troubleshooting.

Java Script GET request Sample Code

fetch('https://api.public.com/api')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));

ImmutableList Example

private ImmutableList<String> getServerProps(){
      ImmutableList.Builder<String> serverProps = ImmutableList.builder();
      serverProps.add(String.format("Azure", "Yes"));
      serverProps.add(String.format("AWS", "Yes"));
      serverProps.add("DisasterRecovery", "Yes");
   
return serverProps ;
}

Java Shutdown Hook

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("Shutdown started");
             //  Close all the resource locks
            System.out.println("Shutdown successful");
        }));

Saga Design Pattern

The Saga design pattern is a way to manage distributed transactions in a microservices architecture. In a microservices environment, where d...