Saturday, February 1, 2014

Delphi Basic and Advanced Interview Questions and Answers

Delphi Basic and Advanced Interview Questions and Answers

Have you got a call for Delphi interview? Must prepare following Delphi interview questions and answers. These Delphi interview questions and answers cover basic as well as many advanced questions. Many of these interview questions can be asked from you depending upon your experience in Delphi and the projects you have done in Delphi. These Delphi interview questions cover basic introduction of Delphi, OOPS concepts in Delphi, database connectivity in Delphi, environment setup steps, handling with DLLs, Handling Indy clients and mail protocols, third party components like reporting tools, UI tools, error tracking tools etc., unicode support in Delphi, File handling concepts in Delphi and much more.

1. What do you know about Delphi Programming Language? Why and where Delphi is it used? What is happening new in Delphi nowadays? What are the latest versions of Delphi? What is new in latest versions of Delphi?

These are the introductory questions about Delphi programming language which every Delphi developer should know. You should remain updated what's happening latest in Delphi world. You can visit Embarcadero official website to gather more info about Delphi.


Delphi XE5 is the latest version. You can read about its features on Embarcadero website.

2. What is RAD Studio? What is VCL, Firemonkey, Object Inspector, StackTrace? What are the various short-cuts for Run, Step Over, Trace Into, Object Inspector, opening dfm files from pas file and vice-versa? 

RAD Studio is the IDE for Delphi programming language. Earlier it was Borland Studio. If you use Delphi intensively every day, you should be knowing about Visual Component Library (VCL), Firemonkey, Difference between VCL and Firemonkey, use of object inspector and all the short-cuts used in the IDE.


3. Write down a simple program in Delphi to show "Hello World" message with proper syntax.

The main motive behind this simple program is that the interviewer wants to know whether you know about various sections of units like uses, interface, implementation etc. Before going for interview, you should prepare some sample and simple programs in Delphi so that you can explain them well when asked. Uses after implementation - cyclic/circular reference


4. What are the various file extensions used in Delphi? or What are various types of files in Delphi?

Variuos file extensions in Delphi are .pas, .dfm, .dcu, .dpk, .dpr, .dproj, .bpl etc. You should also know the full forms and importance of these extensions in Delphi project. Read complete answer 

5. How do we put comments in Delphi for single line as well as for a block? Can we put comments in dfm file? If yes, then how?

Single line comments: //
Block comments: {  }
You cannot put comments in dfm file as it is meant for designing purpose only.

6. What are various access specifiers in Delphi? What is Published? Where is it used? What is the difference between Public and Published? 

Private, Public, Protected and Published are the various access specifiers in Delphi. Published access specifier is used to expose the data at design time in the Object Inspector.


7. How Constructors and Destructors are implemented in Delphi? What is Inherited keyword? Is it necessary for a class to have constructor or destructor?

Constructors are used to create the instances and destructors are used to destroy the instances and free up the memory. Read complete article 

8. How Inheritance, Polymorphism, Overloading, Abstraction concepts are implemented in Delphi? Does Delphi support Multiple Inheritance? How Multiple inheritance is implemented in Delphi? What is the difference between Abstract Class and Interfaces?

Prepare you general OOPS concepts and also relate them with Delphi. Do prepare sample Delphi programs to implement above OOPS concepts like Inheritance, types of inheritances, Polymorphism, Overloading and Overriding, Abstract classes and interfaces etc.

9. How Exception Handling is implemented in Delphi?

You should be able to write down a simple program to explain the try, except and finally blocks and their usage.

10. What are the packages in Delphi? What are design time and runtime packages in Delphi and what is the difference between them? What is difference between DLL and BPL?


11. What is TStringList? When and why is it used? What are its advantages? Explain with simple example.


12. What is datagrid, dataview, datasource, dataset and database components in Delphi and how they are linked to each other? Do you know how locking is done in Delphi? What is BeginUpdate and EndUpdate? What is EnableControls and DisableControls in Dataset? What is the difference between dbExpress, dbGo and BDE in Delphi? Why to use dbExpress?


