280 likes | 293 Views
Explore the benefits of Akka's event-driven model for building distributed applications on AWS, supporting multi-tenancy with low latency. Learn the history, program at a higher level, and why Akka is ideal for reactive architectures.
E N D
Micro Services The Akka Way
Our Use case • Building a new web application for Campaign Management System • Micro Services Architecture on AWS • Support multi-tenancy • High volumes activities with minimal user latency • AWS deployment • CI/CD
It’s time to AKKA Akka is a toolkitand runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on the JVM From akka.io site
Akka History • First Release on January 2010 • Written in Scala (can be used in java) • Part of Typesafe platform together with Play framework and Scala • Created by Jonas Boner
AKKA - Program at a Higher Level • Never think in terms of shared state, state visibility, threads, locks, concurrent collections, thread notifications etc. • Low level concurrency plumbing BECOMES SIMPLE WORKFLOW - you only think about how messages flow in the system • You get high CPU utilization, low latency, high throughput and scalability - FOR FREE as part of the model • Proven and superior model for detecting and recovering from errors
Why Akka? • A sync architecture by nature • Business logic model implementation using Actors is easy! • Small building blocks • Working in a single/multiple JVM seamlessly • Scheduler • Easy Unit Tests
What is Akka? Reactive • Akka’s unit of computation is called Actor • Actors are purely reactive components: • a mailbox • Behavior & State • Scheduled to run when a message is sent • Each actor has a parent, handling its failures
Actor no mutable data is shared and no synchronization is used Event –driven thread Mailbox Actor Behavior State
What is Akka? Actor example in Java Define the message the Actor responds to public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor{ public void onReceive(Object message) throws Exception { if (message instanceof Greeting) Sout("Hello " + ((Greeting) message).who); } } Actor Class definition
Actor System Architecture – transport layer Specific Auth. Actor Specific Auth. Actor Authentication Service Actor Campaign Service BL Routers Forward Specific Auth. Actor Creates Specific Auth. Actor Service Actor Manager
A word about routers There are many routers out there • RoundRobinRouting • RandomRouting • SmallestMailboxRouting • BroadcastRouting • ScatterGatherFirstCompletedRouting • TailChoppingRouting • ConsistentHashingRouting
Code Sample – creating a router private ActorRefinitializeRouter(IServiceNameserviceName) { ActorRefrouterToReturn = system.actorOf( new ClusterRouterPool( new RoundRobinPool(RouterPoolSize).withSupervisorStrategy(strategy), new ClusterRouterPoolSettings(totalInstances, maxInstancesPerNode, allowLocalRoutees, serviceName.name())) .props(Props.create(BEActor.class)), "BEActorClusterAware"+serviceName ); return routerToReturn; }
Router Strategy private SupervisorStrategy strategy = new OneForOneStrategy(10 ,Duration.create("1 minute"), new Function<Throwable, SupervisorStrategy.Directive>() { @Override public SupervisorStrategy.Directive apply(Throwable t) { if (t instanceofActorInitializationException) { return SupervisorStrategy.stop(); } else if (t instanceof Exception) { return SupervisorStrategy.restart(); } else { return SupervisorStrategy.escalate(); } } });
Actor System Architecture – transport layer Specific Auth. Actor Specific Auth. Actor Authentication Service Actor Campaign Service Backend BL Routers Forward Specific Auth. Actor Creates Specific Auth. Actor Service Actor Manager
Calling the BE Service public Future<ServiceResponse> callServiceFuture(ServiceRequestserviceRequest) { ActorRefrouter = getRouter(serviceRequest.getServiceName()); //get scala future from Akka actors Future<Object> futureFromActor= Patterns.ask(router, serviceRequest, timeout); //mapping it to Future<ServiceResponse> return futureFromActor.map(new Mapper<Object, ServiceResponse>() { @Override public ServiceResponse apply(Object parameter) { if (parameter instanceofServiceResponse) { return (ServiceResponse) parameter; } } }, executionContext); }
Actor System Architecture – transport layer Specific Auth. Actor Specific Auth. Actor Authentication Service Actor Campaign Service Backend BL Routers Forward Specific Auth. Actor Creates Specific Auth. Actor Service Actor Manager
@ The BE public class BEActor extends UntypedActor { @Override public void onReceive(Object message) throws Exception { if (message instanceofServiceRequest) { String moduleName = ((ServiceRequest) message).getServiceName().moduleName(); String routerPath = AkkaUtils.getBERouterFullPathByType(moduleName); ActorRefspecificBERouter = getContext().actorFor(routerPath); specificBERouter.forward(message, getContext()); } } }
Akka cluster A set of nodes joined together through themembership service
Akka Cluster • Took Akkaremoting a Huge step forward • Tested with more than 200 nodes in a single cluster • Nodes communicates via Gossipprotocol • Cluster Leader - Managing cluster convergence, partitions, fail-over,rebalancing • Members states – joining, up, leaving, down, removed
Master Responsibilities – Feel The Force • Cluster connection point • Dead letter queue • Monitoring • Trigger schedule jobs
Find The Force… Master Discovery Service Join the force… BE Node BE Node BE Node It’s the yellow guy Where is my master
Amazon AWS and Akka • Multiple services running on the same machine • Each service communicate with different tcp port • Nodes communicates via Gossip every 3 sec (configurable) • Automatic shutdown unreachable nodes after a predefined threshold (60s) • nodes cannot be join the cluster during this time • akka.remote.watch-failure-detector.threshold configuration need to change -> 20ms • Cluster healthy configuration • At least master node is up
Few words to wrap up… • In the world of multi core computers: • Async is inevitable • There are several potential solutions: Actor model, Rx, Futures, mange your own threads (oh no…) etc. • Akka is a simple well proven solution to handle this heavy task with a feeling of imperative programing. • The cluster in a discovery environment is not trivial and requires design. • So should I use it? You Betcha!