Friday, June 28, 2024

Hibernate

What does hibernate uses reflection API for?

  • Hibernate uses reflection extensively under the hood to map Java objects to Database tables And vice versa.

 In hibernate, how can we get data from the database?

  • We can get using session object.
  • The methods are get and load.
  • We have a query and criteria interfaces Based on which we can get the object.
What methods are available to get data from database in hibernate?
  • We have system object via which we can get data from database.
  • Methods are get and load.
  • We have query and criteria interfaces to get an object.
  • Load method with a specifier returns, a proxy object of that entity.
  • When we perform a method, it goes to database and get the actual data.
What is the difference between get and load?
  • When we use the load method with any specifier, it will not give you actual object, but proxy object.
  • When we use get method, it will go to database and Fire native query To get actual data.
What are the three states of hibernate entity?
  • New or transient, attached, rested or detached state.
  • When an object is not attached With any Session, it is just created. It is in New or transient state.
    • Not yet attached with session.
  • If an object is associated with Session, then it is said to be in attached state.
  • Once we perform any operations and commit, it is again not held by any session. It is in detached state.

Service Health and Recovery

 How do we know, service is running or not?

  • Service discovery maintains the information of all services.
  • We implement using Eureka server
  • Maintains the status of every service, like it is up/down
  • Every request first goes to service discovery, then service is hit from API gateway.

What Is the use of actuator?

  • Actuator is used to configure hysteric dashboard. Main role of actuator is To check the heartbeat of a system in micro service environment, whether the system is alive or not.
  • If any service is not responding or sending heartbeat for a particular interval, it will disconnect from service, registry, server, or any other mechanisms.
  • From hystrix dashboard We can check whether any service is live or not.
  • We can use a circuit breaker pattern Which is the design pattern to increase the performance or to have a fallback mechanism.
  • The property used to enable or disable actuator is management.end points.web.exposure.include=*
How can we monitor applications and manage performance?
  • For application monitoring, we can use actuator. For service monitoring, we can use zipkins.
  • From zipkins Dashboard, we can check logs.
  • We can also use cloud watch or ELK to check logs to manage performance.
What is circuit breaker method? 
  • When a micro Service feels we can send a default message or we can define a default fallback method, and we can define hystrix properties. This will break the circuit and it will call default method and default message which we have defined.
  • The circuit breaks Depending upon the properties, for example, after her, a certain number of request or after a certain time out the default fallback method or Message is called.
How can we check the health of a service that is service is down or not?
Using hystrix And circuit breaker, where we can define fallback methods. In fallback method, we have the properties like set the time out milliseconds And number of request. Based on these, it will hit the default Method instead of original one.

Wednesday, June 19, 2024

Microservices

What are the some of disadvantages of micro services?
  • Coordination complexity
    • As the micro service architecture comprises different services, communicating between them, these services become a little more complex.
    • Increased development time
    • Requires more complex architecture
    • Set up new architecture
    • Higher cost of implementation

Can we create micro services as state machines?
  • Micro services are independent entities that solve a specific context.
  • For that context, the micro service can work as a state machine.
  • In a state machine, there are life cycle events that cause change in the state of the system.
    • In a library, service, there is a book that changes state based on different events like procure a book, register a book, issue a book, return book, loser, book, lead to return of book, et cetera.
    • These events and book can form a state machine for library micro service.
How we can use multiple databases in spring micro services?
  • For different environment, we can use profile to use multiple databases.
  • Multiple databases for single environment can be configured using Java configuration.
    • We can have different data sources and object.
    • We can have factory method Which will give us different object based on conditions.
How can we scale micro services?
  • We can use Joule and Dribble to scale micro services.
What are some of the advantages of using micro services?
  • Single chain of responsibility(Scale up) Is better.
  • Easy to code and integrate.
  • Deployment takes less time as only concerned micro service needs to be deployed.
How can we perform session management in micro services?
  • We can use SAGA design pattern To perform session management in micro services.
    • We can apply with choreography approach or orchestration approach.
    • In choreography We can use event sourcing approach.
  • We can also manage session using correlation ID between micro services using mapped diagnostics context.
  • Asynchronous Session Can be managed using a broker like Kafka or Rabbit Mq etc.
What is Load Balancing?
  • Load balancing means distributing your incoming request to multiple server instances.

Saturday, June 8, 2024

Difference between Rabbit MQ and Kafka

Rabbit MQ Kafka
Rabbit MQ is basic Queue based messaging system based on FIFO Kafka adds on to the functionality of Rabbit MQ with Stream Processing System.
We use Advance message queueing protocol(AMQP) Kafka does not use Any such protocol or library.
Rabbit MQ has a smart broker, which allows routing, content based routing, Database routing. Kafka has A low level of broker comparatively. 
Rabbit MQ has a exchange, which can route messages to queues based on key. In Kafka, we publish a message and all the subscribers subscribed to the topic get the message.
Rabbit MQ delivers messages to consumers attached to the queue in round robin Fashion In database, we can set up change data capture(CDC) And send data to Kafka.
Rabbit MQ has a property prefetch To check the status of Queue.
We can define our own connectors to connect to legacy systems in Kafka.
Rabbit MQ has acknowledgement mechanism which does not auto delete message until acknowledgement comes. Kafka is basically Designed on fan out mechanism.
Rabbit MQ is an open source message distributor That works like post office and cloud. We can create a consumer group as all the consumers will get the same message.
Developed in 2007 and written in erlang programming language We can create multiple partitions within a topic. Kaka will mod key with Number of partitions To select partition, Number to Push message too.
Rabbit MQ allows micro services to communicate as-synchronously. The number of consumers must be equal to number of partitions in a consumer group.
Kafka deliver message in round robin fashion to each Partition in a consumer group.
We cannot retain messages in Rabbit MQ. We can retain messages in Kafka
Rabbit MQ is a Message Broker basically. How ever we can enhance it to perform Pub Sub Functionality. Kafka has Consumer offset via which we can check what messages has been delivered and what to deliver next.Rather than relying on a message queue, Kafka appends messages to the log and leaves them there, where they remain until the consumer reads it or reaches its retention limit.
RabbitMQ is an open-source distributed message broker that facilitates efficient message delivery in complex routing scenarios.It’s called “distributed” because RabbitMQ typically runs as a cluster of nodes where the queues are distributed across the nodes — replicated for high availability and fault tolerance. Kafka is purely based on Publish Subscribe Architecture geared towards streams and high-ingress data replay
RabbitMQ employs a push model and prevents overwhelming users via the consumer configured prefetch limit. This model is an ideal approach for low-latency messaging. It also functions well with the RabbitMQ queue-based architecture. Think of RabbitMQ as a post office, which receives, stores, and delivers mail, whereas RabbitMQ accepts, stores, and transmits binary data messages. Kafka employs a “pull-based” approach, letting users request message batches from specific offsets. Users can leverage message batching for higher throughput and effective message delivery.

Spring Boot

What is circular/cyclic dependency in spring boot? When two services are interdependent on each other, that is to start one service, we requ...