Monday, January 24, 2022

Locking : Best Practices

 


Read Lock:

Read lock allows multiple thread to acquire lock in read method when all the synchronizing threads are using only the read lock of the Reentrant Read Write Lock pair.

If any thread is using the write lock of Reentrant Read Write Lock pair, read lock on resource is not allowed

Write Lock : Write lock allows only one thread to acquire lock in write method.All the other synchronizing threads will have to wait for the lock to be released before they can acquire read or write lock on resource

ReadWriteLock rwLock = new ReentrantReadWriteLock();

Lock readLock = rwLock.readLock();

Lock writeLock = rwLock.writeLock();

readLock.lock();

try {

    // reading data

} finally {

    readLock.unlock();

}

writeLock.lock();

try {

    // update data

} finally {

    writeLock.unlock();

}

Friday, January 21, 2022

Storage Solutions EBS vs EFS vs S3

 AmazonEBS :  High-availability block-level storage volumes for Amazon Elastic Compute Cloud (EC2) instances.

 - It is paired with an EC2 instance. So when we need a high-performance storage service for a single instance, use EBS

 - It stores data on a filesystem which is retained after the EC2 instance is shut down.

 Amazon EFS : Offers scalable file storage, also optimized for EC2. Using an EFS file system, you can configure instances to mount the file system.

Amazon S3 :  An object store good at storing vast numbers of backups or user files. 

Unlike EBS or EFS, S3 is not limited to EC2.

 Files stored within an S3 bucket can be accessed programmatically or directly from services such as AWS CloudFront. 

 This is why many websites use it to hold their content and media files, which may be served efficiently from AWS CloudFront.

Thursday, January 20, 2022

How to make Cross Domain Access

You can not allow xhr call to cross domain. Otherwise it would be very risky to allow third party script in your domain code. Here we will go through some of the ways cross domain can be made.

1. Proxy Route

II. Adding Access-Control-Allow-Origin in the header 

Wednesday, January 19, 2022

Designing a Resilient Architecture

 Designing a resilient architecture can comprise of following paradigm:

I. Does your application has retrial mechanism?

II. Is there any mechanism to stop recurring attempt in case of remote external service failure. Eg. circuit breaker, reslient4j  etc.

III. How fast your infrastructure can be muted?  

IV. What maximum downtime your application has?

V. Is your application hosted with cold deployment? 

VI Is there any Leader Election taking place when there is a multiple deployment?

MnagedBean using JMX API

 JMX specification supports the following types of Bean:

  • Standard MBeans
  • Dynamic MBeans
  • Open MBeans
  • Model MBeans
  • MXBeans 
Managing Resource using JMX Agent:
 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 
        ObjectName name = new ObjectName("com.example:type=Hello"); 
        Hello mbean = new Hello(); 
        mbs.registerMBean(mbean, name); 
        
Out of the box predefined JMX bean
ManagementFactory.getRuntimeMXBean().getName()
ManagementFactory.getThreadMXBean().getThreadCount()
ManagementFactory.getMemoryPoolMXBeans().forEach() mpmx> mpmx .getPeakUsage();
ManagementFactory.getClassLoadingMXBean();

Controlling Spring Behaviour

While developing Spring based application there are instances where we would like to get a control of the spring beans. It comes very handy when we can get a control of the Spring Factory and can customise as per the needs

1. BeanPostProcessor:

2. BeanFactoryPostProcessor: 

3. PropertyPlaceholderConfigurer : Property file location

-We can plugin some additional behaviour on top of existing definition.

- This will get called when all bean definitions will have been loaded, but no beans will have been instantiated yet.

-This allows for overriding or adding properties even to eager-initializing beans. 

- This will let you have access to all the beans that you have defined in XML or that are annotated (scanned via component-scan).

public class CustomBeanFactory implements BeanFactoryPostProcessor {

    @Override

    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        for (String beanName : beanFactory.getBeanDefinitionNames()) {

            BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);

            // Manipulate the beanDefiniton or whatever you need to do

        }

    }

//Registering a new Bean

  GenericBeanDefinition myBeanDefinition = new GenericBeanDefinition(); 

            ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition("beanName", myBeanDefinition););


}

Saturday, January 15, 2022

The Life with Plugins

 In today's world when the boilerplate code is readily available, write  minimum code and connect pluggable functionalities are the way of developing software. To make the life of developer easier,  plugin play a very handy role to speed up the development.

Here we would be listing few of the useful plugins :

Intellij: 

1. Dependency Analyzer

Maven:

1. code coverage: jacoco-maven-plugin -> mvn jacoco:report

2. Shading Jar: maven-shade-plugin --->mvn shade:shade

3. Creating a Jar archive: maven-source-plugin