13. How Query component is used in Delphi for SELECT, UPDATE, DELETE and INSERT queries? What do you mean by opening and closing a dataset? What is the difference between activating and opening a dataset? What is ExecSQL statement in Delphi? Where is it used?

You should prepare a sample program in which you fire a simple SQL query (Select, Insert, Update and Delete) and show how will these be executed. Google for more help.


14. What is the Transaction Component in Delphi? Why is it used? What is Commit, Rollback and Timeout in Transaction component?

When you have to fire multiple SQL commands at once, you use transaction. When all queries are fired successfully, fire commit else fire rollback. Timeout is the time in milliseconds given for execution of query. Google to read more.

15. How will you implement File Handling in Delphi? How can you open, close, read, write, append and delete files in Delphi? Explain the usage SaveToFile and LoadFromFile functions in Delphi? What is TFileStream and TMemoryStream? What is the difference between them.


16. How will you create LogFiles in Delphi? 


17. How will you read and write data from INI file in Delphi?


18. What are the various event of the Delphi Forms? In which sequence does these events fire? Explain complete life cycle of Delphi Forms?


19. What is OnCloseQuery event in Delphi Form? Why is it used?


20. What is the purpose of Anchors, Align, Margin and AlignWithMargins Form properties? What do you know about Docking?

These all are used for docking and alignments of the components in the Delphi Form. Google for more details. There are good videos available on YouTube to explain these concepts.

21. What is unicode? How does Delphi support unicode? When was the unicode concept introduced in Delphi? What is the difference between Unicode, UTF-8 and UTF-16? Have you migrated any older Delphi version application to unicode version of Delphi? What was your experience? Which problems did you face while code migration?


22. How does Delphi interact with DLLs? What are the various calling conventions in Delphi? How to call DLL functions from Delphi code? What is difference between directives like safecall and stdcall and cdecl? What is HRESULT?


23. How to handle CHM files or help files in Delphi? What is HTMLHelpViewer in Delphi?


24. How to create new process at runtime in Delphi? What is CreateProcess and ShellExecute API calls in Delphi? How these are implemented?



25. Have you ever used Indy Clients in Delphi? What do you know about Socket Programming in Delphi?


26. How to Send Email with Attachments in Delphi XE2 using Indy Clients?


27. How to run XPath Query in Delphi using MSXML DOM?


28. How to create and consume webservices in Delphi using HTTP and SOAP protocols?


29. What are Live Bindings in Delphi?

LiveBindings is a data-binding feature supported by both the VCL and FireMonkey frameworks in RAD Studio. LiveBindings is an expression-based framework, which means it uses bindings expressions to bind objects to other objects or to dataset fields. Read complete article on Embaracdero.

30. What is Oxygene Programming Language? How is it related to Delphi?


32. What steps will you take for Environment Setup in Delphi?

You should know that you can setup the paths for various library files and install packages from Project --> Options, Tools --> Options, Components --> Install Packages etc. 

33. What are the various Build Configurations in Delphi? What is the difference between Release and Debug mode?

Debug mode is used while developing the application and Release mode is used while deployment of the application. Google for more details.

34. Have you used any third party components in Delphi?

There are many third party tools which are used in complex Delphi application for UI, ErrorLog, Reporting etc. For example, QuickReport, Reportbuilder are used for reporting, Devexpress is used for layout and designing etc, Eurekalog is used for error tracking.


35. What can Delphi do that other languages cannot do? 

You can learn what latest is going on in Delphi and what features has Delphi acquired in its latest versions. Delphi can generate native codes for Windows, Linux, Mac OS, Android and iOS. This cross platform support of Delphi makes it a unique programming language. 

36. What should be the output of following conversion?

StrToInt('A') --error
StrToInt('2') --2

Friday, January 31, 2014

