O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Scala Microservices

Book Description

Design, build, and run Microservices using Scala elegantly

About This Book

  • Build robust microservices using Play Framework and Lagom
  • Model your data for highly interactive applications and scale using Event Sourcing & CQRS
  • Build applications that are resilient to failures by using Message Passing for communication
  • Deploy and manage Scala Microservices for scale by using docker containers with Kubernetes for orchestration

Who This Book Is For

It is assumed that the reader knows Scala or is proficient in a competent programming language such as Java, C#, Ruby, and so on, with some exposure to Scala. Some experience with writing web services would also be ideal but not mandatory.

What You Will Learn

  • Learn the essentials behind Microservices, the advantages and perils associated with them
  • Build low latency, high throughput applications using Play and Lagom
  • Dive deeper with being asynchronous and understand the superiority it provides
  • Model your complex domain data for scale and simplicity with CQRS and Event Sourcing
  • Be resilient to failures by using message passing
  • Look at best practices of version control workflow, testing, continuous integration and deployments
  • Understand operating system level virtualization using Linux Containers. Docker is used to explain how containers work
  • Automate your infrastructure with kubernetes

In Detail

In this book we will learn what it takes to build great applications using Microservices, the pitfalls associated with such a design and the techniques to avoid them.

We learn to build highly performant applications using Play Framework. You will understand the importance of writing code that is asynchronous and nonblocking and how Play leverages this paradigm for higher throughput. The book introduces Reactive Manifesto and uses Lagom Framework to implement the suggested paradigms. Lagom teaches us to: build applications that are scalable and resilient to failures, and solves problems faced with microservices like service gateway, service discovery, communication and so on. Message Passing is used as a means to achieve resilience and CQRS with Event Sourcing helps us in modelling data for highly interactive applications.

The book also shares effective development processes for large teams by using good version control workflow, continuous integration and deployment strategies. We introduce Docker containers and Kubernetes orchestrator. Finally, we look at end to end deployment of a set of scala microservices in kubernetes with load balancing, service discovery and rolling deployments.

Style and approach

