Microservices FlashCard

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 14

Some popular tools and frameworks used for microservices development

1)Spring Boot - for building microservices.


2)Node.js - to build lightweight and scalable microservices using JavaScript.
3)Docker- A containerization platform to package services into containers.
4)Kubernetes - An orchestration platform for managing containerized applications.
5)Istio - service mesh that offers advanced traffic management, security
6)Netflix OSS
- Eureka (service discovery),
- Ribbon (client-side load balancing), and
- Hystrix (circuit breaker)
7)RabbitMQ, Kafka
- Message brokers that facilitate event-driven communication and
asynchronously decouple services
8)Prometheus, Grafana-Monitoring tools that collect, store,and visualize metrics.
9)Consul, etcd
- Distributed key-value stores used for
- service discovery,
- configuration management, and
- coordination.
10) ELK Stack - Elasticsearch, Logstash, and Kibana
- a popular combination for log aggregation and centralized logging
11) Linkerd
- service mesh solution that provides
- observability,
- security, and
- traffic control capabilities for microservices
========================================
10 Essential Microservice Design Patterns and Principles

1) Database per Microservice


2) Event Sourcing
3) CQRS (Command Query Segmentation (CQRS))
4) Saga
5) BFF (Backend For Frontend)
6) API Gateway
7) Strangler
8) Circuit Breaker
9) Externalized Configuration
10)Consumer-Driven Contract Tracing
------------
1. Database per Microservice
2. Event Sourcing
- any change in an entity's state should be captured by the system
3. CQRS (Command Query Segmentation (CQRS)) (Pronounce as C-quee-res)
- either change the state of the entity or return the result
4. Saga
- to keep consistency with data in distributed architecture
without having the ACID principles.
SAGA is responsible for committing multiple commentary transactions
by giving rollback opportunities.
5. BFF (Backend For Frontend)
- to identify how the data is fetched between the server and clients
6. API Gateway
- a single entry point for a certain group of microservices
7. Strangler
- to incrementally transform your monolithic application to microservices
by replacing old functionality with a new service.
8. Circuit Breaker
- to improve the fault tolerance and
resilience of the microservice architecture.
- prevent the cascading of failure to other microservices.
9. Externalized Configuration
- to read configuration from many sources and
- potentially change previously specified configuration settings
based on the reading order
10. Consumer-Driven Contract Tracing
- useful in legacy application which contains a large data model
and existing service surface area.
------------

------------

------------
Saga

Benefits of the saga pattern:


• Loose coupling: Sagas allow services to operate independently,
promoting loose coupling between microservices.
• Reliability: By breaking down transactions into smaller,
isolated steps, the saga pattern reduces the likelihood of
system-wide failures and increases overall system reliability.
• Scalability: Each microservice can independently scale based on its workload,
avoiding bottlenecks in the overall transaction process.
• Atomicity: Although not providing the same strict atomicity
as a traditional ACID transaction,
the saga pattern ensures that the system eventually reaches a consistent state.

------------

------------

------------

------------
Server-side Load balancing

Steps:-
1) Create a spring boot project with spring cloud gateway
and spring cloud eureka client dependencies.
- spring-cloud-starter-netflix-eureka-client
=> gateway project needs to register itself to the Eureka Server.
- spring-cloud-starter-gateway
=> converting your microservice into an API gateway

2) Configure application.yml to handle routing.


------------

========================================
Commonly used Spring Cloud annotations:
1) @EnableCircuitBreaker
2) @EnableConfigServer
3) @EnableEurekaServer
4) @EnableFeignClients
5) @FeignClient(name=”ApplicationName”)
Annotations On Fault Tolerance provided by Resilience4j:
1) @RateLimiter
2) @Retry
3) @CircuitBreaker
4) @Bulkhead
5) @Timelimiter
========================================
JAR Vs WAR

JAR (Java ARchive) file


- used to distribute standalone Java applications or libraries.

WAR (Web ARchive) file


- used to package and distribute web applications
- contains the web application’s resources,
such as servlets, JSPs, and HTML files,
as well as a WEB-INF directory that contains additional metadata,
such as the "web.xml" deployment descriptor

JAR files are used for packaging and distributing


standalone Java applications or libraries.

WAR files are used for packaging and distributing web applications.
========================================
Useful Ports

Eureka Server => 8761


