Tuesday, 13 December 2016

Connection Pool in MongoDB

As your application grows in functionality and/or usage, managing resources becomes increasingly important. Failure to properly utilize connection pooling is one major “gotcha” that we’ve seen greatly impact MongoDB performance and trip up developers of all levels.

Connection Pools

Creating new authenticated connections to the database is expensive. So, instead of creating and destroying connections for each request to the database, you want to re-use existing connections as much as possible. This is where connection pooling comes in.
A Connection Pool is a cache of database connections maintained by your driver so that connections can be re-used when new connections to the database are required. When properly used, connection pools allow you to minimize the frequency and number of new connections to your database.

Connection Churn

Used improperly however, or not at all, your application will likely open and close new database connections too often, resulting in what we call “connection churn”. In a high-throughput application this can result in a constant flood of new connection requests to your database which will adversely affect the performance of your database and your application.

Opening Too Many Connections

Alternately, although less common, is the problem of creating too many MongoClient objects that are never closed.  In this case, instead of churn, you get a steady increase in the number of connections to your database such that you have tens of thousands of connections open when you application could almost certainly due with far fewer. Since each connection takes RAM, you may find yourself wasting a good portion of your memory on connections which will also adversely affect your application’s performance.
Although every application is different and the total number of connections to your database will greatly depend on how many client processes or application servers are connected, in our experience, any connection count great than 1000 – 1500 connections should raise an eyebrow, and most of the time your application will require far fewer than that.

MongoClient and Connection Pooling

Most MongoDB language drivers implement the MongoClient class which, if used properly, will handle connection pooling for you automatically.
The syntax differs per language, but often you do something like this to create a new connection-pool-enabled client to your database:
mongoClient = new MongoClient(URI, connectionOptions);
Here the mongoClient object holds your connection pool, and will give your app connections as needed. You should strive to create this object once as your application initializes and re-use this object throughout your application to talk to your database. The most common connection pooling problem we see results from applications that create a MongoClient object way too often, sometimes on each database request. If you do this you will not be using your connection pool as each MongoClient object maintains a separate pool that is not being reused by your application.

Example with Node.js

Let’s look at a concrete example using the Node.js driver.
Creating new connections to the database using the Node.js driver is done like this:
mongodb.MongoClient.connect(URI, function(err, db) {
  // database operations
});
The syntax for using MongoClient is slightly different here than with other drivers given Node’s single-threaded nature, but the concept is the same. You only want to call ‘connect’ once during your apps initialization phase vs. on each database request.
Let’s take a closer look at the difference between doing the right thing vs. doing the wrong thing.
Note: If you clone the repo from here, the logger will output your logs in your console so you can follow along.
Consider the following examples:
var express = require('express');
var mongodb = require('mongodb');
var app = express();

var MONGODB_URI = 'mongo-uri';

app.get('/', function(req, res) { 
  
  // BAD! Creates a new connection pool for every request
  
  mongodb.MongoClient.connect(MONGODB_URI, function(err, db) {
  if(err) throw err;
  
  var coll = db.collection('test');

    coll.find({}, function(err, docs) {
      docs.each(function(err, doc) {
        if(doc) {
          res.write(JSON.stringify(doc) + "\n");
        }
        else {
          res.end();
        }
      });
    });
  });  
});

// App may initialize before DB connection is ready

app.listen(3000);
console.log('Listening on port 3000');
The first (no pooling):
  • calls connect() in every request handler
  • establishes new connections for every request (connection churn)
  • initializes the app (app.listen()) before database connections are made
  • var express = require('express');
    var mongodb = require('mongodb');
    var app = express();
    
    var MONGODB_URI = 'mongodb-uri';
    var db;
    var coll;
    
    // Initialize connection once
    
    mongodb.MongoClient.connect(MONGODB_URI, function(err, database) {
      if(err) throw err;
     
      db = database;
      coll = db.collection('test');
    
      app.listen(3000);
      console.log('Listening on port 3000');
    });
    
    // Reuse database/collection object 
    
    app.get('/', function(req, res) { 
      coll.find({}, function(err, docs) {
        docs.each(function(err, doc) {
          if(doc) {
            res.write(JSON.stringify(doc) + "\n");
          }
          else {
            res.end();
          }
        });
      });
    });
      
The second (with pooling):
  • calls connect() once
  • reuses the database/collection variable (reuses existing connections) waits to initialize the app until after the database connection is established