Frequently Asked Basic PostgreSQL Interview Questions and Answers

Frequently Asked Basic PostgreSQL Interview Questions and Answers

PostgreSQL is the widely used open source database. If you are preparing for PostgreSQL interview, following list of basic PostgreSQL interview questions and answers might help you in your interview preparation. Following PostgreSQL interview questions and answers cover PostgreSQL basic concepts like feature and advantages of PostgreSQL, key difference between MySQL and PostgreSQL, basic PostgreSQL database administration commands and tools, general PostgreSQL database concepts like Stored Procedures, Functions, Triggers, Cursor, Index, Joins, Subqueries etc. 

1. What is PostgreSQL? What do you know about PostgreSQL?

PostgreSQL, often simply "Postgres", is an open-source object-relational database management system (ORDBMS) with an emphasis on extensibility and standards-compliance. It is released under the PostgreSQL License, a free/open source software license, similar to the MIT License. PostgreSQL is developed by the PostgreSQL Global Development Group, consisting of a handful of volunteers employed and supervised by companies such as Red Hat and EnterpriseDB.

Read more about PostgreSQL on Wikipedia and PostgreSQL official website

2. What are the various features and advantages of PostgreSQL?

This is very basic question and you should be updated on this. You should know why are you using PostgreSQL in your project, what features and advantages does PostgreSQL provide.

Visit official PostgreSQL website to learn more features and advantages of PostgreSQL

3. What are the key differences between MySQL and PostgreSQL? Which Open Source Database to Choose? Which one is best? 

MySQL and PostgreSQL are both free and open source powerful and full-featured databases. You should be able to compare these two databases. Here is the complete article on this.

4. What are the various PostgreSQL database administration commands and tools?

You should know basic PostgreSQL database administration commands like creating users in PostgreSQL, setting up user credentials in PostgreSQL, change / update PostgreSQL user password, check whether PostgreSQL is up and running, commands to create, delete, drop, start, stop, restart, backup, restore PostgreSQL database, getting the list of all databases in PostgreSQL, finding out what version of PostgreSQL is running, PostgreSQL help and history commands, commands to get the list of all the tables in a PostgreSQL database, commands to turn on timing and checking how much time a query takes to execute, commands to see the list of available functions in PostgreSQL etc. Here is the complete article on this topic.

You should also know some of the PostgreSQL administration tools. You can visit Wiki and Stackoverflow to get to know various PostgreSQL administration tools.

5. PostgreSQL database general concepts

Beside all this you should be well aware of datatypes in PostgreSQL, DDL, DML, DCL commands used in PostgreSQL. You should have good knowledge of Indexes, Joins, Subqueries, Stored Procedures, Functions, Triggers, Cursors etc.

I hope you will get benefited by these basic PostgreSQL interview questions and answers.

16 PostgreSQL Database Administration Commands

16 PostgreSQL Database Administration Commands

Following are basic PostgreSQL database administration commands which each PostgreSQL database administrator should know. These PostgreSQL database administration commands include creating users in PostgreSQL, setting up user credentials in PostgreSQL, change / update PostgreSQL user password, check whether PostgreSQL is up and running, commands to create, delete, drop, start, stop, restart, backup, restore PostgreSQL database, getting the list of all databases in PostgreSQL, finding out what version of PostgreSQL is running, PostgreSQL help and history commands, commands to get the list of all the tables in a PostgreSQL database, commands to turn on timing and checking how much time a query takes to execute, commands to see the list of available functions in PostgreSQL etc. Lets have a look on following PostgreSQL Database Administration Commands.

1. How to change PostgreSQL root user password?

$ /usr/local/pgsql/bin/psql postgres postgres
Password: (oldpassword)
# ALTER USER postgres WITH PASSWORD 'tmppassword';

$ /usr/local/pgsql/bin/psql postgres postgres
Password: (tmppassword)

Changing the password for a normal postgres user is similar as changing the password of the root user. Root user can change the password of any user, and the normal users can only change their passwords as Unix way of doing.

