Jan 2, 2013

Scaling Your Web Farm and Keep Supporting User Generated Content using File Sharing (NFS)


Have you ever wanted to scale your web operations to multiple web servers and still needed a simple common place to share data between servers?

rsync
A very useful way for that is rsync that is commonly used to distribute the web server static content (HTML, PHP, CSS, and JavaScript files) between different servers.

What to do with User Generated Content?
When we want to store user generated content such images, documents or video that are uploaded by them from time to time we have three options:
  1. Push them as BLOBs into the database (or your NoSQL system such as MongoDB).
  2. Store them on disk that is accessible by several servers. 
  3. Store them on a "File Server as a Service" like Amazon S3.
This time we'll focus on the second option:

DIY with Network Services
The basic solution for DIY is installing a file sharing daemon on one of your servers and share a folder of it to the other web servers.
There are three common file sharing protocols that may be used:
  1. SAMBA (that is common and supported by Windows as well).
  2. NFS (supported only on  *NIX machines).
  3. CIFS that mostly used in Windows, but can be mounted to LINUX machines as well.

In this case I decided to focus on the NFS implementation, so stay tuned:

NFS Configuration
First configure the NFS server
  1. Install the NFS RPM:  yum -y install nfs-utils
    1. Start service: /etc/init.d/nfs start
  2. Open the relevant ports in the iptables FW (2049 and 111):
    1. iptables -I INPUT -p tcp -s 192.168.85.0/24 -m state --state NEW,RELATED,ESTABLISHED --dport 2049 -j ACCEPT
    2. iptables -I INPUT -p udp -s 192.168.85.0/24 -m state --state NEW,RELATED,ESTABLISHED --dport 2049 -j ACCEPT
    3. iptables -I INPUT -p tcp -s 192.168.85.0/24 -m state --state NEW,RELATED,ESTABLISHED --dport 111 -j ACCEPT
    4. iptables -I INPUT -p udp -s 192.168.85.0/24 -m state --state NEW,RELATED,ESTABLISHED --dport 111 -j ACCEPT
  3. Configure exported locations by editing /etc/exports according to the following examples:
    1. Provide every server behind the firewall R/W to this folder: /path/to/directory *(rw)
      1. Please notice that in this case you should provide the relevent permissions on disk (since the guest machine will use in default the nobody user to access this disk). For example: chmod -R 777 /path/to/directory
    2. Provide a single server read only permission to this folder: /path/to/directory 192.168.2.21(ro)
  4. Finally load these exported locations: /usr/sbin/exportfs -a

Finally mount this disk to all other servers:
  1. Create local directory: mkdir /local/directory
  2. Add line to /etc/fstab
    1. SOURCE_SERVER:/path/to/directory /local/directory nfs
  3. Mount the folder: mount /local/directory
Bottom Line
Scaling you system is possible with few simple steps, this was one of them

Keep Performing,

P.S In the DevOps world, scripts is everything. So you may use the following: 
Server:

yum -y install nfs-utils
/etc/init.d/nfs start
mkdir /path/to/directory
chmod -R 777 /path/to/directoryecho '/path/to/directory *(rw)' >> /etc/exports
/usr/sbin/exportfs -a


Client:

#!/bin/sh
mkdir /path/to/directory
echo "$1:/path/to/directory /path/to/directory nfs" >> /etc/fstab
mount /path/to/directory

Dec 20, 2012

mongoDB Sharding

If I should have made some safe bets on the near future, I would choose two: Hadoop and mongoDB. 

There is a huge demand for both technologies and many players consider these technologies as a foundation for their future products.

Sharding?
MySQL Sharding was a major issue for large scale installations and it is the same for mongoDB large installations.

Back to Basics
mongo is pretty similar to a regular database, but it has two main advantages: 1) Software engineers love it as it can easily be used for object persist-ency and 2) it support unstructured objects (documents) that can easily store different objects based on the same virtual class.

mongoDB terms

  1. Database: database
  2. Collections: very similar to tables.
  3. Documents: very similar to rows. Yet, a document can be as flexible as a JSON document can be. For example, it may include 1 to many fields in the document itself.
  4. mongod: a mongoDB instance or shard.
  5. Chunk: a 64MB storage unit that stores documents.
  6. Config database: Chunks to mongos mapping directory.
Why use sharding?
  1. Support large dataset using commodity servers.
  2. Support high IO requirements using commodity disks.
What are mongoDB sharding features?


  1. Range-based Data Partitioning: a very similar method to MySQL partitioning. You should choose one or more fields (shard key) that sharding will be based on. You should choose a shard key according to the business logic, like splitting according to account id in a SaaS application.
  2. Automatic Data Volume Distribution: mongoDB will take care of the shards balancing by itself according to the chosen shard key.
  3. Transparent Query Routing: mongoDB takes care of queries map reduce to multiple shared by itself when a query does not match the shard key (very much like Hadoop).
Key Recommendations for mongoDB Sharding
  1. Sufficient Carnality: choose a shard key that can be split later to more shards if a database size is getting too large (exceeds chunk size).
  2. Uniform Distribution: choose a sharding key that will spread a in uniform distribution to avoid unbalanced design.
  3. Distribute Write Operations: if you have a billing system, prefer to shard according to account id rather than shard according to billing month. Otherwise, in a given day, probably only a single shard will be used.
  4. Query according to the shard key: if any of your queries will include the shard key, each of your queries will result in a single shard query. Otherwise, it will generate N queries (one per shard).
