Tuesday, July 27, 2021

Microservcies: Database patterns for Microservices

 

When we design microservices architecture, the biggest challenge is how do we manage the data persisted, consistency, availability, scalability when business grows and accuracy at any time.

Unlike the monolithic applications where data is strictly have the ACID properties, microservices need different strategy and design to meet the real need of microservices architecture. The transaction management cannot be guaranteed in microservices architecture considering the decomposed components and number of nodes of applications services running.  

So here are recommended database design patterns (solutions to popular problems).

1. Database per service

2. Shared Database

3. SAGA

4. API Composition

5. CQRS

6. Domain Event

7. Event Sourcing


Lets discuss each one of them in detail.

1. Database per services: 

If we are on Domain Driven Methodology and all our business design and system design is based on Domain Driven Methodology, then we can prefer this option. Creating own database for each API, so no other services can directly access this database. Only through the API this database can be accessed and data manipulations can be performed. 

This is called as Data As Service. Considering the database we wanted to use in enterprise we make decision.

 This is not suitable for larger enterprises like Banking and Financial Services, Insurance Companies because they still rely on RDBMS systems and need ACID properties. So they might different strategies. 

When we building this Data As a Service, there are other issues design issues need to be assessed example Boundary Context. 




 



Tuesday, July 20, 2021

AWS : VPC and Subnet Selection : IPV4 and IPV6

What is VPC? Why do we need? 

VPC is better knows as Virtual Private Cloud, a virtual private network where you can create your AWS resources S3, EC2 etc., It is isolated virtual network defined by you. 
Also you have privilege to: 
  • Choose your own IP address range
  • Choose your gateways
  • Configure route tables
  • Add subnets
  • And create VPN connection 
  • ACL - Access Control Lists
You must have and default VPC, otherwise you cannot create any AWS resources. Its is like an Data Center without networking. You can access any resource within this VPC from public internet through Internet Gateway. Route tables enables router which routes the traffic from public internet to resources, between resources, etc.,

You can create custom VPC and configure if you do not want to use default VPC. Else you can edit the default VPC and configure according to your needs.

They are highly scalable and without any bandwidth limitations.

Using ACLs and Security Groups you can control your inbound and outbound traffic. ACLs for subnets and Security Groups for instances.

What is Subnet? How to configure?

It is just range of IP addresses within your VPC. You can create any resource and it will have the IP address assigned to it and within the Subnet range.  By  editing CIDR IPV4 or IPV6 you can increase IP address range. See below for detailed explanation. 

Subnet Selection : IPV4


When we work on AWS, we all must come across a mandate step, that is defining our subnets and setting subnet ranges based on our required IP addresses needed for our components with in the VPC. Here let us see how to choose Subnet and range. 

Lets define Subnet: What is Subnet? 

Subnets are part of available networks within the Availability zone. Each VPC has its own isolated virtual private networks which are parts of available network addresses with-in the availability zone. The subnets cannot span across multiple availability zones.


Here we will see how to choose the subnet and define its range.

When we create VPC,  we have to choose the Subnet block either in IPV4 block or IPV6 block. We will discuss the differences between IPV4 and IPV6 later. Let us look at the IPV4 block now. 
Here are the default options 10.0.0.0/24. 

Now lets decode the 10.0.0./24. 
IPV4 has the address ranges of 2^32 it is equal to 4,29,49,67,296 different IP addresses. 

If we define 10.0.0.0/0 -  it means 32-0 = 32, so 2^32 = 4,29,49,67,296 addresses with in the VPC. But we are not going to have such big number of AWS Services or  EC2 instances with-in our VPC. So we are going to have less number of IP addresses. 

If we want 16 addresses in our VPC, we have to go for 10.0.0.0/28 it means, 32-28=4, 2^4=16. So we will be having the 16 IP addresses range. 

OK that's cool. What will be my IP addresses.


 If we have chosen, 10.0.0.0/28, then it is 32-28=4, 2^4=16, ranges from  10.0.0.0 to 10.0.0.15. 
If we have chosen, 10.0.0.0/24, then it is 32-24=8, 2^8= 256, ranges from 10.0.0.0 to 10.0.0.255. 


Lets see what happen is the range is beyond 256. Lets choose, 10.0.0.0/16.
32-20=12, 2^16= 4096, ranges from 10.0.0.0 to 10.0.15.255. 


Let's see if we have chosen 10.0.0.0/16, 32-16=16, 2^16=65536, ranges from 10.0.0.0 to 10.0.255.255.

Can we have only one IP address in my VPC?

Yes it is possible. Give 10.0.00/32, 32-32=0, 2^0=1, ranges from 10.0.0.0 to 10.0.0.0


IPV6 Subnet:


If you want to set bigger range of IP addresses, then go with IPV6, where you can set 2^64  


Monday, July 5, 2021

Quick Summary on Lombok and MapStruct

 

Lombok

@Data

@AllArgsConstructor

@RequiredArgsConstructor

@NoArgsConstructor

@Getter

@Setter

@Builder


And Special 


@Singular

@NonNull

@Cleanup

@Value

@SneakyThrows

@Log

@With



MapStruct


Multi-layered applications often require to map between different object models (e.g. entities and DTOs). Writing such mapping code is a tedious and error-prone task. MapStruct aims at simplifying this work by automating it as much as possible.

In contrast to other mapping frameworks MapStruct generates bean mappings at compile-time which ensures a high performance, allows for fast developer feedback and thorough error checking.



  1. public class Car {
  2.  
  3. private String make;
  4. private int numberOfSeats;
  5. private CarType type;
  6.  
  7. //constructor, getters, setters etc.
  8. }


  1. public class CarDto {
  2.  
  3. private String make;
  4. private int seatCount;
  5. private String type;
  6.  
  7. //constructor, getters, setters etc.
  8. }


Here we define the Mapper. So after compilation the carToCarDto method is implemented as simple java code.

  1. @Mapper 1
  2. public interface CarMapper {
  3.  
  4. CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); 3
  5.  
  6. @Mapping(source = "numberOfSeats", target = "seatCount")
  7. CarDto carToCarDto(Car car); 2
  8. }




How to add AnnotationProcessor in gradle.


https://tomgregory.com/annotation-processors-in-gradle-with-the-annotationprocessor-dependency-configuration/