Book Image

Building Scalable Apps with Redis and Node.js

By : Joshua Johanan
Book Image

Building Scalable Apps with Redis and Node.js

By: Joshua Johanan

Overview of this book

Table of Contents (17 chapters)
Building Scalable Apps with Redis and Node.js
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Node.js is a fantastic way to begin your journey in building scalable applications. As it was built using non-blocking I/O, it can serve more concurrent requests than a blocking server, such as Apache, which can be achieved through asynchronous events. When a function call blocks, Node.js will reply via a callback. The great foundation of Node.js has led to many libraries that allow you to build a scalable application.

Redis is another vital building block of scalable applications. While Redis is not Node.js-specific, it offers great support. It fills up the need of shared memory between web servers that scalable applications require. Redis also has bindings in some other frameworks, which will be covered in this book. This prevents us from falling into the trap of needing five different data stores for five different frameworks, which only adds to the complexity of our environment and system administration overhead.

Building Scalable Apps with Redis and Node.js combines both of these essential building blocks into one comprehensive and straightforward project guide. It shows you how to build an application from scratch, straight from having an idea all the way to deploying it. The first seven chapters show you a specific problem or issue you might come across during development. The following chapter will cover some best practices when developing. The final two chapters will cover the deployment of your site to Amazon Web Services. These particular chapters will also show you how you can troubleshoot and debug your application. Although the book covers one project, the chapters are organized in such a way that you can jump straight into them, to be shown how to create a certain feature or learn a specific topic. It is recommended, though, that you work from the beginning of the book. You will find that because we will be building an entire site, the code from one chapter will have relevance to another. However, most code samples will be usable in other contexts.