# ALTER USER username WITH PASSWORD 'tmppassword';

2. How to setup PostgreSQL SysV startup script?

$ su - root

# tar xvfz postgresql-8.3.7.tar.gz

# cd postgresql-8.3.7

# cp contrib/start-scripts/linux /etc/rc.d/init.d/postgresql

# chmod a+x /etc/rc.d/init.d/postgresql

3. How to check whether PostgreSQL server is up and running?

$ /etc/init.d/postgresql status
Password:
pg_ctl: server is running (PID: 6171)
/usr/local/pgsql/bin/postgres "-D" "/usr/local/pgsql/data"
[Note: The status above indicates the server is up and running]

$ /etc/init.d/postgresql status
Password:
pg_ctl: no server running
[Note: The status above indicates the server is down]

4. How to start, stop and restart PostgreSQL database?

# service postgresql stop
Stopping PostgreSQL: server stopped
ok

# service postgresql start
Starting PostgreSQL: ok

# service postgresql restart
Restarting PostgreSQL: server stopped
ok

5. How do I find out what version of PostgreSQL I am running?

$ /usr/local/pgsql/bin/psql test
Welcome to psql 8.3.7, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

test=# select version();
version
----------------------------------------------------------------------------------------------------
PostgreSQL 8.3.7 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 20071124 (Red Hat 4.1.2-42)
(1 row)

test=#

6. How to create a PostgreSQL user?

There are two methods in which you can create user.

Method 1: Creating the user in the PSQL prompt, with CREATE USER command.

# CREATE USER ramesh WITH password 'tmppassword';
CREATE ROLE

Method 2: Creating the user in the shell prompt, with createuser command.

$ /usr/local/pgsql/bin/createuser sathiya
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n
CREATE ROLE

7. How to create a PostgreSQL Database?

There are two metods in which you can create two databases.

Method 1: Creating the database in the PSQL prompt, with createuser command.

# CREATE DATABASE mydb WITH OWNER ramesh;
CREATE DATABASE
Method 2: Creating the database in the shell prompt, with createdb command.

$ /usr/local/pgsql/bin/createdb mydb -O ramesh
CREATE DATABASE
* -O owner name is the option in the command line.

8. How do I get a list of databases in a Postgresql database?

# \l  [Note: This is backslash followed by lower-case L]
List of databases
Name | Owner | Encoding
----------+----------+----------
backup | postgres | UTF8
mydb | ramesh | UTF8
postgres | postgres | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8

9. How to Delete/Drop an existing PostgreSQL database?

# \l
List of databases
Name | Owner | Encoding
----------+----------+----------
backup | postgres | UTF8
mydb | ramesh | UTF8
postgres | postgres | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8

# DROP DATABASE mydb;
DROP DATABASE

10. Getting help on postgreSQL commands

\? will show PSQL command prompt help. \h CREATE will shows help about all the commands that starts with CREATE, when you want something specific such as help for creating index, then you need to give CREATE INDEX.

# \?

# \h CREATE

# \h CREATE INDEX

11. How do I get a list of all the tables in a Postgresql database?

# \d
On an empty database, you’ll get “No relations found.” message for the above command.

12. How to turn on timing, and checking how much time a query takes to execute?

# \timing — After this if you execute a query it will show how much time it took for doing it.

# \timing
Timing is on.

# SELECT * from pg_catalog.pg_attribute ;
Time: 9.583 ms

13. How To Backup and Restore PostgreSQL Database and Table?

We discussed earlier how to backup and restore postgres database and tables using pg_dump and psql utility.

14. How to see the list of available functions in PostgreSQL?

To get to know more about the functions, say \df+

# \df

# \df+

15. How to edit PostgreSQL queries in your favorite editor?

# \e
\e will open the editor, where you can edit the queries and save it. By doing so the query will get executed.

16. Where can I find the PostgreSQL history file?

