Traffic-shaping WebSockets using Node.js and Redis

How to protect your WebSocket-enabled application against traffic spikes by using Redis as a real-time request counter

1 2 Page 2
Page 2 of 2

Now, let’s change out the function used to control delay. You can do this by specifying the switch delayFn when starting the Node.js process. The valid functions are:

  • noDelay
  • linear
  • batch3

To launch the Node.js process with batch3, the command would look like this:

$ node socket-shape.node.js --connection ../connection.json --delayFn batch3

Now, let’s see how our graph changes in batch3 mode:

redis websocket traffic shaper 5 Redis Labs

Traffic -> 1 destination (batch3)

redis websocket traffic shaper 6 Redis Labs

Traffic -> All destinations (batch3)

redis websocket traffic shaper 7 Redis Labs

Traffic -> Mixed destinations (batch3)

This last graph might seem a little unexpected, but it’s important to understand that the “Mixed destinations” pattern is pushing double the traffic to the server. Also keep in mind that the graph connects the dots regardless of whether or not there is data, so big stretches may not be reflected because of the delay.

Even though this is just a simple illustration, it is easy to see how using a simple distributed counter stored in Redis can give you a great deal of control over the flow of traffic to your application—especially when responding to full-duplex communication. Of course, when placing something directly in your data path to shape traffic, high availability and performance are critical. Redis Enterprise is always a good option for meeting those two requirements.

Kyle J. Davis is the technical marketing manager at Redis Labs. Kyle is an enthusiastic full-stack developer and works frequently with Node.js and Redis, documented in his long-running blog series on the pair. Previously, Kyle worked in higher education, helping to develop tools and technologies for academics and administrators. 

New Tech Forum provides a venue to explore and discuss emerging enterprise technology in unprecedented depth and breadth. The selection is subjective, based on our pick of the technologies we believe to be important and of greatest interest to InfoWorld readers. InfoWorld does not accept marketing collateral for publication and reserves the right to edit all contributed content. Send all inquiries to

Copyright © 2018 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2
How to choose a low-code development platform