We are using MongoDB for few of our products. We like to share some of the
recommendations for MongoDB in production.
Handle Connection Errors
Few weeks before, we faced one weird problem in our NodeJS application.
Our customer can view our site and not able to login into the application.
We tried to figure out the issue and found that MongoDB database connection is
broken during mid night. After that, our application is not crashed,
but it is failed to connect MongoDB. We assume that it should auto reconnect if it is failed.
But it does not happen. Finally we found that we did not handle error of MongoDB
connection in our code.
You must handle error of MongoDB connection in you code. It can be even simple console log.
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:'));
If you are using NodeJS, you must handle error
and exception
in all places.
It is a best practice.
Setting Timeout
You have to properly set your timeouts. The driver's default timeouts are very aggressive ranges anywhere from 1 second (e.g. the Node.JS driver in some cases) to 30 seconds (e.g the Ruby driver), so you really need to think about what the optimal setting is for your use case.
For connections made through a Platform-as-a-Serivce (PaaS) such as Heroku, you might consider an even higher timeout (e.g. 30 seconds) since your application is likely running in a container that can be “idled” or “passivated” during periods of low activity
If you use Mongoose and NodeJS, then you can set connection timeout by
// Database connect options
var options = { replset: { socketOptions: { connectTimeoutMS : conf.dbTimeout }}};
// Establish Database connection
var conn = mongoose.connect(conf.dbURL, options);
MongoLab has written very good article on MongoDB Timeout
Connection Polling
You should consider using connection pooling. It will make for a much faster and
more stable application. You won't spend the time on each request to create connections (can be noticeable to the end user) and you aren't subject to connect errors either.
The mongoose
package for NodeJS handles connection pooling by default. If you use your own
MongoDB driver, make sure you have implemented connection pooling.
Thanks to MongoLab for sharing amazing tips of MongoDB with us.
The support of MongoLab is awesome and respond for questions very quickly. I highly recommending for MongoDB database.
Have a nice day.
I am a very average programmer, but like to learn new things from others. We have lot of medium to learn something new, interesting and useful. As a programmer, I use Google Search
, Stack Overflow
and Github
to get to know new things and the answers for my questions. But most of the time,
when I see the answers, there are multiple questions and other solutions(Mostly Stupid) come out from my brain. Even though, I can reply or add comment to questions in Stack Overflow
and Github issues
, they are not very humanized. I mean, it is not like interacting with real human and have continuous conversation in a moment when we have lot of things going around mind, not like getting reply after two hours and sometimes two days.
What is IRC
IRC is the greatest medium to have humanized conversation where you can clarify your doubts, learn new things. In the top of all, it is a place to meet amazing talented personalities.
From Wikipedia
Internet Relay Chat (IRC) is a protocol for live interactive Internet text messaging (chat) or synchronous conferencing. It is mainly designed for group communication in discussion forums, called channels, but also allows one-to-one communication via private message as well as chat and data transfer, including file sharing.
IRC consists of channels. Channel is something like a chat room, but it will be dedicated for some technology or language, ex: #html, #css, #javascript, #go-lang. You can go to any channel and ask questions or just listen and learn from other's discussion.
I am using Freenode which is a widely used open IRC server.
Lets see how you can get started with IRC on Freenode
.
Register your nickname
In IRC, you will be addressed by your nickname. It is good to register your nick name.
But it is not mandatory to register your nick name to access IRC channel.
- Visit http://webchat.freenode.net/
- Enter your nick name
- Enter
Captcha
and Connect
To register your nick name,
/msg NickServ REGISTER password youremail@example.com
Once you registered, email is sent to you and just copy and paste the VERIFY command in IRC.
Now you own the nick name, no one can use the same nick name. Even if someone try to use, it will
be suffixed by underscore(_)
Login with your nick name
Whenever you enter IRC, you have to identify your nickname with password by
/msg nickserv identify yourpassword
Join Channel
If you want to go to particular channel, then type /join #channelname
.
Lets say if you want to go to c
language channel, type /join #c
Incase you do not know the channel name, then go and search here
Once you join the channel, you can ask questions or listen others.
Other useful commands
/msg <name> - Sends a short message to a person privately.
/whois <name> - Get a short description of who a person is.
/nick <name> - Change your nick name
/me - Sends an action to the channel.
/away <message> - Sets an away message.
/quit <last words> - Quits and allows you a final message before quitting.
/clear - Clears a channel's text.
/clear all - Clears a channel's text.
When you use IRC, you must follow some etiquette which is defined precisely by
Christoph Haas.
Regarding IRC client, apart from web, there are other IRC client application available.
You can checkout here
IRC taught me a lot. I hope, you will feel the same after using it.
Have a nice day.
When I work with web application, I got few JS snippet from different places(Mostly from StackOverflow). I hope, it will save your time.
Get Query String Value
This function helps you to get query string value from URL(href).
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Add leading zeros to date
Sometimes you may want to add leading zeros to date field.
("0" + anynumber).slice(-2))
will add leading zero to a number.
var date = new Date();
var dateString = ('0' + date.getDate()).slice(-2) + '/'
+ ('0' + (date.getMonth()+1)).slice(-2) + '/'
+ date.getFullYear();
Set checkbox checked property using Jquery
I was bit struggle with this problem. In latest jquery, I try to set checked property using $('#checkBox').attr('checked');
. It does not work.
From jQuery > 1.6
, it should be
// new property method
$('#checkBox').prop('checked');
Best way to Toggle Classes on window scroll
We like to hide or show some element based on window scroll. It may be show/hide of goto top button or fixed header position.
// Toggle header position
var breakpoint = 100; // Can be any value based on your layout
var headerDomElement = $("header")[0];
var $window = $(window)
$window .on("scroll", function () {
var top = $window.scrollTop();
headerDomElement.classList.toggle("header-fixed", top > 0);
});
// Goto top button show and hide
var breakpoint = 0;
var btnGoTopDomElement = $("btn-gotop")[0];
var $window = $(window)
$window.on("scroll", function () {
var top = $window .scrollTop();
btnGoTopDomElement.classList.toggle("show", top > 0);
});
Instead of Jquery toggleClass
method, you can use classList.toggle
on Dom element will improve the performance. Thanks to Paul Irish.
Interactive Form Submission
Form submission might take time if server or network is slow. Meantime, if submit button
is still clickable, It will not be user friendly.
You can disable submit button and do any validation of the field by adding onsubmit
attribute to the form.
<form accept-charset="UTF-8" method="POST" action="/message"
onsubmit="return validateForm(this);">
<input type="submit" value="Submit">
</form>
function validateForm(formObj) {
formObj.submit.disabled = true;
formObj.submit.value = "Submitting...";
return true;
}
Share if you have any interesting JS snippets.
Happy coding and have a nice day.