Similar to the Linux ~/.bash_history file, postgreSQL stores all the sql command that was executed in a history filed called ~/.psql_history as shown below.

$ cat ~/.psql_history
alter user postgres with password 'tmppassword';
\h alter user
select version();
create user ramesh with password 'tmppassword';
\timing
select * from pg_catalog.pg_attribute;

Thursday, January 30, 2014

How to load Help File (.chm file) in your Delphi Project using HTMLHelpViewer?

How to load Help File (.chm file) in your Delphi Project using HTMLHelpViewer?

Having the help option in your Delphi project is very common in which you want to load the chm file. If you want to create such a functionality in your project, here is the solution. It is very simple in Delphi. Following are the steps which you require to implement help functionality in your Delphi Project.

1. Create a valid chm file and place it in a directory where your exe is placed.

2. Include a unit "HTMLHelpViewer" under your uses section. HTMLHelpViewer is needed to register a viewer for HTML help.

3. On the click of help menu button, put the following code:

procedure TMyForm.ExecuteHelp(Sender: TObject);
begin
  If Application.HelpFile <> '' then
    Application.HelpCommand(HELP_CONTEXT,100) 
  else
    Messagedlg('Cannot load help file');
end;

Application.HelpFile fetches the exact address of your chm file. Application.HelpCommand(HELP_CONTEXT,100) loads your chm file in your Delphi project.

I think this simple tutorial will be useful to you for creating help file in your Delphi project.

Monday, January 27, 2014

Calling Conventions in Delphi: safecall vs stdcall vs cdecl

Calling Conventions in Delphi: safecall vs stdcall vs cdecl

Calling conventions in Delphi determine the order in which parameters are passed to the routine. Calling conventions also affect the removal of parameters from the stack, the use of registers for passing parameters, and error and exception handling. 

Calling Conventions in Delphi

One of the most common operations in the processing of code is calling a subroutine that carries out a given task. In order to do that, the main code needs to hand over control to the subroutine, allow the subroutine to execute, and then return to where it left the main execution path. This process requires agreement between the caller and the callee about how to pass information to the subroutine, how to return results where applicable and who is responsible for the memory allocation and cleanup. Various conventions exist to deal with invoking subroutines, commonly known as calling conventions.

Calling conventions in Delphi define a number of different aspects of subroutine invokation:

1. Where parameters are located: in registers or on the stack
2. In which order parameters are passed: right-to-left or left-to-right
3. Who is responsible for cleaning up the parameters afterwards, the caller or the callee

Various calling conventions in Delphi are register, pascal, cdecl, stdcall, and safecall. The default calling convention is register.

The register and pascal conventions pass parameters from left to right; that is, the leftmost parameter is evaluated and passed first and the rightmost parameter is evaluated and passed last. The cdecl, stdcall, and safecall conventions pass parameters from right to left.

For all conventions except cdecl, the procedure or function removes parameters from the stack upon returning. With the cdecl convention, the caller removes parameters from the stack when the call returns.

The register convention uses up to three CPU registers to pass parameters, while the other conventions pass all parameters on the stack.

The safecall convention implements COM error and exception handling.

The default register convention is the most efficient, since it usually avoids creation of a stack frame (Access methods for published properties must use register). The cdecl convention is useful when you call functions from DLLs written in C or C++, while stdcall and safecall are used for Windows API calls. The safecall convention must be used for declaring dual-interface methods. The pascal convention is maintained for backward compatibility. 

Difference between Safecall and Stdcall calling conventions in Delphi

In Delphi, the safecall calling convention encapsulates COM (Component Object Model) error handling, thus exceptions aren't leaked out to the caller, but are reported in the HRESULT return value, as required by COM/OLE. When calling a safecall function from Delphi code, Delphi also automatically checks the returned HRESULT and raises an exception if necessary.

Safecall implements exception 'firewalls'; especially on Win32, this implements interprocess COM error notification. It would otherwise be identical to stdcall (the other calling convention used with the win api)

