1 / 28

Micro Services

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.

shirleyr
Download Presentation

Micro Services

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Micro Services The Akka Way

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. Actor no mutable data is shared and no synchronization is used Event –driven thread Mailbox Actor Behavior State

  9. 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

  10. Actor libraries and frameworks

  11. The Big Picture – almost true…

  12. 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

  13. A word about routers There are many routers out there • RoundRobinRouting • RandomRouting • SmallestMailboxRouting • BroadcastRouting • ScatterGatherFirstCompletedRouting • TailChoppingRouting • ConsistentHashingRouting

  14. 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; }

  15. 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(); } } });

  16. 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

  17. 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); }

  18. 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

  19. @ 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()); } } }

  20. Akka cluster A set of nodes joined together through themembership service

  21. 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

  22. Master Responsibilities – Feel The Force • Cluster connection point • Dead letter queue • Monitoring • Trigger schedule jobs

  23. Find The Force… Master Discovery Service Join the force… BE Node BE Node BE Node It’s the yellow guy Where is my master

  24. The Big Picture – the true story

  25. 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

  26. 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!

More Related