You are previewing Developing Microservices with Node.js.
O'Reilly logo
Developing Microservices with Node.js

Book Description

Learn to develop efficient and scalable microservices for server-side programming in Node.js using this hands-on guide

About This Book

  • Real world example explained chapter after chapter with code examples.

  • Useful concepts for other languages like Java or PHP

  • Easy to follow by people with little to none experience in Node.js

  • Node.js Version 0.12.2 and the latest compatible versions of Seneca and PM2

  • Who This Book Is For

    Node.js developer with basic server-side development knowledge - but no knowledge of implementing microservices in Node.js applications. Also useful for developers in other languages like Java or C#.

    What You Will Learn

  • Identify where the microservice oriented architectures can tackle the most common problems in the software used by the big organisations.

  • Re-architecture an existing monolithic system into a microservices oriented software.

  • Build robust and scalable microservices using Seneca and Node.js.

  • Testing of the microservices in insolation in order to create a solid system.

  • Deploy and manage microservices using PM2

  • Monitoring the health of a microservice (CPU, memory, I/O…) and how the degradation of the performance in one microservice could degrade the performance of full system.

  • In Detail

    Microservices architecture is a style of software architecture. As the name suggests, microservicess refers to small services. For a large implementation, this means breaking the system into really small, independent services. Alternative to monolithic architecture (where the entire system is considered as a single big, interwoven segment), microservices approach is getting more and more popular with large, complex applications that have a very long lifecycle, which require changes at regular intervals. Microservices approach allows this type of changes with ease as only a part of the system undergoes changes and change control is easy.

    An example of such large system can be an online store—includes user interface, managing product catalog, processing orders, managing customer's account. In a microservices architecture each of these tasks will be divided and into smaller services. Also, these services will be further broken down into independent services—for user interface, there will be separate services for input, output, search bar management, and so on. Similarly, all other tasks can be divided in very small and simple services.

    Style and approach

    This book is a comprehensive guide to developing production-ready implementation of the microservices architecture with Node.js. Each chapter builds up on the earlier one creating a strong foundation on which you create your own microservice in a distributed environment.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Developing Microservices with Node.js
      1. Table of Contents
      2. Developing Microservices with Node.js
      3. Credits
      4. About the Author
      5. About the Reviewer
        1. eBooks, discount offers, and more
          1. Why subscribe?
      7. 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. Errata
          3. Piracy
          4. Questions
      8. 1. Microservices Architecture
        1. Need for microservices
          1. Monolithic software
          2. Microservices in the real world
          3. Microservice-oriented architectures
          4. How is it better?
          5. Shortcomings
        2. Key design principles
          1. Business units, no components
          2. Smart services, dumb communication pipes
          3. Decentralization
          4. Technology alignment
          5. How small is too small?
        3. Key benefits
          1. Resilience
          2. Scalability
          3. Technology heterogeneity
          4. Replaceability
          5. Independence
            1. Why is replaceability important?
            2. Easy to deploy
        4. SOA versus microservices
        5. Why Node.js?
          1. API aggregation
          2. The future of Node.js
        6. Summary
      9. 2. Microservices in Node.js – Seneca and PM2 Alternatives
        1. Need for Node.js
          1. Installing Node.js, npm, Seneca, and PM2
            1. Learning npm
          2. Our first program – Hello World
          3. Node.js threading model
          4. Modular organization best practices
            1. Javascript
            2. SOLID design principles
        2. Seneca – a microservices framework
          1. Inversion of control done right
          2. Pattern matching in Seneca
            1. Patrun – a pattern-matching library
            2. Reusing patterns
            3. Writing plugins
            4. Web server integration
              1. Seneca as Express middleware
              2. Data storage
        3. PM2 – a task runner for Node.js
          1. Single-threaded applications and exceptions
          2. Using PM2 – the industry-standard task runner
        4. Summary
      10. 3. From the Monolith to Microservices
        1. First, there was the monolith
          1. How to tackle organic growth?
          2. How abstract is too abstract?
        2. Then the microservices appeared
          1. Disadvantages
          2. Splitting the monolith
          3. Problems splitting the monolith – it is all about the data
        3. Organizational alignment
        4. Summary
      11. 4. Writing Your First Microservice in Node.js
        1. Micromerce – the big picture
        2. Product Manager – the two-faced core
          1. Fetching products
          2. Fetching by category
          3. Fetching by ID
          4. Adding a product
          5. Removing a product
          6. Editing a product
          7. Wiring everything up
          8. Integrating with Express – how to create a REST API
        3. The e-mailer – a common problem
          1. How to send e-mails
          2. Defining the interface
          3. Setting up Mandrill
          4. Hands on – integrating Mandrill in your microservice
          5. The fallback strategy
        4. The order manager
          1. Defining the microservice – how to gather non-local data
          2. The order manager – the code
            1. Calling remote services
            2. Resilience over perfection
        5. The UI – API aggregation
          1. Need for frontend microservice
          2. The code
          3. Service degradation – when the failure is not a disaster
          4. Circuit breakers
          5. Seneca – a simple puzzle that makes our lives easier
          6. Seneca and promises
        6. Debugging
        7. Summary
      12. 5. Security and Traceability
        1. Infrastructure logical security
          1. SSH – encrypting the communications
        2. Application security
          1. Common threats – how to be up to date
            1. Injection
              1. Input validation
            2. Cross-site scripting
              1. Output encoding
            3. Cross-site request forgery
            4. Open redirects
          2. Effective code reviews
        3. Traceability
          1. Logging
          2. Tracing requests
          3. Auditing
          4. HTTP codes
            1. 1xx – informational
            2. 2xx – success codes
            3. 3xx – redirection
            4. 4xx – client errors
            5. 5xx – server errors
            6. Why HTTP codes matter in microservices
        4. Summary
      13. 6. Testing and Documenting Node.js Microservices
        1. Functional testing
          1. The pyramid of automated testing
            1. Unit tests
            2. Integration tests
            3. End-to-end tests
            4. How much testing is too much?
          2. Testing microservices in Node.js
            1. Chai
              1. BDD-style interfaces
              2. Assertions interface
            2. Mocha
            3. Sinon.JS – a mocking framework
            4. Testing a real microservice
              1. TDD – Test-driven development
              2. Unit testing
              3. End-to-end testing
            5. Manual testing – the necessary evil
              1. Building a proxy to debug our microservices
              2. Postman
        2. Documenting microservices
          1. Documenting APIs with Swagger
          2. Generating a project from the Swagger definition
        3. Summary
      14. 7. Monitoring Microservices
        1. Monitoring services
          1. Monitoring using PM2 and Keymetrics
            1. Diagnosing problems
            2. Monitoring application exceptions
            3. Custom metrics
              1. Simple metric
              2. Counter
              3. Average calculated values
        2. Simian Army – the active monitoring from Spotify
          1. Throughput and performance degradation
        3. Summary
      15. 8. Deploying Microservices
        1. Concepts in software deployment
          1. Continuous integration
          2. Continuous delivery
        2. Deployments with PM2
          1. PM2 – ecosystems
          2. Deploying microservices with PM2
            1. Configuring the server
        3. Docker – a container for software delivery
          1. Setting up the container
            1. Installing Docker
            2. Choosing the image
            3. Running the container
            4. Installing the required software
            5. Saving the changes
          2. Deploying Node.js applications
          3. Automating Docker container creation
        4. Node.js event loop – easy to learn and hard to master
        5. Clustering Node.js applications
        6. Load balancing our application
          1. Health check on NGINX
            1. Passive health check
            2. Active health check
        7. Summary
      16. Index