The safecall calling convention is the same as the stdcall calling convention, except that exceptions are passed back to the caller in EAX as a HResult, while the function result is passed by reference on the stack as though it were a final "out" parameter. When calling a Delphi function from Delphi this calling convention will appear just like any other calling convention, because although exceptions are passed back in EAX, they are automatically converted back to proper exceptions by the caller. When using COM objects created in other languages, the HResults will be automatically raised as exceptions, and the result for Get functions is in the result rather than a parameter. When creating COM objects in Delphi with safecall, there is no need to worry about HResults, as exceptions can be raised as normal but will be seen as HResults in other languages.

function function_name(a: DWORD): DWORD; safecall;

Returns a result and raises exceptions like a normal Delphi function, but it passes values and exceptions as though it was:

function function_name(a: DWORD; out Result: DWORD): HResult; stdcall;

SafeCall function mapping determines which functions are declared as safecall when declarations specified in Delphi are converted to Restricted Interface Definition Language (RIDL) in the generated type library. Safecall functions automatically implement COM conventions for errors and exception handling, converting HRESULT error codes into exceptions. If you are entering function declarations in RIDL, you must explicitly specify the calling convention as safecall or stdcall.

Difference between stdcall and cdecl calling convention

cdecl and __stdcall just tells the compiler whether the called function or the calling function cleans up the stack. In __stdcall calling convention, the called function cleans up the stack when it is about to return. So if it is called in a bunch of different places, all of those calls do not need to extra code to clean up the stack after the function call.

In __cdecl calling convention, it is the caller function that is responsible for cleaning the stack, so every function call must also need to include extra code to clean up the stack after the function call.

Default Calling Convention for C programmes

The __cdecl is the default calling convention for C programs. In this calling convention, the stack is cleaned up by the caller. The __cdecl calling convention creates larger executables than __stdcall, because it requires each function call to include stack cleanup code.

Default Calling Convention for Windows Programmes

The __stdcall calling convention is used to call Win32 API functions. The callee cleans the stack. Functions that use this calling convention require a function prototype.

return-type __stdcall function-name[(argument-list)]

Saturday, January 25, 2014

Devexpress vs Telerik vs Infragistics: Which one to choose and why?

Devexpress vs Telerik vs Infragistics: Which one to choose and why?

Devexpress, Telerik and Infragistics are the great third party tools for you for both your windows and web application. These third party tools mainly support .NET technologies like WinForms, Silverlight, ASP.NET and WPF. Devexpress also supports windows application built in Delphi programming language and I am currenty working on it. Also I have heard some other third party tools like ComponentOne, SyncFusion and Januscontrols but never worked upon them. Which one is good, which one to choose and why? Although this is difficult and time consuming task, but let's try to solve the confusion.

Why do we require these third party tools for our application?

These all thrid party tools are mainly used for improving the user interface which standard controls cannot provide. Devexpress, Telerik and Infragistics third party tools provide a lot of exciting features and components which you require like attractive dashboards, advanced reporting tools, sorting, searching, grouping, paging, filtering, databinding, drag and drop features, intelligent editors, smart control panels, importing and exporting features, attractive ribbon bars, complex tree views and graphs, cool transition controls, rich textboxes, navigation contorls, charting controls, complex themes and much more...

Devexpress vs Telerik vs Infragistics: Which one to choose and why?

Which tool is best? This is very hard to say. This question seems philosophical to me. There's no one answer.

Devexpress, Telerik and Infragistics have their own strong and weak points. Each and every tool has some learning curve to understand its way of doing the things. These all tools provide excellent performance and great convenience to the user. 

It is upto you and your needs to choose one of these. Following are some humble suggestions which you can consider before choosing one of these tools for your windows or web application.

1. What is your requirement?

