Scaling an Express.js Application with Memcache on Amazon Elastic Beanstalk

In this guide, we’ll explore how to create a simple Express 4 application, deploy it using Amazon Elastic Beanstalk, then add Memcache to alleviate a performance bottleneck.

Memcache is a technology that improves the performance and scalability of web apps and mobile app backends. You should consider using Memcache when your pages are loading too slowly or your app is having scalability issues. Even for small sites, Memcache can make page loads snappy and help future-proof your app.


Before you complete the steps in this guide, make sure you have all of the following:

  • Familiarity with Node.js (and ideally Express.js)
  • An AWS account. If you haven’t used AWS before, you can set up an account here.
  • The AWS CLI installed and configured on your computer.
  • Node.js, npm, git, and the EB CLI installed on your computer.

Deploying an Express.js application to Elastic Beanstalk

Express.js is a minimalist framework that doesn’t require an application skeleton. To make things easier for you, we’ve got a basic example application set up here. If you’d like to follow along, go ahead and clone that to your local machine, and cd into the expressjs-elastic-beanstalk-tutorial directory.

Next, install all of the required packages using:

To simplify development, we’ve use a template engine, ejs, but you can use whichever engine you prefer, including mustache, pug, or nunjucks.

Now that we’ve installed all the packages we need, we can add our app code. We’ll create a page that calculates the largest prime number that’s smaller than a number a visitor submits.

Open up app.js and add the following code into the section labeled ADD THE DIY CODE HERE:

Now let’s add a corresponding view. Open up the file views/index.ejs and copy the following ejs-enhanced HTML into it:

You now have a working app that you can start locally by running npm start.

Deploying with Elastic Beanstalk

To deploy the app to Elastic Beanstalk (EB), it needs to live in a Git repository. We’ve already created a .gitignore file with the following lines in it:


This prevents any secret or unwanted configuration information ending up in Git.

Let’s go ahead and commit the initial changes we’ve made to the app:

Next, we’ll need to create a EB CLI repository with enough information so EB knows how to run it. Start by creating the repository using eb init:

Then create an environment running a sample application:

Don’t worry too much about the sample app. This just gives you all of the boiler plate for a basic Amazon Elastic Beanstalk setup. For now it’s running a default application. If you would like to see it when environment creation completes, use the eb open command to open the environment’s URL in the default browser.

But that doesn’t do us any good. We want our own application up and running. In order to do that we’ll need to set up a node configuration file to tell EB the npm start command. So create a folder called .ebextensions/ and add the file nodecommand.config to it.

Then add the following code to nodecommand.config

Then stage this file in git, and deploy the changes:

If you haven’t already, run eb open and checkout your shiny new application!

Learn to write Express.js middleware

Our prime-calculating app works, but it has one mayor flaw: a user can submit invalid input, such as a string of letters. To validate the input, we’ll create middleware in Express.

There are several validation middleware packages available for Express, and you should use one of those in most cases. In this tutorial, we create our own validation for demonstration purposes.

Express middleware typically consists of a chain of functions that inspect and potentially modify the details of a request and its corresponding response. Each function takes three parameters:

  • The request object
  • The response object
  • A next function that represents the next middleware function in the chain

Each middleware function can modify the request and response objects as necessary. After doing so, it can either call the next middleware function or return to terminate the chain prematurely.

For our app, we create a validation middleware function that parses the submitted query and checks whether it’s a number below 10000.

  • If it is, the function calls next.
  • If it isn’t, the function returns an error response.

Add this function to app.js and call it when processing the GET route:

The validation middleware might return an error message, which we need to display in the index.ejs view:

Commit and deploy your changes:

Open the app and submit some invalid queries to see the error message in action.

Adding caching to Express

Memcache is an in-memory, distributed cache. Its primary API consists of two operations: SET(key, value) and GET(key). Memcache is like a hashmap (or dictionary) that is spread across multiple servers, where operations are still performed in constant time.

The most common use for Memcache is to cache expensive database queries and HTML renders so that these expensive operations don’t need to happen over and over again.

Set up Memcache