Eureka client (eureka-client) => 8080
========================================
Role of Docker in microservices deployment
Docker provides contanerization.
Each microservice and its dependencies are packaged into lightweight,
isolated containers.
Docker ensures that each container runs consistently across
different environments such as development, testing, and production.
This avoids the notorious "it works on my machine" issue.

========================================
Purpose of an API gateway in microservices
An API gateway in microservices acts as a central entry point
that handles client requests and
then routes them to the appropriate microservices.
Several purposes of API Gateway
1) Aggregation
- combine multiple backend microservices' responses into
a single cohesive response to fulfill a client request.
This reduces round-trips.

2) Load balancing
- can distribute incoming requests across
multiple instances of the same microservice.

3) Authentication and authorization


- can handle security-related concerns by authenticating clients and
authorizing access to specific microservices

4) Caching
- can cache responses from microservices to improve performance and
reduce redundant requests.

5) Protocol translation
- can translate client requests from one protocol.

========================================
How do microservices ensure fault tolerance and
resilience in distributed systems.

1) Redundancy
- By replicating microservices across multiple instances.

2) Circuit breaker pattern


- To prevent cascading failures.

3) Bulkheads
- Microservices are isolated from each other.
Hence failures remain unaffected in any service.

4) Graceful Degradation
- can gracefully degrade their functionality.
or provide limited but essential features.

5) Timeouts
- Setting appropriate timeouts for communication between microservices.
========================================
Coupling and Cohesion

Coupling is the relationship between software modules A and B,


as well as how dependent or interdependent one module is on the other.

Cohesion is a connection between two or more parts/elements of a module


that have the same function.
A module with strong cohesion may effectively execute a given function
without requiring any connection with other modules.
========================================
Conway's Law and its relevance in microservices architecture

Conway's Law states that the structure of a software system will


mirror the communication structures of the organization that builds it.
In the context of microservices architecture, this means that
the architecture will reflect the communication and collaboration patterns
of the development teams.

Understanding Conway's Law is crucial for effective microservices design


as it emphasizes the importance of communication and collaboration
within the organization to ensure a well-structured and
coherent microservices architecture.
========================================
Service registration:
When a microservice starts up, it registers itself with a service registry
(e.g., Consul, Eureka) by providing essential information like
its network location, API endpoints, and health status.

Service discovery:
When a microservice needs to communicate with another microservice,
it queries the service registry to discover the network location and
endpoint details of the target service.

In Microservices, service discovery is commonly implemented using tools


like Netflix Eureka, Consul, etc.
========================================
WebMvcTest annotation is used for unit testing in Spring MVC Applications.
========================================
How can you handle database management efficiently in microservices?
1) Database per service
2) Eventual consistency - allow data to propagate and synchronize over time.
3) Sagas - a sequence of local transactions.
4) CQRS (Command Query Responsibility Segregation)
- separates read and write operations,
allowing the use of specialized databases for each.
5) Event Sourcing
- sourcing stores all changes to the data as a sequence of events.
========================================
Benefits of using Kubernetes for microservices orchestration:- Learning curve

1) Container orchestration
2) High availability - Kubernetes supports multiple replicas of services.
3) Auto-scaling
4) Service discovery
- Kubernetes provides built-in service discovery and
DNS resolution for communication between services

Challenges of using Kubernetes for microservices orchestration:-


1) Steep Learning curve
2) Infrastructure complexity
3) Networking
4) Resource overhead
========================================
Best practices for securing communication between microservices:-
1) Transport Layer Security (TLS)
- Enforce TLS encryption for communication over the network
to ensure data confidentiality and integrity.
2) Authentication and authorization
3) Use API gateways
4) Secure service-to-service communication.
5) Service mesh - using a service mesh like Istio or Linkerd.
6) API security
- Use API keys, OAuth tokens, or JWT (JSON Web Tokens)
to secure APIs and prevent unauthorized access
========================================
Materialized View pattern
- a method for aggregating data from numerous microservices.
- In this method, we create a read-only table with data
owned by many Microservices in advance
(prepare denormalized data before the real queries).
The table is formatted to meet the demands of the client app or API Gateway.

IMP:-
A materialized view and the data it includes are "disposable"
since they may be recreated entirely from the underlying data sources.
========================================
How does microservices architecture facilitate rolling updates and
backward compatibility

1) Service isolation
2) API versioning
3) Semantic versioning
4) Feature flags
5) Graceful degradation

What is Semantic Versioning(also known as SemVer)?