What is the nature of your application and what functionalities do you want to put in? This should be the first questions striking in your mind before choosing any thrid party tool for your UI. Once you finalized your requirement, then move to these contorls and read their documentation, features list, online demos, try trial versions to implement your functionality. If the tool is giving you your desired functionality, consider it.

2. Your familiarity with tool

A good choice, nevertheless is to choose one you know. I would start by expressing my opinion that the best tool set is the one with which you gain your greatest familiarity. I think the market demonstrates that there is a sufficient percentage of buyers to support all these three tools. I would also conjecture that the longer you work in any one, the more you will like it. 

My observation is that each library comes with its own set of presuppositions: about structure, nomenclature, interface and class design and inheritance; about consistency and your mental model of the controls and events; about many things. 

Some of these presuppositions blend easily with the way some people think. Others blend better with other brain architecture. It doesn't mean one is better or worse. It means one is different than the other. You have to find the one that works for the way you think and program. Otherwise you will be twisting on a right-hand nut with a left-hand wrench.

3. Which tool your co-workers and other teams of your company are using?

You should also investigate with other teams in your company if they are using any of these tools. If yes, learn from their experience. 

4. Support and Documentation

Evaluate each tool in terms of their support and documentation. Go with tool which provide solid support, documentation, sample examples, tutorials, example codes and demonstrations. All the component libraries should be well-represented and documented. When you will use a third party software or tool, you will need much help from those people. The tool vendor should have good forum.

5. Cross browser compliant

If you are looking for Devexpress, Telerik and Infragistics for you web application, must ensure that all the features which you need from these third party tools run same on all the browsers. 

6. Cost and Licensing 

Cost should be reasonable and licensing should be simple. 

Conclusion

To be fair, all grid components will do great if you're on the standard track. As soon as you'll have something to implement that is not a general purpose usage it will get harder. This is true for all third party controls.

Compare demos and trial versions based on your needs. I would encourage you to evaluate the controls on your own to see which control will best meet your needs. That's the best suggestion I can suggest.

Friday, January 17, 2014

MongoDB vs Cassandra: Difference and Similarities between MongoDB and Cassandra

MongoDB vs Cassandra: Difference and Similarities between MongoDB and Cassandra

MongoDB and Cassandra have occupied a very big market of NoSQL Schema-Free databases. So, before going to choose any one of them, it is logical to compare basic features of both MongoDB and Cassandra. MongoDB and Cassandra both are fully-featured NoSQL databases and choosing one of them heavily depends upon your application requirements. There are a lot of similarities and differences between MongoDB and Cassandra. Following is the basic comparison of MongoDB and Cassandra in terms of data storage model, usage etc.

Differences between MongoDB and Cassandra

1. MongoDB has a document-oriented data model while Cassandra has column-oriented data model and storage type. 

MongoDB acts much like a relational database. Its data model consists of a database at the top level, then collections which are like tables in MySQL (for example) and then documents which are contained within the collection, like rows in MySQL. Each document has a field and a value where this is similar to columns and values in MySQL. Fields can be simple key / value e.g. { 'name': 'David Mytton' } but they can also contain other documents e.g. { 'name': { 'first' : David, 'last' : 'Mytton' } }.

In Cassandra documents are known as “columns” which are really just a single key and value. e.g. { 'key': 'name', 'value': 'David Mytton' }. There’s also a timestamp field which is for internal replication and consistency. The value can be a single value but can also contain another “column”. These columns then exist within column families which order data based on a specific value in the columns, referenced by a key. At the top level there is a keyspace, which is similar to the MongoDB database.

2. MongoDB is developed by MongoDB, Inc while Cassandra is a product of Apache Software Foundation. The original authors of MongoDB are core contributors to the code and work for 10gen (indeed, 10gen was founded specifically to support MongoDB and the CEO and CTO are the original creators). In contrast, Cassandra was created by 2 engineers from Facebook and is incubated by the Apache Foundation. MongoDB was initially released in 2009 while Cassandra was released in 2008.

3. MongoDB is implemented in C++ while Cassandra is implemented in Java.

