17
Actor Pattern and NATS Charlie Strawn and Steven Osborne

Actor Patterns and NATS - Boulder Meetup

  • Upload
    apcera

  • View
    708

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Actor Patterns and NATS - Boulder Meetup

Actor Pattern and NATSCharlie Strawn and Steven Osborne

Page 2: Actor Patterns and NATS - Boulder Meetup

Basic Outline

● Introduction to Actor Model● Similarities to decoupled pub-sub● Simple NATS-Akka connector● NATS Actor implementation

Page 3: Actor Patterns and NATS - Boulder Meetup

Actor Model

Page 4: Actor Patterns and NATS - Boulder Meetup

Actor Model

The actor model is comprised of a system of actors. An actor is a primitive that embodies

1. Processing2. Storage3. Communication

In an actor system everything is an actor. Actor people love the “one ant is no ant” analogy: One actor is no actor - they come in systems.

Page 5: Actor Patterns and NATS - Boulder Meetup

Actor Model

When an actor receives a message it can

1. Send messages to other actors2. Create new actors3. Designate the behavior to be used for the next message it receives

This is the real key. The actor is always doing work reacting to some message.

Page 6: Actor Patterns and NATS - Boulder Meetup

Actor Model - Key points

● Actors can live anywhere. ● There is no shared state among actors. Let it crash.● Each actor has an address at which it receives messages. ● There is a many-to-many relationship between actors and addresses.● Messages are delivered at most once.● Actors process messages serially.

Page 7: Actor Patterns and NATS - Boulder Meetup

Actor Model - Key points

● The model is inherently concurrent (yey!) so keep computation lightweight and consistent

● Throughput is king● Each actor processes messages serially - so avoid blocking (except for

unavoidable ops like I/O)● The unit of concurrency is actor - so all computation within the actor is serial.

If the actor has too much work and needs more processing power it creates and messages other actors.

Page 8: Actor Patterns and NATS - Boulder Meetup

Good synopsis...

If we’ve piqued your interest in actors but find our summary lacking content - check out this video from Carl Hewitt - the original designer of the Actor Model.

https://www.youtube.com/watch?v=7erJ1DV_Tlo

Or just do the sane thing and search youtube for “the actor model”. It’s the first one.

Page 9: Actor Patterns and NATS - Boulder Meetup

Akka

● Akka is Java/Scala library that implements the actor model. Akka is especially well suited to an EventBus style implementation of pub sub. http://doc.akka.io/docs/akka/current/scala/event-bus.html

● 1-to-1 Message to Event● Effective Akka

Page 10: Actor Patterns and NATS - Boulder Meetup

Ok, great, isn’t this a NATS talk?

NATS is clearly not an actor model - it’s a communication protocol. That being said, it upholds many of the basic tenants embraced by the actor model.

● Decoupled sender/receiver (i.e. publisher-subscriber)● Publishers and subscribers can live anywhere● Messages are delivered at most once● Lightweight and high throughput

Page 11: Actor Patterns and NATS - Boulder Meetup

NATS-Akka

Let’s take the low hanging fruit here - we should be able to connect NATS to an existing actor model implementation. Better yet, let’s take the lowest of the fruit - NATS connector framework (in Java) to Akka (also Java).

Lets combine the speed and simplicity of NATS with the speed and simplicity of Akka.

Page 12: Actor Patterns and NATS - Boulder Meetup

NATS-Akka-Connector

Demo!

Page 13: Actor Patterns and NATS - Boulder Meetup

Akka Clustering

● Akka has a built-in clustering module - but it’s based on complicated on somewhat complicated tcp routing configuration.

● With NATS as the communication point between nodes - clustering akka is simple.

Page 14: Actor Patterns and NATS - Boulder Meetup

Connecting to an actor system is cool, but...

What if the actor system was built on top of NATS?

Then multi-node would be a cinch. Setup would be quick. Message latency would be low. Actor Systems could span languages (i.e. have java actors that interacts with legacy apis, go actors to do some heavy lifting, and some rust actors to just be awesome).

Page 15: Actor Patterns and NATS - Boulder Meetup

Let’s try it out

Demo!

https://github.com/sosborne/nats-actor/tree/actor

Page 16: Actor Patterns and NATS - Boulder Meetup

More work

● Actually implement akka clustering using NATS as the message transport across cluster nodes

● Multi-node go nats-actor implementation● More languages for nats-actor (maybe rust? Shameless plug…)