Semantic Versioning is a versioning scheme for using meaningful version numbers.
Specifically, the meaning revolves around how API versions compare in terms of
backwards-compatibility.
SemVer is in the form of Major.Minor.Patch.
------------
Major version - Major changes breaks the API
Minor version - Minor changes does not break the API.
Used for the release of new functionality in the system.
Patch - Bug Fixes

E.g. 2.6.8
2.6.9 => for bug fix
2.7.0 => for adding new functionality
------------
Valid identifiers are in the set [A-Za-z0-9] and cannot be empty.
Pre-release metadata is identified by appending a hyphen
to the end of the SemVer sequence.
E.g. Pre-release for version "1.0.0" could be "1.0.0-alpha.1"

Note that names cannot contain leading zeros,


but hyphens are allowed in names for pre-release identifiers.
------------
IMP Points
• The first version starts at 0.1.0 and not at 0.0.1,
as no bug fixes have taken place.
• SemVer does not cover libraries tagged 0.*.*.
The first stable version is 1.0.0
========================================
Role of a message broker in asynchronous microservices communication

- acts as an intermediary that facilitates the exchange of messages


between microservices
without requiring them to interact directly in real time.
========================================
12-factor app methodology and its significance in microservices development

- a set of best practices for building modern, scalable, and


maintainable web applications,
particularly in the context of cloud-based and microservices architectures.

1) Codebase
2) Dependencies
3) Config
4) Backing Services
5) Build, release, and Run
6) Processes
7) Port Binding
8) Concurrency
9) Disposability
10) Dev/prod parity
11) Logs
12) Admin processes

CDC BBPP CDD LA


------------
Codebase
Single codebase per application tracked in version control with many deploys.

Each application must have only a single codebase.


Each such codebase must be managed in a version control system.

If there exists multiple codebases, then it’s not an application,


it’s a distributed system.
------------
Dependencies
Explicitly declare and isolate dependencies.

We must always declare the dependencies in the "manifest" file,


a file containing the metadata for the dependencies like name, version.
It increases the speed of the development
as now the developer is free from the task of managing
the correct version of the libraries.
There is no need for explicitly downloading the required JARs anymore.
------------
Config
Store config in the environment.

The source code and the configurations


must be completely separated from each other.

Must store all the configurations like DB credentials, path, URI


in the environment variables.
No configuration should be stored in git in plain text.
------------
Backing Services
Treat backing services as attached resources.

One should be able to easily change the database


by just changing the DB URL and credentials.

Our services become easily interchangeable and


offer great portability to our application.
------------
Build, Release and Run
Strictly separate built and run stages.
The deployment of your application must be properly separated
into three non-overlapping non-dependent phases
namely build, release and run.
------------
Processes
Execute the application as one or more stateless processes.

Usage of sticky sessions must be avoided.


Sticky sessions refers to catching the logged-in user’s session data
in the local memory of the process and
then directing each subsequent request from that particular user
to the same process.

The problem with sticky sessions is that


it causes uneven load balancing among the instances of the application.
------------
Port Binding
Export services via port binding.

Exports HTTP as a service and


doesn’t require any server like a tomcat to listen to requests.
------------
Concurrency
Scale out via the process model.

horizontal scaling in which we run multiple instances of our processes.


------------
Disposability
Maximize robustness with fast startup and graceful shutdown.

Robustness of an application refers to the graceful starting and


termination of its processes
without affecting the overall application’s functionality.
Also, it must start quickly whenever required.
------------
Development/Production Parity
Keep development, staging, and production as similar as possible.

The development and production environment must be as similar as possible.


The processes being used, technologies and the infrastructure must be the same.

Helps in the continuous deployment of our application and


reduces the development time and efforts also.
------------
Logs
Treat logs as event streams.

Whenever a request enters into the system,


corresponding logs are made and
they are treated as a sequence of events that can be used to debug
when some problem occurs.
------------
Admin Processes
Run admin/management tasks as one-off processes.

Certain scripts which are required for the execution of certain tasks,
should be maintained in the codebase and managed by the version control system.
========================================
Use of configuration management tools in microservices
Facilitates the dynamic and centralized management of configuration settings
for individual services.

Some popular configuration management tools used in microservices include


Consul, etcd, ZooKeeper, and Spring Cloud Config.
========================================
Log aggregation consolidates logs from multiple sources into
a centralized repository, simplifying log analysis and
providing a holistic view of the system's health and performance.

Centralized logging allows developers and operations teams to