If you run the first example and refresh your browser enough times, you’ll quickly see that your MongoDB has a hard time handling the flood of connections and will terminate.

Further Consideration – Connection Pool Size

Most MongoDB drivers support a parameter that sets the max number of connections (pool size) available to your application. The connection pool size can be thought of as the max number of concurrent requests that your driver can service. The default pool size varies from driver to driver, e.g. for Node it is 5, whereas for Python it is 100. If you anticipate your application receiving many concurrent or long-running requests, we recommend increasing your pool size- adjust accordingly

Tuesday, 13 September 2016

ECMAScript 6 (Oops in JavaScript)

JavaScript classes are introduced in ECMAScript 6 are syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance.


I’d like to share with you a series of articles about ECMAScript 6, sharing my passion for it and explaining how it can work for you. I hope you enjoy reading them as much as I did writing them.

I love JavaScript, but when it comes to working on large projects like Babylon.js, I prefer TypeScript, which is now powering Angular 2 by the way. The reason is that JavaScript (otherwise known as ECMAScript 5) doesn’t have all the syntax features I am used to from other languages I write large projects in. I miss classes and inheritance, for instance.
So without further ado, let’s get into just that:
JavaScript is a prototype-oriented language and it is possible to simulate classes and inheritance with ECMAScript 5.
The flexibility of functions in JavaScript allows us to simulate the encapsulation we are used to when dealing with classes. The trick we can use for that is to extend the prototype of an object:
We can see here that we defined a “class” with “properties” and “methods”.
The constructor is defined by the function itself (function Animal) where we can instantiate properties. By using the prototype we can define functions that will be considered like instance methods.
This works, but it assumes you know about prototypical inheritance, and for someone coming from a class-based language it looks very confusing. Weirdly enough, JavaScript has a class keyword, but it doesn’t do anything. ECMAScript 6 now makes this work, and allows for shorter code:
The result is the same, but this is easier to write and read for developers who are used to writing classes. There is no need for the prototype, and you can use the new keyword to define the constructor.
Furthermore, classes introduce a number of new semantics that aren’t present in the ECMAScript 5 equivalent. For example, you cannot call a constructor without new, and you cannot attempt to construct methods with new. Another change is that methods are non-enumerable.
Interesting point here: Both versions can live side by side.
At the end of the day, even with the new keywords, you end up with a function with a prototype where a function was added. A “method” here is simply a function property on your object.
Two other core features of class-based development, getters and setters, are also supported in ES6. This makes it much more obvious what a method is supposed to do:
Pretty handy, right?
But we can see here a common caveat of JavaScript: the “not really private” private member (_age). I wrote an article some time ago on this
Thankfully, we now have a better way to do this with a new feature of ECMAScript 6: symbols.
So what’s a symbol? This is a unique and immutable data type that could be used as an identifier for object properties. If you don’t have the symbol, you cannot access the property.
This leads to a more “private” member access.
Or, at least, less easily accessible. Symbols are useful for the uniqueness of the name, but uniqueness doesn’t imply privacy. Uniqueness just means that if you need a key that must not conflict with any other key, create a new symbol.
But this is not really private yet because, thanks to Object.getOwnPropertySymbols, downstream consumers can access your symbol properties.
Once we have classes, we also want to have inheritance. It is—once again—but it's pretty complex to do.
For instance, here what is produced by TypeScript to simulate inheritance:
Not really easy to read.
But the ECMAScript 6 alternative is better:
Thanks to the extends keyword you can specialize a class into a child class while keeping reference to the root class with the super keyword.
With all these great additions, it is now possible to create classes and work with inheritance without dealing with prototype voodoo magic.
With all these new features being available on our browsers, I think it is even more relevant to use TypeScript to generate JavaScript code.
First off, the latest version of TypeScript (1.4) started adding support for ECMAScript 6 code (with the letand const keywords), so you just have to keep your existing TypeScript code and enable this new option to start generating ECMAScript 6 code.
But if you look closely at some TypeScript you will find that this looks like ECMAScript 6 without the types. So learning TypeScript today is a great way to understand ECMAScript 6 tomorrow!
Using TypeScript, you can have all this now across browsers as your code gets converted into ECMAScript 5. If you want to use ECMAScript 6 directly in the browser, you can upgrade to Windows 10 and test with Microsoft Edge’s rendering engine there.