Introdução ao Akka – uma biblioteca de código aberto para desenvolvimento distribuído

Introduction to Akka – an open source library for distributed development

Written in Scala, Akka is a toolkit for building distributed, message-oriented applications. Based on the Actor-Model system, Akka is the choice of many companies for their backend. Why?

Imagem em destaque

What is Aka?

Akka is an application development tool for building highly concurrent, message-oriented Java and Scala applications. It is a development framework that allows you to create distributed, JVM- based applications.

Applications built through Akka are event-driven and resilient. They are also highly scalable and allow you to focus on your core business objectives. Additionally, you don't need to worry about low-level concurrency primitives or server-side traffic routing when building your application with Akka.

Akka apps can send 50 million messages per second. It can also handle 2.5 million actors per GB. This allows applications to be highly scalable and efficient. The machine is also self-healing, meaning it can automatically correct local and remote actors in case of any problem. Scaling can be done through different techniques such as balancing, clustering, sharding and partitioning.

What are Akka actors?

Akka actors are similar to objects in an object-oriented design. They receive messages, decouple them, and perform tasks according to the message's instructions. They can perform a task themselves or forward the message to another actor with authentication to perform the required task.

Actors are used to create, configure, and deploy Akka components. They are typically used in the Actor-Model System for synchronization and message passing. These actors run on different threads and communicate with each other through a group router. The router uses a receptionist to discover available actors. The receptionist is also responsible for forwarding the message only to registered actors.

What are the problems with traditional concurrency models?

In modern IT infrastructure, there are many ways to handle concurrency, such as multithreading and encapsulation, among others. However, in these methods, you must assign part of your memory for sharing, which often leads to corrupted internal states of the objects.

You can apply locks to fix this issue. However, they are an inefficient and outdated method of resolving communication problems. They don't scale efficiently and can cause problems like deadlocks.

You can use call stacks to fix some of the multithreading issues. However, call stacks cannot handle failed requests. For example, the main thread will not access the worker thread if you have a message waiting in a queue. In this scenario, if the worker thread starts working on the message and fails, the main thread does not receive notification of the failure. Additionally, if the worker thread terminates when the main thread is not accessing it, its failure will not generate any notification. This means that the thread will remain faulty until the next time it is accessed, at which time it will raise its first failed state exception.

How does Akka work?

Akka's actor model solves the concurrency problem by sending messages to other actors instead of using the main thread. This allows you to emulate encapsulation without using locks. Akka also ensures that actor states are maintained in a thread-safe manner.

Actors pass the responsibility for sending notifications to the receiving actor. They also process incoming thread messages sequentially rather than instantly. This allows them to synchronize without using multiple threads.

How do actors work?

Akka follows the model of actor and actor supervisor. When a failure occurs, the subordinate actors shut down and throw an exception. In this situation, the supervisor can escalate the failure, restart the subordinate, or permanently stop the actor. However, if the failed actor is also a supervisor, terminating him would mean terminating all of his subordinates.

You can also terminate an actor's minions before restarting it. Actors cannot be attached to supervisors from outside the network and be cleanly disconnected when their supervisors fail.

Instead of direct communication, actors communicate through message passing. The actor's references support the messages the actor wants to send. Actors follow known methods of message passing, such as fire and forget, request response, and adapted response.

Each actor has a default configuration that includes a mailbox (for message queuing), execution environment (for code manipulation and message passing), behavior variables, and address. However, actors can be differentiated based on paths.

Paths are addresses that describe the protocol and location of an actor. Each actor has a physical and logical path. These paths help engineers define the functional location of an actor in the actor system.

The preferred method for dealing with actors is through clusters rather than connecting remotely to them. The clusters module handles adding and removing actors in different cluster systems. It is also responsible for computing distribution.

Akka Services

Alpaca

This is an open source product for Java/Scala pipeline integration.

Alpakka has an extension called Alpakka Kafka that allows you to connect to Kafka streams. Using Akka development services transactions for Kafka ensures that all messages are consumed without duplication and successfully produced at the sink.

Akka Flows

Akka Streams is a toolkit that allows you to create high-level abstraction streams without worrying about actor behavior and variables.

Akka Projections

Akka projections allow you to process a stream from a source. The flow must have a payload and a traceable displacement. You can also use it to reset and track these projections.

Akka vs Spark?

Apache Spark is a data processing engine used to process data in batch. It focuses on fault tolerance. It uses resilient distributed datasets for fast data sharing and processing. It also allows you to run tasks on different machines in parallel.

Akka is a general-purpose framework for building high-performance distributed systems. It allows you to process transactions without worrying about concurrency.

Spark is much more useful for performing actions on large data sets (big data), while Akka emphasizes a greater degree of actor control. You should use Akka wherever you need to perform real-time batch processing using business logic.

Is Akka worth learning in 2021?

The Actor-Model system is highly useful if you are working on distributed algorithms and processes. If you want to run your application in the background, this model would be beneficial to synchronize messages and facilitate communication, especially if you are working in languages ​​like Scala, Java or Erlang.

Source: BairesDev

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.