search, filter, and analyze logs easily,
making it quicker to identify and resolve issues.
Additionally, centralized logging enables long-term storage and
data retention for compliance and auditing purposes.

Tools like the ELK stack (Elasticsearch, Logstash, Kibana), Graylog, and Splunk
are commonly used to implement log aggregation and
centralized logging in microservices architectures.
========================================
Microservices Vs Serverless Architecture

• In microservices, applications are divided into smaller,


independent services that can be developed, deployed, and scaled individually.
• Microservices typically run on servers or containers that are managed
by the organization or cloud provider.
• Developers are responsible for managing the underlying infrastructure,
including server provisioning, scaling, and maintenance.
• Microservices offer more flexibility in technology choice for each service.
• Scaling is usually manual or based on predefined rules.
• Microservices are suitable for complex applications and long-running processes.

Serverless:
• Serverless architecture allows developers to focus on writing code
without managing the underlying infrastructure.
• It operates on a pay-as-you-go model with developers only paying for the
actual compute resources used during code execution.
• Serverless functions are event-driven and stateless, meaning
they are triggered by specific events and
do not retain any state between executions.
• Scaling is automatic and based on demand,
ensuring that resources are allocated dynamically as needed.
• Serverless is ideal for event-driven applications,
real-time processing, and short-lived tasks.

AWS Lambda is a "serverless compute service" that runs your code


in response to events and
automatically manages the underlying compute resources for you.

Is EC2 AWS serverless?


With Amazon EC2, you have the ability to start and
allocate virtual machines as needed for your application.
It provides you with complete control of your computing resources and
lets you run on Amazon's computing environments.
Unlike Serverless, EC2 requires management and provisioning of the environment.

========================================
How do you ensure data privacy and compliance in a microservices ecosystem?
1) Data encryption - Implement encryption techniques (e.g., TLS/SSL).
2) Access control and authentication
- Use robust authentication mechanisms, such as OAuth and JWT.
3) Role-based access control (RBAC)
- to manage permissions and
restrict access based on the roles of users or services.
4) Data masking
5) Compliance and auditing
6) Secure APIs
7) Least privilege principle
8) Data lifecycle management
8) Data governance
9) Regular security assessments
========================================
Domain-Driven Design (DDD)

Domain-Driven Design (DDD) is a software design method wherein


developers construct models to understand the business requirements of a domain.
These models serve as the conceptual foundation for developing software.

Principles of DDD:-
1) Ubiquitous language
2) Bounded contexts
3) Aggregates
5) Domain events
6) Context mapping

Application in microservices:
In a microservices architecture, DDD principles can be applied as follows:
• Each microservice represents a bounded context,
containing its domain logic and data.
• Aggregates are mapped to individual microservices,
allowing for more focused and independent development.
• Domain events can be published and subscribed to by various microservices
to maintain consistency and provide loose coupling.
• By embracing the ubiquitous language, developers and
domain experts can have meaningful discussions,
leading to better-aligned solutions.
========================================
Best practices for versioning microservices APIs
1) URL versioning
2) Header versioning
3) Semantic versioning
4) Deprecation strategy
5) API documentation
6) Continuous integration and deployment
7) API gateways
8) Version negotiation
9) Graceful migration
10) Monitoring and analytics

========================================
Blue-green deployment strategy and its advantages in a microservices setup

Blue-green deployment is a deployment strategy that involves


running two identical environments (blue and green)
and switching between them during software updates or releases.

In a microservices setup, this strategy can be applied at the service level,


allowing for seamless updates of individual services
while maintaining overall system availability.

Advantages of blue-green deployment strategy


1) Zero downtime
2) Quick rollback
3) Canary releases
- a small percentage of traffic is routed to the green environment first.
- enables real-time testing before rolling out to the entire user base.
4) Isolated updates
5) Consistent testing
6) Lower risk
========================================
Auto-scaling in a microservices architecture allows services
to automatically adjust their resource allocation based on demand.

Achieving auto-scaling involves the following steps:-


1) Implement robust monitoring of key performance metrics such as CPU usage,
memory consumption, request latency, and throughput for each service.
2) Define scaling policies based on the monitored metrics.
E.g. (1) increase the number of service instances
- if CPU utilization exceeds a certain threshold
(2) reduce instances - if the request latency is too high
3) Employ load balancing mechanisms
to distribute incoming traffic evenly among available instances.
4) Leverage container orchestration platforms like Kubernetes or Docker Swarm,
which have built-in auto-scaling features.
5) Use Cloud Providers auto-scaling capabilities
to dynamically adjust the number of instances based on predefined rules.
6) Implement health checks to monitor the status of instances.
7) In complex microservices architectures,
use service meshes like Istio or Linkerd,
which offer additional auto-scaling features and traffic control capabilities
========================================
How can you apply the bulkhead pattern
to improve fault isolation in microservices?