To use Memcache in Express, you first need to provision an actual Memcache cache. MemCachier provides a fast and flexible multi-tenant cache system that’s compatible with the protocol used by the popular memcached software. When you create a cache with MemCachier, you’re provided with one or more endpoints that you can connect to using the memcached protocol, accessing your cache just as if you had set up your own memcached server. So head over to, sign up for an account, and create a free development cache. If you need help getting it set up, follow the directions here.

There are three config vars to you’ll need for your application to be able to connect to your cache: MEMCACHIER_SERVERS, MEMCACHIER_USERNAME, and MEMCACHIER_PASSWORD. You can either add these variables through the management console, or using the EB CLI.

EB provides an in-browser management tool for your application. To access it, run:

This will open the management console in your default browser. Once there, click on Configuration in the left side menu. Then select modify under the Software portion. If you scroll to the bottom, you’ll see an Environment properties section. This is where we’ll add our environment variables.

AWS Elastic Beanstalk Management Console
AWS Elastic Beanstalk Management Console

If you’d prefer to keep everything running in the console, we can do that too.

We can confirm that they’ve been set by running:

Once your environment variables are set up, next step is to install memjs with npm so we can use caching in Express.

and configure it in app.js:

Caching expensive computations

There are two reasons why caching the results of expensive computations is a good idea:

  1. Pulling the results from the cache is much faster, resulting in a better user experience.
  2. Expensive computations use significant CPU resources, which can slow down the rest of your app.

Our prime number calculator doesn’t really have any expensive computations, because we limit the input value to 10000. For the sake of the tutorial, however, let’s assume that calculating the prime is an expensive computation we would like to cache.

To achieve this, let’s modify the GET route in app.js as follows:

Deploy these changes with eb and submit some numbers to find primes:

The page should work just as before. However, under the hood, already calculated primes are now cached. To see what’s going on in your cache, open the MemCachier dashboard (which is where you found your environment variables.)

On the dashboard you can refresh the stats each time you request a prime. The first time you enter a number, the get misses will increase. For any subsequent request of the same number, you should get an additional get hit.

If it’s not working, and you’re getting the following error:
Error: connection ECONNREFUSED
Error: connection ECONNREFUSED

Check and make sure that your MEMCACHIER_* ENV variables are set correctly.

Caching rendered views

Rendering HTML views is generally an expensive computation, and you should cache rendered views whenever possible. In Express, you can achieve this easily with middleware. Let’s add a cacheView middleware function to app.js that checks whether the view for a given URL (including query parameters) is in the cache.

  • If it is, the view is sent immediately from the cache.
  • If not, we wrap the send function in the response object to cache the rendered view and call the next function.

This is easy enough and works well. However, if the view ever changes, we need to be careful. To illustrate the case of a changing page, let’s add a “Like” button to each number and its calculated largest prime. Let’s put the button just below the calculated prime in the index.ejs file:

The like is submitted via POST request, and to parse its input we need the body-parser package:

We can now create a controller for the POST route in app.js and store the posted like in a variable.

Storing likes in a variable is a bad idea. Each time the app restarts, it wipes all likes. We do this here only for convenience. In a production application, you should store such information in a database.

In addition, we also need to make sure the likes are passed to the render function in the GET controller:

To illustrate the problem with changing pages, let’s commit our current implementation and test it:

If you submit a number, you will now get the largest prime below it, together with a Like button. However, when you click Like!, the like count doesn’t increase. This is because the view is cached.

To resolve this, we need to invalidate the cached view whenever it is updated:

Deploy again to Elastic Beanstalk:

Now you can see the number of likes increase.

Session Caching

Memcache works well for storing information for short-lived sessions that time out. However, because Memcache is a cache and therefore not persistent, long-lived sessions are better suited to permanent storage options, such as your database.

To use sessions in Express, you need express-session. To store the sessions in Memcache, you need connect-memjs:

The configuration in app.js is easy enough:

Now you can now use sessions as you please. For more information about session usage in Express, check out the express-session documentation.

Clean up

Once you’re done with this tutorial and don’t want to use it anymore, you can clean up your EB instance by using:

This will clean up all of the AWS resources.

Further reading & resources