Distributed Applications in C#, Intro to Aktores

Actor Model in C#

Created by @ajlopez

https://github.com/ajlopez/Talks/AktoresIntro

Using reveal.js

Agenda

  • Distributed Applications
  • Actor Model
  • Aktores in C#

Sources

Distributed Applications

Distributed Computing is the New Normal

  • You already have a distributed system
  • Mobile
  • NoSQL Databases
  • SQL Replications
  • Cloud
  • REST Services

Needs

  • Elasticity (beyond a single node)
  • Availability (resilience if one node fails)

Programming Distributed Computing Systems

Programming Distributed Computing Systems

Motivation

  • Moore's Law
  • 1971: 2 300 transistors per chip
  • 2011: 2 600 000 000 transistors per chip
  • Network
  • ARPANET 1968: 50 000 bits per second
  • US-Japan backbone 2011: 500 000 000 000 bits per second
  • Storage
  • First hard drive 1956: 4 400 000 bytes
  • Hard drive 2011: 4 000 000 000 000 bytes

The Future

"At the same rate of growth, by 2031, we may see a thousandfold further improvement in hardware, resulting in 1,000 plus-core processors in mobile devices, laptops, and desktops, 100-terabit-per-second-plus network backbones, and hard drives storing petabytes of data. The growing number of transistors in emerging multicore architectures can only be translated into practical software applications’ performance if software developers understand how to program concurrent systems effectively."

Concurrency vs Parallelism

Concurrencty: n threads are making progress

Parallelism: n threads are executing SIMULTANEOSLY

Distributed Computing

"Internet computing, web computing, grid computing, and cloud computing are all forms of distributed computing"

"One significant advantage of distributed computation is the potential scalability afforded by applications"

Actor Model

Actor Model

  • Formalized by Carl Hewitt (1973)
  • Refined by Gul Agha (mid 80s)
  • First major adoption: Ericsson (mid 80s)
  • Invented Erlang (open sourced in 90s)
  • Built distributed, concurrent, fault-tolerant telcom system 99.99....% uptime

Actor

Actor

  • Lightweight Object
  • No Shared State
  • Messages in Mailbox
  • They are processed in order
  • Only one message at a time

Actors and Messaging

Supervisor and Workers

Akka

http://akka.io/

Akka

  • Founded by Jonas Boner (@jboner), now part of Typesafe stack
  • Actor implementation on Java Virtual Machine
  • Java API
  • Scala API
  • Local and Remote Actors
  • Cluster
  • Actor Persistence
  • Supervision

Aktores

Actor Model in C#

https://github.com/ajlopez/Aktores

Actor

## Actor A custom class that inherits abstract class Actor. ```csharp public class LambdaActor : Actor { private Action<object> fn; public LambdaActor(Action<object> fn) { this.fn = fn; } // Override this methods public override void Receive(object message) { this.fn(message); } } ```

Actor System

## Actor System Top of actor hyerarchy. Create ```csharp ActorSystem system = new ActorSystem(); ``` Create Actors ```csharp MyActor actor = new MyActor(); ActorRef actorref = system.ActorOf(actor, "actor1"); ActorRef actorref2 = system.ActorOf(typeof(MyActor), "actor2"); ``` Retrieve Actors ```csharp ActorRef actorref = system.ActorSelect("/myactor/mychildactor"); ```

Actor Context

## Actor Context Each Actor has an Actor Context. ```csharp this.Context ``` Create Actors ```csharp MyActor actor = new MyActor(); ActorRef actorref = this.Context.ActorOf(actor, "actor1"); ActorRef actorref2 = this.Context.ActorOf(typeof(MyActor), "actor2"); ``` Retrieve Actors ```csharp ActorRef actorref = this.Context.ActorSelect("/otheractor/childactor"); ActorRef actorref2 = this.Context.ActorSelect("mychildactor"); ```
## Actor Reference Reference to Actor, tell message. Create Actors ```csharp MyActor actor = new MyActor(); ActorRef actorref = system.ActorOf(actor, "actor1"); ``` Send Message ```csharp actorref.Tell("hello"); actorref.Tell("hi", this.Self); // this actor ActorRef ```
## Routers Send a message to a Router, then it is received by one Actor in Router list. Create router and register ActorRefs ```csharp var router = new RouterActor(); router.Register(actorref); router.Register(actorref2); var routerref = system.ActorOf(router, "actor"); //... ``` Send Message to Router ```csharp routerref.Tell("hello"); ``` Only one Actor processes the message
## Remote Actors Each Actor has a Path, but an ActorSystem can be publish in an Address. Publish System via TCP ```csharp ActorSystem system = new ActorSystem(); TcpServer server = new TcpServer("localhost", 3000, system); ``` Send Message to Remote Actor ```csharp ActorRef actorref = localsystem.ActorSelect("aktores.tcp//sys@localhost:3000/remoteactor"); actorref.Tell("hello"); ```

Example: Web Crawler

Resources

My Delicious Links

The End

BY Angel 'Java' Lopez / www.ajlopez.com / @ajlopez