The bulkhead pattern is a design principle borrowed from shipbuilding.


Multiple compartments (bulkheads) are used to isolate the ship's sections,
preventing the entire vessel from flooding in case of damage.
In a microservices architecture, the bulkhead pattern is used
to isolate components and limit the impact of failures.

Here's how it can be applied in microservices:-


1) Thread pool isolation
2) Database isolation
3) Service instance isolation
- Run multiple instances of the same service and
distribute incoming requests among them.
4) Circuit breaker - to handle failures gracefully
5) Asynchronous communication
6) Rate limiting and throttling
========================================
How do you implement distributed authorization
and access control in microservices?

To prevent unauthorized access to resources and actions


while maintaining a consistent and
secure authentication mechanism across the entire system.
Approaches:-
1) Token-based authentication - JWT (JSON Web Tokens)
2) Centralized identity provider
- Implement a centralized identity provider or single sign-on (SSO) service.
3) OAuth 2.0
4) API gateway
5) Claims-based authorization
- user roles and permissions are embedded within the authentication token.
6) Attribute-based access control (ABAC)
- access control policies based on various attributes such as
- user roles,
- environmental conditions, and
- resource properties
7) Service-to-service authentication
- Implement secure communication between microservices using mutual TLS (mTLS)
or other authentication mechanisms
8) Role-based access control (RBAC)
- Define roles and permissions for each service .
========================================
How do you achieve data partitioning in microservices
to manage large datasets efficiently?

1) Horizontal partitioning
- Data is distributed across multiple databases
based on a specific criterion like customer ID or date range
- Ensures that each microservice only deals with a subset of the data,
which improves performance and scalability.
2) Vertical partitioning
- Breaking down a large table into smaller, more focused tables
3) Sharding
- data is distributed across different databases
or clusters based on a specific shard key.
- Enables independent scaling of each shard.
4)

========================================
Canary testing is a deployment strategy that allows
the gradual release of new microservices versions to a subset of users.

To implement canary testing, you first deploy the new version


to a small group of servers or instances,
serving only a fraction of the user traffic.
You then closely monitor the behavior and performance of the canary version.
If everything goes well, you gradually increase the rollout to a larger audience.

========================================
Importance of contract testing in a microservices environment

Contract testing involves testing the contracts or agreements


established between services when they interact with each other.
These contracts define the expected inputs, outputs, and
behaviors of each service.

With contract testing, microservices can verify that


they can communicate correctly with their dependencies.
This prevents breaking changes from being introduced and
avoids cascading failures caused by incompatible service interfaces.
========================================
Trade-offs between synchronous and asynchronous communication in microservices

Synchronous communication involves direct request-response interactions


between services, where a service waits for a response before proceeding.

Synchronous communication can lead to increased coupling


between services as they are directly dependent on each other's availability
and responsiveness.
This can create a single point of failure and result in cascading failures
if one service becomes overwhelmed or unresponsive.

Asynchronous communication decouples services and improves resilience.


Services communicate through messages or events,
allowing them to process requests independently and at their own pace.
This reduces the immediate impact of failures and provides better scalability.

Asynchronous communication adds complexity to the system


as you need to handle eventual consistency, message persistence,
and message ordering.
Implementing retries and handling failed messages
becomes necessary to ensure reliability.

Choosing between synchronous and asynchronous communication


depends on the specific use case and
requirements of the microservices architecture.
A hybrid approach that uses both types of communication
can also be employed to strike a balance between simplicity and resilience.
========================================
Monitoring and observability in microservices

Monitoring involves collecting and analyzing metrics and logs


from various components in the system.
It provides insights into the health, performance,
and resource usage of individual services.

Observability focuses on understanding the system's internal behavior


based on real-time data and traces.
========================================
Key performance metrics to monitor in a microservices architecture

1)Response time
2)Error rate
3)Throughput
4)CPU and memory usage
5)Network latency
6)Request rate distribution
7)Database performance
8)Service dependency health
========================================

========================================

========================================
========================================

========================================

========================================

You might also like