Asynchronous Programming - 14 Asynchronous, distributed, message-driven framework Akka

Insert image description here


Akka overview

Akka is an open source concurrent, distributed, message-driven framework for building highly scalable, reliable, and concurrent applications. It is based on the JVM (Java Virtual Machine) and is primarily developed using the Scala programming language, but also provides a Java API so it can be used in both Java and Scala.

The following are the key concepts and features of the Akka framework:

  1. Actor Model : The core building block of Akka is the Actor, a lightweight concurrency primitive. Actors interact with each other through message communication. Each Actor has its own state and behavior, and they are isolated from each other, which helps build a highly scalable system.

  2. Concurrency and parallelism : Akka allows developers to easily write concurrent and parallel code without having to worry about underlying thread management. It handles all the complexities associated with multi-threaded programming and provides abstractions so that developers can focus on business logic.

  3. Distributed systems : Akka provides support for building distributed systems. You can deploy Actors on different nodes, which can be physical machines or virtual machines. Akka provides transparent messaging, making sending messages in a distributed environment as easy as locally.

  4. Fault tolerance : Akka emphasizes fault tolerance, allowing developers to build reliable systems. It provides supervision strategies that allow customized recovery actions when an Actor fails. This helps the system continue to operate during failures, increasing system availability.

  5. Event-driven : Akka is event-driven, and its reactive programming model is suitable for handling asynchronous events. It allows developers to build responsive systems that work with large numbers of concurrent events and messages.

  6. Scalability : Akka has good scalability and can easily expand the system according to needs. You can add more nodes or actors to handle more load.

  7. Plug-ins and extensions : Akka provides a rich plug-in and extension mechanism that can easily integrate other libraries and frameworks, such as Akka HTTP, Akka Streams, etc., to build full-stack applications.


To remain resilient, Akka adopts the "Let it crash" model, which has been successfully used in the telecommunications industry to build self-healing applications and systems. The actor model also provides an abstraction for transparent distribution and the basis for truly scalable and fault-tolerant applications.

Let’s take a look at the features of Akka:

  • Makes it easier to build concurrent and distributed systems

    Akka is based on the Actor model and Streams, allowing us to build a scalable system that can efficiently use server resources and use multiple servers to expand.

  • Resilient design

    Adhering to the principles of the Reactive Manifesto, Akka allows us to write systems that can repair themselves when failures occur and remain responsive.

  • high performance

    Up to 50 million messages per second can be processed on a single computer. Small memory footprint; approximately 2.5 million actors (participants) can be created per GB heap.

  • elasticity and dispersion

    Distributed systems have no single point of failure, with load balancing and adaptive routing across nodes. Event sourcing and CQRS (Command Query Responsibility Segregation) with cluster sharding. Use CRDT (Conflict-free Replicated Data Types, conflict-free replicated data types) to achieve eventually consistent distributed data.

  • reactive flow data

    Asynchronous non-blocking stream processing with backpressure. Fully asynchronous and stream-based HTTP server and client provide a great platform for building microservices.

Insert image description here


Problems with traditional programming models

Challenges to packaging characteristics

  • Encapsulation in object-oriented programming requires that data can only be accessed indirectly through the methods provided by the object, but under multi-threading, multiple threads modify the internal data of the object at the same time, which will lead to thread safety issues.
  • The way to solve the thread safety problem is to use locks, but the use of locks will affect performance, may lead to deadlock, and is difficult to extend to distributed systems.

Misunderstandings about shared memory in modern computer architectures

  • In multi-core CPU architectures, there is no longer true shared memory between multiple threads, but data is passed through cache lines, making the memory visibility of shared variables an issue.
  • Using the volatile keyword or atomic data structure can solve the memory visibility problem of shared variables, but it will harm performance. You need to carefully choose the variables that need to be modified.

Misconceptions about the call stack

  • The traditional call stack model is not suitable for concurrent programming because asynchronous tasks cannot pass exceptions or notify the main thread through the call stack.
  • When an asynchronous task fails to execute, the task status may be lost, and new error signaling mechanisms and methods for recovering from failures need to be introduced.

These issues highlight the advantages of the Actor model, as it provides a more adaptable way to concurrent programming, solving the above challenges through message passing, rather than relying on shared memory and traditional call stacks. The actor model has been proven in dealing with concurrent and distributed systems.


The Actor model solves the problems of traditional programming models

Actor model

  • The Actor model is used to handle concurrent calculations. Each Actor represents a basic computing unit that can receive messages and perform calculations based on the messages.
  • Actors are isolated from each other, do not share memory, and each Actor has its own private state variables.
  • Each Actor has its own address and communicates by sending messages to each other through the address. The messages are delivered asynchronously.
  • The actor model allows building distributed systems that are not limited to a single JVM.

Insert image description here

[Actor system diagram]

Use message passing to avoid locks and blocking

  • Communication between actors is through message passing rather than method calls, which will not cause the calling thread that sends the message to be blocked.
  • Actors maintain encapsulation because message processing is serial and no locks are required to synchronize multi-threaded access.
  • The state of the Actor is local and not shared. Data is passed through messages, which is consistent with the way memory works in modern systems.
  • Actors can efficiently handle large amounts of messages, fully utilizing the potential of multi-core CPUs.

Handle errors gracefully using Actors

  • There is no shared call stack in the Actor model, so errors are handled differently.
  • The target Actor can reply with an error message to indicate an error condition, and the error is handled as a normal message.
  • The Actor model adopts a tree-like hierarchical supervision mechanism, and the parent Actor can monitor and handle the faults of the child Actors.
  • The supervisor can decide whether to restart or stop a child actor, ensuring the recoverability and robustness of the system.

summary

Overall, Akka is a powerful framework for building highly concurrent, distributed, scalable, and fault-tolerant applications. It is widely used in finance, social media, online games and other fields, and is a powerful tool for building responsive systems. If you need to build this kind of application, understanding and using Akka can be very helpful.

Insert image description here

おすすめ

転載: blog.csdn.net/yangshangwei/article/details/132755301