The book will step through each of the core microservice concepts in Scala, building an overall picture of their capabilities. This book adopts a systematic approach, allowing you to build upon what you've learnt in previous chapters. By the end of this book you'll have an understanding of the complex aspects of building microservices in Scala and will be able to take that knowledge with you into further projects.ng of the complex aspects of building Microservices in Scala and will be able to take that knowledge with you onto whatever project calls for it

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Introduction to Microservices
    1. Business idea
      1. Data collection
      2. Linking users across sites
      3. Rank developers
      4. User interaction
    2. Implementation
    3. Development issues
      1. Configuration and maintenance hazards
      2. Difficult to get started
      3. New functionality
      4. Restart and update
      5. Testing and deployment
      6. Scalability
    4. Isolation
      1. Isolation in space
      2. Isolation in time
      3. Overview of application design till now
    5. Microservices
      1. Code example
      2. Restructuring
      3. What exactly are microservices
      4. Sharing of a database
      5. Defining microservice
      6. Micro in microservice
      7. Polyglot
      8. The dark side of microservices architecture
      9. Why Scala
    6. Summary
  3. Introduction to Play Framework
    1. Quick introduction to Play 2.66
      1. Getting started
      2. Hello world
      3. Structure of the Play project
      4. Routing
        1. Routes
        2. HTTP POST
        3. Actions
        4. Auth
        5. Templates
      5. REST
    2. JSON marshalling/Unmarshalling
      1. Reading JSON
        1. Macro to generate Reads
      2. Generating JSON
        1. Macro to generate Write
    3. Play production mode
    4. Summary
  4. Asynchronous and Non-Blocking
    1. Being asynchronous
      1. Scenario 1 - synchronous
      2. Scenario 2 - synchronous
      3. Scenario 3 - asynchronous
      4. Scenario 4 - waiting
    2. Being asynchronous in Scala
      1. ExecutionContext
        1. Synchronous ExecutionContext
      2. Future
        1. Functional composition with Futures
      3. Blocking
        1. scala.concurrent.blocking
        2. Non-blocking I/O
        3. Blocking and synchronous, non-blocking and asynchronous
    3. Work Stealing - what makes Play fast!
      1. Scheduling improvements - 1
      2. Scheduling improvements - 2
      3. Work Stealing in Play
      4. Play thread pools
      5. Mixing synchronous and asynchronous code
      6. Asynchronous WebService calls
    4. Summary
  5. Dive Deeper
    1. Talent search engine
    2. Project structure
      1. The build.sbt file
    3. Brief overview of the application
    4. Security
      1. Inter-microservice authentication
      2. The auth-app microservice explained
      3. Action.async
    5. Brief introduction to Slick
      1. Slick evolutions
      2. Slick is asynchronous
        1. The web-app microservice
        2. The rank-app microservice
        3. The stackoverflow-app (so-app) microservice
        4. github-app (github-app)
        5. The commons project
    6. Pitfalls
    7. Summary
  6. Reactive Manifesto
    1. Reactive hype?
    2. Reactive Manifesto
    3. Manifesto explained
      1. Elastic
      2. Resilience
      3. Message-driven
        1. Brief overview of Akka
        2. Akka versus message brokers
        3. Isolation
          1. Flow control
        4. Location Transparency
        5. Immutability
        6. Event driven versus message-driven
    4. Summary
  7. Introduction to Lagom
    1. Why Lagom?
    2. Brief overview of Lagom
    3. Lagom Service API
    4. Minimized chirper application
      1. Anatomy of a Lagom project
        1. Dependency injection in Lagom
        2. API and Impl
      2. Defining services
        1. ServiceCall
      3. Brief overview of Macwire
      4. Implementing friend-impl
      5. Akka Streams
      6. Chirp service
      7. Activity Stream
    5. Frontend
      1. Running the application
    6. Multi project builds
    7. Summary
  8. CQRS and Event Sourcing
    1. Data modelling
      1. Bounded context
        1. Domain-driven design
    2. Event Sourcing
      1. Advantages of Event Sourcing
      2. Event
    3. CQRS
      1. CQRS example
      2. Pain points with CQRS
    4. Event Sourcing and CQRS
      1. Conclusion
    5. Lagom Persistence API
      1. Getting started with setup
      2. Managing Friends with CQRS
        1. Command
        2. Events
        3. State
        4. PersistentEntity
        5. Implementing FriendService
    6. Summary
  9. Effective Communication
    1. Isolation
      1. Isolation - part 2
    2. Message brokers
    3. Apache Kafka
      1. Conclusion
    4. Lagom Message Broker API
    5. Friend Recommendation
      1. build.sbt
        1. Friend-api
        2. The friend-recommendation-api
    6. Summary
  10. Development Process
    1. Getting to know the key ingredients
    2. The feedback loop
      1. Code versioning
      2. Testing strategies
      3. Continuous integration and deployments
        1. Deployments
        2. Jenkins pipelines
        3. Jenkins pipeline - In action
    3. Traditional deployments and machine images
      1. Dependency versus package manager
    4. Containers
      1. Introducing hardware virtualization
      2. OS-level virtualization
        1. What do we expect from containers?
      3. Container runtime - Docker
        1. What made Docker a possibility?
      4. Container images
      5. Deployments with containers
      6. Container concerns
    5. Summary
  11. Production Containers
    1. Distributed systems and their essentials
      1. Distributed system - definition
      2. Reasons to distribute
      3. Components of a distributed system
      4. Domain name service
    2. Server automation
      1. What does an automation framework look like?
      2. Infrastructure as code - Ansible
        1. Ansible primitives
        2. Ansible - creating a Pageview counter
        3. Ansible cloud modules
    3. An introduction to cluster orchestration
    4. Kubernetes
      1. K8s internals
        1. API objects
        2. Config management
    5. K8s setup guide
    6. K8s example app
    7. K8s monitoring
    8. K8s security
      1. Network level
      2. Service level
        1. Certificates
        2. Authentication
        3. Authorization using RBAC
    9. Caveats
    10. Summary
  12. Example Application in K8s
    1. Talent search engine example
    2. Dockerize
      1. Creating a Docker image
      2. Testing the Docker image
      3. Pushing the Docker image
    3. Deployment topology
    4. K8s configurations
      1. Auth-service
    5. Installation of K8s
    6. Deploy!
    7. Rolling deployments
    8. Persistent workloads
      1. Persistent disks
    9. Summary