Technical Aspects for mongoDB Sharding
  1. Every sharded collection must have an index that its first fields are the shard key (use shardCollection for that).
  2. Chunk size default limit is 64MB
  3. When a chunk reaches this limit, mongoDB will split it to two.
  4. If chunks are not distributed uniformly, mongoDB will start migrating chunks between different mongos.
  5. Cluster Balancer is taking care of this process.
  6. Balancing can cause performance issues and therefore can be restricted to off peak hours (nights and weekends for example) using balancing windows.
  7. The shards mapping to mongos is saved at the config database.
  8. Replication should be considered as well  a complementary method.
Bottom Line
mongoDB brings to the table an out of the box sharding solution that can scale your operations. Now, you only need to analyze your needs and select the right solution for them.

Keep Performing,

Dec 13, 2012

MySQL Crash Course Presentation

In the last few weeks I lectured a MySQL crash course. The course topics covered almost all what is needed to make an initial ramp up when you get into MySQL: ERD, DDL, DML, installation, security, scaling, backup, Schema design, tuning, master slave and more...

The good news
I got a very good feedback from the students, so I decided to share with you the presentation itself:




Keep Performing,
Moshe Kaplan

Dec 9, 2012

How to use rsync for high availability environments?

What if...
  • What if I have a large number of web servers and I need to deploy the same code on all of them?
  • What if  I would like to enable high availability and redundancy for static user content such as images?
  • What if I want to to backup files to a central storage?

A Swiss knife for static content replication
rsync was considered for a long time as the best solution for static content and code replication  in environments that consist of large number of servers.

rsync has a simple protocol that replicates a directory (one or more) on a single server to other servers. This can be achieved in two different methods (like SCP that it is based on):

  • Push from the master to the slave: rsync [OPTION] … SRC [SRC][USER@]
    HOST:DEST
    
    
  • Pull from the server by the slave: rsync [OPTION][USER@]HOST:SRC [DEST]

Can I perform a change on the destination directory?
Please note that the rsync protocol analyzes differences between two directories, and therefore probably will not match cases when you want to change the content of the destination directory.

How should I authenticate?
Use one of the two options:

  1. Static user/pwd using sshpass for non interactive SSH based authentication.
  2. PKI authentication using on-the-fly keys generation or pre-generated keys
Master-Master replication
Like in MySQL, Master-Master replication can be achieved by a dual Master-Slave connections setup . Please consider to enable only one of these connections. Then, during a failover, disable the replication. Last, when you bring the master server back enable the other replication.


Note: you may consider using OpenStack Storage for these purposes as well, as it provides an out the box solution for high availability and redundancy that easily supports multi master out of the box

Keep Performing,
Moshe Kaplan

Oct 4, 2012

How Amazon SES Mailbox Simulator Makes Your System Better?

Amazon latest feature, Amazon SES Mailbox Simulator, is great!

With this feature you can email to simulator addresses. Each address triggers a specific type of behavior such as: a successful acceptance, hard bounce, "out of office" auto responder or complaints.

Why am I so excited from a feature that actually generates no direct business value to the end user?

At dSero, the Anti AdBlock Creators, we invest a lot in providing a high quality anti AdBlock solution to our clients in no time.
You can do that, only if you totally automate your software development and even more important: your quality processes.

Bottom Line
More automation and better coding enables you providing great product to your users in a better quality and lower cost.

Keep Performing,

Sep 5, 2012

How to Scale WordPress?

WordPress is so easy to install. Just 5 minutes and you have a working site...

But what happens when traffic increases and page-views soar?
That was the main subject of the presentation I had at WP TLV (The Israeli WordPress Community) first event. The event was hosted by Google and organized by dSero: The Anti Ad Block Creators (disclaimer: I'm a major stake holder of this firm).

The answer can be found in the attached presentation:



Bottom Line
WordPress fits small sites and large sites. You just need to make the proper architecture decisions and WordPress will scale to your needs.

Keep Performing,
Moshe Kaplan

Aug 29, 2012

Who takes care of non Sharded tables in Sharding?

The Challenge
As you may remember, Sharding is about splitting a database according to a key. Usually it is according to user id, client id or a phone number.
One of the major questions when we implement Sharding is what to do with data dictionary tables? The data dictionary tables are those tables that include static data that is being used by all shards.

The Options
In general there are two options:

  1. Have a single copy of the data dictionary tables and ask the application to take care of the complex queries.
  2. Have multiple copies of the same table and create a data distribution solution that will update all of these table copies.


Data Dictionary/Static Tables Sharidng Solution

The Elegant Solution
Create a new database that will serve as a master to all shards.
This database will include all data dictionary tables, and will serve as the single write instance for all data dictionary tables.
Once a record will be updated at one of these data dictionary tables, it will instantly be replicated to the all of the shards and will be available.

Bottom Line
Sharding may seem hard, that with simple and elegant solutions you can achieve your own scale-able solution.

Keep Performing,
Moshe Kaplan

ShareThis

Intense Debate Comments

Ratings and Recommendations