On a personal note, I will say that using JavaScript for backend development is refreshing. I readily admit that when I first heard about Node.js and the idea of using JavaScript in the backend, my initial reaction was "Why would I want to do that?" I have run the gamut of server-side languages (PHP to Ruby and Python, and C# .NET). I have worked with many different web servers (Apache, IIS, built-in servers, Unicorn, Gunicorn, and so on). Throw in the fact that all modern web applications require some JavaScript and you start to build mental barriers, which is especially true if the syntax is different (consider comparing Ruby to JavaScript, for example). Node.js allows you to remain in the JavaScript mode, utilizing the same design patterns and even the same tools. You will also be glad to hear there is not much to set up. You build your Node.js app and then you simply run it. All these reasons are exactly why Node.js is one of the best web application/serving frameworks and why I, personally, love to use it.

What this book covers

Chapter 1, Backend Development with Express, shows us how to serve our pages using Express. Express is a full-featured web application framework that provides us with many features while writing very little code. It also has a rich middleware system that others have extended. This middleware allows us to work with form data as well as use templates and sessions. We will build the foundation of the application, on which all the other chapters will be based.

Chapter 2, Extending Our Development with Socket.IO, shows us how to build real-time applications using WebSockets. WebSockets are the next step in the evolution of dynamic web pages that allow users to interact instantaneously. This chapter also covers the use of tying Socket.IO to the sessions that Express creates.

Chapter 3, Authenticating Users, shows us how to build a login page that actually works! We will be using the Passport framework to build our authentication functions. Passport has performed a lot of heavy lifting in building connectors to different providers. Many of these implemented OAuth or OAuth 2.0. If you have ever had to develop against these different OAuth providers, you can appreciate the work that went into each library. You will also be shown how to store local passwords securely.

Chapter 4, RabbitMQ for Message Queuing, covers the topic of message queues. These are a requirement of any scalable application, allowing you to break your application up, that serves both its complexity and scope. In this chapter, we will cover some great use cases for this. In addition, you will be able to build your own message queues and tie them to functions.

Chapter 5, Adopting Redis for Application Data, shows us how to use the store information and retrieve it from Redis. This is important, as the Redis data storage engine is unlike any relational database. Thinking of it as such can actually create issues! We will cover the commands you will use the most throughout your application, as well as take a look at how Redis implements message queuing in your application.

Chapter 6, Using Bower to Manage Our Frontend Dependencies, begins to take a look at how you can begin the frontend development of your application. We will not have an application without a frontend. We will talk about the frameworks that will be used and why they are chosen.

Chapter 7, Using Backbone and React for DOM Events, covers the backbone, if you can excuse the pun, of the frontend of our application. The two most important tasks when using JavaScript in a browser are DOM manipulation and responding to events. You will learn how to listen for real-time events and then interact with the page. Backbone and React will help us build the maintainable code to do this.

Chapter 8, JavaScript Best Practices for Application Development, shows us how to build better JavaScript. JavaScript, as a scripting language, will run despite making many mistakes, which is both a good and a bad thing. However, you will still need to know if you have forgotten a semicolon or caused a runtime error. We will achieve this by building a repeatable build system. You will also be shown modules and how to module proof the code.

Chapter 9, Deployment and Scalability, shows us how to remove our site off localhost. It is critical to get a deployment script right, as it is very easy to miss a step when deploying. We will cover how to deploy to one, two, or more servers, including having different environments from which we can deploy. The advantage of these multiple servers for your application is that it is horizontally scalable, making it easy to add more servers.

Chapter 10, Debugging and Troubleshooting, shows us how to look at the context of a function call because strewing console.log() functions everywhere is a horrible method to debug. We will also learn how to track memory leaks in both the frontend and backend. If you have had experience in debugging JavaScript in Chrome, you will feel right at home here.

What you need for this book

A computer and an editor! Node.js is cross-platform; so Windows, Mac OS X, or Linux will all work. You can use any text editor, although I will be using Sublime Text 2 for some of the coding. I will also use an IDE for other parts, which is JetBrains PyCharm, with its Node.js module installed. You can use PyCharm, WebStorm, or IntelliJ IDEA from JetBrains, as they are all cross-platform.

You will also need the latest (at the time of this writing) version of Node.js, which is v0.10.26. The API could change (it has changed previously), so later versions could cause issues. The version of each Node package will be shown to you when you first go to install and use it.

The level of expertise you will need is just a little familiarity with Node and experience with building applications. We will touch upon many different frameworks throughout the book. You do not need to know about any of them in depth, and that is why you are reading this book! You should also know how to run things from the console (whether it is Mac OS X, Linux, or Windows) and how to get and install software packages.

Here is a summary of the version of different technologies you will be using in some of the chapters:

  • Each chapter will list out all of the npm packages that will be needed for development.

  • Chapter 4, RabbitMQ for Message Queuing, will require RabbitMQ Version 3.2.3.

  • Chapter 5, Adopting Redis for Application Data, will be using Redis Version 2.8.6.

  • Chapter 9, Deployment and Scalability, will require Python 2.7+ or Python 3+ to be installed. It will be used for building the deploy script. You don't need to be fluent in Python, as the syntax is very straightforward and I will cover what you will need to know. We will also need an SSH client. Mac OS X already has both installed. Linux will have an SSH client and will most likely have Python installed already, and you can install both in Windows.

  • Chapter 10, Debugging and Troubleshooting, will be using Google Chrome. If you have worked in web development, you probably already have this installed. If you do not have Chrome, it is free and can be easily downloaded from Google.

I will personally be using Mac OS X using iTerm as my terminal application and Homebrew for installing software packages. If I run any commands, it will be in this context.

Who this book is for

This book is geared toward an intermediate JavaScript developer. It is assumed you have built applications using Node.js and that you may have used some of the frameworks before. We will work through each framework separately and also explain how to tie them all together. This book also touches upon the backend and frontend of JavaScript development. You will find this book helpful if you are well-versed in one, but lacking in the other.

This book is also perfect for someone who has read about scalability but is not sure what exactly this means for their projects. We will cover how to build applications that are horizontally scalable.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Our app.js file kicks off everything that deals with Express."

A block of code is set as follows:

var sayMyName = function(name){
//please don't alert anything
//this is not 1992
alert(name);
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

//get the err and data
function(err, data) {
//you have access to data here
var id = data.objects[0].id;
}

Any command-line input or output is written as follows:

# npm install express --save

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes, for example, appear in the text like this: "Click on the Login button to log in."

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you at a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: https://www.packtpub.com/sites/default/files/downloads/4480OS_ColoredImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.