Java Spring Boot and Microservices Insights

Sep 10, 2024

Lecture Notes on Java Spring Boot and Microservices Mock Interview

Introduction

  • Guest: Shivar Engineer
  • Experience: 6 years in Java, Spring Boot, and Microservices
  • Opportunity for free mock interviews

Shivar Engineer's Background

  • Experience: 6+ years as a backend developer
  • Current Work: Enterprise and internet-based applications
  • Current Project: Government-related project to track IND histories using a reporting system with uploads of photos/videos.

Technical Questions Discussion

HashMap & HashSet

  • Collision Handling:
    • When two different objects share the same hash code, collisions occur.
    • Use unique hash code algorithms to minimize collisions.
    • Internally, HashMap uses linked lists or red-black trees (after Java 8) to manage collisions.

Comparable vs Comparator

  • Comparable:
    • Interface for natural ordering of objects (e.g., sort by age).
  • Comparator:
    • Interface for custom ordering of objects (e.g., sort by name).
  • Shivar has customized sorting in previous projects.

Finalization in Java

  • finalize() method: Invoked by the garbage collector to clean up resources before object destruction.
  • finally block: Used in exception handling to ensure resource cleanup regardless of exception occurrence.
  • Note: finalize() method is discouraged in modern Java.

Memory Management

  • JVM handles memory leaks through garbage collection.
  • Common causes of leaks include static references and unreferenced objects in HashMaps.
  • Recommended tools for identifying memory leaks: JProfiler, VisualVM, Eclipse Memory Analyzer.

Class Loading

  • ClassLoader vs Class.forName():
    • ClassLoader loads classes at runtime, while Class.forName() is a static method that loads and initializes a class by its name.

String Handling

  • String vs StringBuffer vs StringBuilder:
    • String: Immutable
    • StringBuffer: Thread-safe (synchronized)
    • StringBuilder: Not thread-safe, but faster for single-threaded scenarios.

Java Memory Model

  • Generational Garbage Collection:
    • Memory divided into Young Generation and Old Generation. Objects move based on their age through various phases of collection.

Object Immutability

  • To create immutable objects: use final class, private variables, and no setters.
  • Advantages in concurrent applications where state consistency is crucial.

Serialization

  • Process of converting an object into a binary format (serialization) and back (deserialization).
  • Use Serializable interface and avoid serialization with the transient keyword.

Exception Handling

  • Errors vs Exceptions:
    • Errors are system-level issues; exceptions can be handled in code.
  • Runtime exceptions: a subset of exceptions that occur during execution (e.g., NullPointerException).

Design Principles

  • Composition Over Inheritance: Favor composing objects with specific functionalities instead of inheriting from a base class.

Spring Boot Security

  • Use JWT for authentication and authorization mechanisms.

Spring Boot Features

  • Auto-Configuration: Automatically configures beans based on classpath settings.
  • Override configurations using annotations like @Conditional.

Performance Optimization

  • For high traffic applications: leverage Spring Cloud for client-side load balancing and consider horizontal scaling.

Spring Bean Lifecycle

  • Lifecycle includes initialization, resource allocation, and cleanup.

Handling Production Issues

  • Use Spring Actuator for monitoring and tracking performance.
  • Investigate using logging tools like AWS CloudWatch to identify issues.

Maven Build Optimization

  • Clean up before building; handle dependency conflicts if they arise.

Coding Challenges

  • Task 1: Use Stream API to find the employee with the second highest salary from a list.
  • Task 2: Count how many salaries in a list are greater than 10,000.

Conclusion

  • Importance of thorough understanding in Java, Spring Boot, and microservices for effective interviews and project work.