Wednesday, February 15, 2012

Memcached Overview

 General

What is memcached:  "Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load." You may want to visit: Memcahed Wiki.

Memcached is developed in C programming language so that makes it cross platform and many other languages  have API interfaces to it so it can be used with lot of other technologies.

Installation

Installation on linux is a breeze :)  just run this command in terminal

- sudo apt-get install memcached

I got to a problem when installing memcached with a missing library pcre.h 
Well run this command in terminal

- sudo apt-get install libpcre3-dev


And I found this blog with the answer for this at pcre.h missing
Memcached has some default settings which are best for most server configurations so select all defaults when installing. If you have some special requirements visit the conf page .

If you want to use Memcached with PHP you will need PECL extension called memcache which is not included by default with PHP package, so you have install it manually. Open terminal and run this command

- sudo pecl install memcache

Accept all defaults. Once it is built and installed you may need to add this line to your php.ini file

- extension=memcache.so

into you extension section.

Well thats it for the installation, you are ready for caching :)

 Tutorial

When starting up with Memcached I recomend reading of the Caching story :) and also to illustraded one :)

So the basic idea of memcached is to store all your server adresses in an array like this

$MEMCACHE_SERVERS = array(
   "10.1.1.1", //web1
   "10.1.1.2", //web2
   "10.1.1.3", //web3
);

and put them into a memcache object like this

$memcache = new Memcache();
    foreach($MEMCACHE_SERVERS as $server){
    $memcache->addServer ( $server );
}


The next thing you should do is to use memcached i.e utilize the caching systems on all your servers in the cluster. In this example we have 3 with one 1 GB RAM, so that means 3 GB of distributed cache.

 Illustrations

Below are some images I made about memcahed usage and structure







Wednesday, February 8, 2012

PHP Accelerators Overview

When you have a dynamic web application that has a need of big payload of data at once you will need a caching management system that will optimize the loading process by scheduling what, how much and when to be loaded. Of course there are several PHP extensions that are available. But first to mention is that there are two levels of optimizing the loading of the data in a web appication:

level 1: local optimization
level 2: distributed (over several application servers)

maybe level 3: (disk caching if plausible)

level 1: For the first level now there are several well known accelerators like APC(Alternative PHP Cache), eAccelerator, XCache. Here is a wikipedia page for them opcode php cachers. The most popular is APC which will be built into the next versions of the php, probalbly 5.5 or newer.  APC and alike are very useful for bytecode caching i.e when you have many php pages to load it would be better to have them cached in memory rather than load them with each php request. But in either case you can use also data caching. When the data is distributed you have to use something else.

level 2: The second level is for web applications that are distributed accross several web application servers and have need of distributed data. Here comes into place one and well know caching system called memcached (http://memcached.org/)


Monday, November 28, 2011

Learn linux

If you want to learn linux and download some books on it visit Linux Tips

Linux OpenSUSE 12 Review

OpenSUSE 12 Review


Ubuntu Gadgets

I am using Ubuntu so I found several usefull gadgets for your desktop.


The clock, weather and the calendar are the most I use :)

Linux Mint 12 Review

If you are a Linux fan like me, here is a Linux Mint 12 Review


Wednesday, November 23, 2011

Database solutions for PHP

In this post I will explain the most popular and most used database engines capable of working with PHP.  They are many, and have different capabilities and architectures. Some of them are payable and expensive, some of them are open source. So depending on your needs you can use some of the great variety of database engines. No matter which one you use PHP has prebuilt drivers and PDO libraries for the all of them.

Here is a short list:
1. SQLite (open source)
2. MySQL (open source)
3. PostgreSQL (open source)
4. dbm-style databases (Oracle Berkeley DB)
5. Oracle Database (payable), Oracle XE (open source)
6. ODBC

1. SQLite

Although you might think that MySQL is most popular when working with PHP, you are quite wrong. The most popular db engine is SQLite. It is so popular because it is small, reliable and also embeded database engine. When you develop application that uses SQLite it is by default embeded inside the application that uses it, for example Firefox, Skype, mobile platforms like Symbian, iPhone etc. In the newest version of PHP SQLite is bundled with it so you don't have to install anything additional. It is fast and supports the SQL standard commands. It is simple because it is not structured like client-server and the code is open source so you can use it and modify it to your needs.

Some of the features of SQLite:

 - Dynamically typed: Given field can hold different types of data from one record to the next, much like a NoSQL database. This is very useful when working with PHP since it is loosely typed language. The drawback is that it is hard to maintain db integrity and it is not compatible with other db engines.

- Given database is stored in a single cross platform db file. So you can easily use the same db with other application that works with SQLite.

- It is easy to configure because there is no need of installation and no need to create users and add persmissions.

- You can call PHP commands from within an SQLite SQL query. So when working with some query you can call PHP function within the same query and it will surely work well.

MySQL is most known by PHP developers so I will jump right to 3.

3. PostgreSQL

PostgreSQL is also open source db engine much like MySQL. In fact the closest relative to MySQL is PostgreSQL. They both support the same capabilities and work at the same level of scalability. MySQL is known for its ease of use and great speed, while PostgreSQL is known as feature rich and reliable. But as the time passes both of them became powerful enough and surpassed their disabilities. You can access PostgreSQL with extension and also with PDO driver much like MySQL. So after this you can use both, as they are both good. It depends on your flavour which one to choose.

4. dbm-style databases (Oracle Berkeley DB)

 dbm-style database is an embeded type of db developed in 1970s. It is like SQLite but not nearly powerful. It stores files on the hard disk and it doesn't use SQL commands. But it can be very fast as records are taken with only a given key. This type of dbs aren't used very much today but the most popular of the existing is Oracle's Berkely DB.

5. Oracle Database and Oracle XE

Oracle is a company most well know by the database software systems that it develops. Oracle databases are used mainly in large organisations and companies that have a need of complex queries and store large amounts of data. Thus it is very expensive to use. So if you need small scale db engine for a low traffiic application than you should use the above given or Oracle's XE edition which is open source. For use with PHP you can use OCI8 extension or PDO driver.

6. ODBC

ODBC is not a database engine but it is an API interface for accessing and working with several db engines like MS Access, MS SQL server, and IBM's DB2 engine. MS Access and SQL Server are only available on Windows systems so this would be good solution if you work on Windows. As for DB2 there are AIX and, Unix, Linux and Mac as well as Windows.

Windows has preinstalled ODBC driver but there are open source drivers for other platforms like Linux, Unix and Mac OS X.

Ok so this would be all about PHP compliant db engines.

used literature from Beginning PHP 5.3 by Matt Doyle