performance tuning Microservices , Docker - vidyasekaran/current_learning GitHub Wiki

Performance Tuning Microservices

https://www.youtube.com/watch?v=zjZ7-pmfezE&list=PLUTKeNksL7kp68S3aeTWSdZj2FkKIvZFx

  1. HazelCast, springboot, distributed etc https://www.javainuse.com/hazelcast

https://hazelcast.com/blog/spring-boot/ https://hazelcast.com/glossary/distributed-cache/

0.1. Cahcing in Spring boot https://howtodoinjava.com/spring-boot2/spring-boot-cache-example/ https://www.javainuse.com/spring/spring-boot-hazelcast

  1. **Look at MS code and improve ** - use JVisualVM to monitor, take heap dump and use Eclipse mat to figure out memory leaks and fix

https://github.com/vidyasekaran/current_learning/wiki/Java-Application-Performance-and-Memory-Management

2.** Check Database and see if indexes are in place, how much time it takes (JPA Caching)**

https://vladmihalcea.com/jpa-hibernate-first-level-cache/ https://www.developer.com/java/using-second-level-caching-in-a-jpa-application.html

Fist level cache in hibernate is enabled by default and you do not need to do anything to get this functionality working. In fact, you can not disable it even forcefully. Similarly, first level cache associated with session object is available only till session object is live. It is available to session object only and is not accessible to any other session object in any other part of application.

https://howtodoinjava.com/hibernate/understanding-hibernate-first-level-cache-with-example/ (Has examples to test)

Implementing Second Level Cache

package org.mano.dto; @Entity @Cacheable(true) public class Account{

https://howtodoinjava.com/hibernate/how-hibernate-second-level-cache-works/ It also means that once session factory is closed, all cache associated with it die and cache manager also closed down.

Second level cache: This is apart from first level cache which is available to be used globally in session factory scope.

NOTE: you will need to configure ehCache or any other cache as a Second Level Cache

Hibernate EhCache Configuration Tutorial

https://howtodoinjava.com/hibernate/hibernate-ehcache-configuration-tutorial/

  1. In UI if keep showing static data in Screen you use RedisCache or any distributed caches.

https://programmerfriend.com/ultimate-guide-to-redis-cache-with-spring-boot-2-and-spring-data-redis/

  1. Improve on the capacity of the systems where it is deployed.

  2. Increasing number of instance of MS redundancy.

  3. Have a profiler and check all the time JVisualVM, EclipseAnalyser https://www.baeldung.com/java-profilers

Performance Tuning docker

  1. Find using docker stats the docker container is consuming huge memory and cpu and utilisation of a docker container and u can proceed with further analysis as to why it’s causing issues.

https://youtu.be/h1dum0193hA

Use top cmd and find total ram used and number of process running also If no top install - apt-get update && apt-get install -y procps && apt-get install -y iputils-ping Open 2 terminals in one run

Run below commands in 2 different terminals

1st machine docker stats - this cmd shows number of process, network io, blocked io,cpu and memory utilisation of all containers running in a server.

2nd machine docker run -d -name nginx-container nginx docker exec -it nginx-container /bin/bash

  1. Container performance analysis https://youtu.be/bK9A5ODIgac

Application analysis- using cpu flame graphs with containers Host tuning - file system, networking, sysctls Conatainer analysis and tuning - groups, GPUs Capacity planning - reduce over provisioning

Use utilisation (time busy),saturation (run queue) , error

  1. Reduce size of docker image using docker multistage where dockerfile will have multiple FROM

https://youtu.be/KLOdisHW8rQ

  1. Change base image to alpine or some smaller FROM golang:alphine

Then build docker image and check

docker build -t webserver .

  1. Use layering of docker and separate it out and create image

FROM golang:alphine as BUILDER WORKDIR /go/src/app COPY main.go . RUN go build -o webserver .

#2nd stage where we use the 1st stage WORKDIR this reduces image size here u r avoiding copy main.go FROM alphine ( u can use something else other than alphine example scratch where we don’t have full os. WORKDIR /app COPY —-from=builder /go/src/app /app CMD [./webserver]

  1. Multi stage in springboot example where in 1st stage we copy source files and build using mvn command https://youtu.be/9MAU-O5jWJg

From openjdk:8-joke-alphine as builder RUN mkdir -p /app/source COPY . /app/source WORKDIR /app/source RUN ./mvn clean package

From openjdk:8-joke-alphine as runtime COPY —from= builder /app/source/target/*.jar /app/app.jar EXPOSE 8080 ENTRYPOINT [“java”,

  1. Speed up docker build https://youtu.be/oZ9nyCWERYc

In docker file each command may be a layer so if u change an image, it will recalculate so it will pull all depencies so copy specific files and folders instead of complete base directory.

Old before cache

FROM golang:alphine as BUILDER WORKDIR /app COPY . . RUN npm install CMD [“npm”,”start”]

New

FROM golang:alphine as BUILDER WORKDIR /app COPY package-log.Json package.json ./ RUN npm install COPY . . CMD [“npm”,”start”]


Docker Performance

use maven dependecny plugin - goal unpack help to cache the jar so if we make a change to code it won't load again. https://github.com/vidyasekaran/docker-crash-course/tree/master/01-hello-world-rest-api

FROM openjdk:8-jdk-alpine ARG DEPENDENCY=target/dependency COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib COPY ${DEPENDENCY}/META-INF /app/META-INF COPY ${DEPENDENCY}/BOOT-INF/classes /app ENTRYPOINT ["java","-cp","app:app/lib/*","com.in28minutes.rest.webservices.restfulwebservices.RestfulWebServicesApplication"]

also refer file:///E:/docker_crash_course/Docker-in-Action.pdf

How to provide auth for dockerhub in spotify

https://github.com/spotify/docker-maven-plugin

settings.xml file as part of the block.

docker-hub foo secret-password [email protected]
⚠️ **GitHub.com Fallback** ⚠️