4. MongoDB supports Linux, OS X, Solaris and Windows server operating systems while Cassandra supports BSD, Linux, OS X and Windows server operating systems.

5. MongoDB supports more programming languages than Cassandra. MongoDB supports Actionscript, C, C#, C++, Clojure, ColdFusion, D, Dart, Delphi, Erlang, Go, Groovy, Haskell, Java, JavaScript, Lisp, Lua, MatLab, Perl, PHP, PowerShell, Prolog, Python, R, Ruby, Scala and Smalltalk. Cassandra supports C#, C++, Clojure, Erlang, Go, Haskell, Java, JavaScript, Perl, PHP, Python, Ruby and Scala. 

6. Handling of indexes is different in MongoDB and Cassandra

MongoDB indexes work very similar to relational databases. You create single or compound indexes on the collection level and every document inserted into that collection has those fields indexed. Querying by index is extremely fast so long as you have all your indexes in memory.

Prior to Cassandra 0.7 it was essentially a key/value store so if you want to query by the contents of a key (i.e the value) then you need to create a separate column which references the other columns i.e. you create your own indexes. This changed in Cassandra 0.7 which allowed secondary indexes on column values, but only through the column families mechanism. Cassandra requires a lot more meta data for indexes and requires secondary indexes if you want to do range queries.

7. Handling of replication is different in MongoDB and Cassandra

In MongoDB replication is achieved through replica sets. This is an enhanced master/slave model where you have a set of nodes where one is the master. Data is replicated to all nodes so that if the master fails, another member will take over. There are configuration options to determine which nodes have priority and you can set options like sync delay to have nodes lag behind (for disaster recovery, for example).

Writes in MongoDB are “unsafe” by default; data isn’t written right away by default so it’s possible that a write operation could return success but be lost if the server fails before the data is flushed to disk. This is how Mongo attains high performance. If you need increased durability then you can specify a safe write which will guarantee the data is written to disk before returning. Further, you can require that the data also be successfully written to n replication slaves.

MongoDB drivers also support the ability to read from slaves. This can be done on a connection, database, collection or even query level and the drivers handle sending the right queries to the right slaves, but there is no guarantee of consistency (unless you are using the option to write to all slaves before returning). In contrast Cassandra queries go to every node and the most up to date column is returned (based on the timestamp value).

Cassandra has much more advanced support for replication by being aware of the network topology. The server can be set to use a specific consistency level to ensure that queries are replicated locally, or to remote data centres. This means you can let Cassandra handle redundancy across nodes where it is aware of which rack and data centre those nodes are on. Cassandra can also monitor nodes and route queries away from “slow” responding nodes.

The only disadvantage with Cassandra is that these settings are done on a node level with configuration files whereas MongoDB allows very granular ad-hoc control down the query level through driver options which can be called in code at run time.

8. MongoDB provides a custom map/reduce implementation while Cassandra provides native Hadoop support, including for Hive (a SQL data warehouse built on Hadoop map/reduce) and Pig (a Hadoop-specific analysis language that many think is a better fit for map/reduce workloads than SQL).

9. MongoDB supports server-side scripting while Cassandra does not.

10. Major users of MongoDB are Craigslist, Foursquare, Shutterfly, Intuit while Facebook, Twitter and Digg heavily use Cassandra.

11. MongoDB is commercial while Cassandra is free.

Similarities between MongoDB and Cassandra

1. Both MongoDB(AGPL) and Cassandra(Apache 2.0 license) are open-source databases.
2. Both MongoDB and Cassandra are NoSQL schema-free databases.
3. Both MongoDB and Cassandra support Sharding.
4. Both MongoDB and Cassandra support concurrency. Concurrency is supported by using Locks in MongoDB while concurrency in Cassandra is achieved by MVCC.
5. Both MongoDB and Cassandra support Eventual and Immediate Consistency. 
6. None supports Foreign keys, Transaction concepts, Triggers etc.