Sunday, December 23, 2018

AWS Database Services: RDS, DynamoDB, ElastiCache, Neptune and Redshift

AWS provides following services under database section:

1. RDS
2. DynamoDB
3. ElastiCache
4. Neptune
5. Redshift

Following are some basic and important points about AWS Database services:

RDS

1. Relational Database Service. Supports Aurora, MySQL, MariaDB, PostgreSQL, Oracle, MS SQL Server.

2. Backup and Restore methods: Automated (done by AWS automatically, backs up data with transaction logs) and Snapshots (manual process, usually done by system admins).

3. To improve DB performance, you can use ElastiCache, DAX and Read Replicas.

Aurora 

1. Combination of MySQL and PostgreSQL (RDBMS based).

2. Up to 5 times faster than standard MySQL databases and 3 times faster than standard PostgreSQL databases.

3. Automatically scales up to 64TB on SSD per database instance.

4. Replicates 6 copies of database across 3 Availability Zones.

5. Each DB cluster can have up to 15 READ replicas.

6. Failover takes less than 30 seconds.

7. Backs up database to S3.

8. You can monitor database performance using Amazon CloudWatch.

DynamoDB

1. DynamoDB (Dynamo Database or DDB) is Amazon NoSQL Database.

2. DynamoDB Security is provided by Fine-Grained Access Control (FGAC) mechanism. FGAC is based on the AWS IAM.

3. DynamoDB Triggers integrate with AWS Lambda.

4. DynamoDB Streams provides a 24-hour chronological sequence of updates to items in a table. AWS Lambda can read updates to a table from a stream.

5. Dynamo DB Accelerator (DAX) is in-memory database cache for Dynamo DB.

Neptune

1. Graph based database

Redshift 

1. Data Warehouse and Reporting System in the Amazon Cloud.

2. Use OLAP (Online Analytical Processing), SQL and BI tools to analyze the data.

3. Redshift Spectrum extends the power of Redshift to query unstructured data in S3 (without loading your data into Redshift).

ElastiCache

1. In-memory database cache in the Amazon Cloud for fast performance.

2. ElastiCache Engines: Redis, Memcached

DMS

1. Database Migration Service with zero/negligible downtime.

2. Supports homogenous (example: Oracle to Oracle) and heterogeneous (example: Oracle to Aurora or MySQL) database migration.

Thursday, October 5, 2017

Use of Private Constructor in OOPS

If we set access specifier of a constructor to private, then that constructor can only be accessed inside the class. A private constructor is mainly used when you want to prevent the class instance from being created outside the class. 

This is mainly in the case of singleton class. Singleton classes are employed extensively in concepts like Networking and Database Connectivity. Using private constructor we can ensure that no more than one object can be created at a time. 

Example of Private Constructor in a Singleton Class

public class SingletonClass
{
    public static SingletonClass singletonClass;

    private SingletonClass() 
    {
    }

    public static SingletonClass getInstance() 
    {
        if(singletonClass == null) 
        {
            singletonClass = new SingletonClass();
        }
        return singletonClass;
    }
}

A class with private constructor cannot be inherited.

If we don’t want a class to be inherited, then we make the class constructor private. So, if we try to derive another class from this class then compiler will flash an error. Why compiler will flash an error? 

We know the order of execution of constructor in inheritance that when we create an object of a derived class then first constructor of the base call will be called and then constructor of derived class. Since base class constructor is private, hence, derived class will fail to access base class constructor.

We can also use sealed class to stop a class to be inherited. Sealed class provide more flexible and readable way to stop inheritance.

Diamond Problem in Multiple Inheritance in OOPS

The diamond problem occurs when two super classes of a class have a common base class. 

Suppose there are four classes A, B, C and D. Class B and C inherit class A. Now class B and C contains one copy of all the functions and data members of class A. Class D is derived from class B and C. Now class D contains two copies of all the functions and data members of class A. One copy comes from class B and another copy comes from class C.

Let’s say class A has a function with name display(). So class D have two display() functions as I have explained above. If we call display() function using class D object then ambiguity occurs because compiler gets confused that whether it should call display() that came from class B or from class C. If you will compile above program then it will show error.

This kind of problem is called diamond problem as a diamond structure is formed (see the image).

That is why major programming languages like C#, Java and Delphi don't have multiple inheritance because it can lead to diamond problem and rather than providing some complex way to solve it, there are better ways through which we can achieve the same result as multiple inheritance. We can use interfaces to resolve this problem.

C++ supports multiple inheritance.

Notice that the above problem with multiple class inheritance can also come with only three classes where all of them has at least one common method.

Because of this problem we can not extend two classes for implementing multiple inheritance and to resolve this problem of multiple inheritance in object oriented programming we use interfaces for implementing the functionality of multiple inheritance. 

As we know we do not define a function but only declare that function in an interface. So if we use interfaces we can extend one class and one or more interfaces or we can implement more than one interfaces at a time to use the functionality of multiple inheritance and we can escape from diamond problem.

Thursday, September 28, 2017

How to declare Static/Class variables, properties, functions and procedures in Delphi?

In Delphi, we do have static variables, properties, functions and procedures, but instead of the word "static" we refer the word "class". In this tutorial, I have shown the syntax of declaring the static variables, properties, functions and procedures in Delphi. We don't have to use "static" keyword while declaring the static variables and properties, just use "class" keyword. While declaring static functions and procedures, we have to use both "class" and "static" keyword.

Declare a static/class variable in Delphi

type
  TStaticDemoClass = class(TObject)
  public
    class var StaticVar: integer;
  end;
Declare a static/class property in Delphi

type
  TStaticDemoClass = class(TObject)
  private
    class var FStaticVar: integer;
  public
    class property StaticVar: integer read FStaticVar write FStaticVar;
  end;

Now we can use above StaticVar variable/property anywhere in the unit like this:
procedure TForm1.Button1Click(Sender: TObject);
var a:integer;
begin
  TStaticDemoClass.StaticVar := 1;
end;

We can also use static/class functions and procedures in Delphi. Below example, I have copied from documentation:

type
  TMyClass = class
    strict private
      class var FX: Integer;
    strict protected  
      //Note: accessors for class properties must be declared class static.
      class function GetX: Integer; static;
      class procedure SetX(val: Integer); static;
    public
      class property X: Integer read GetX write SetX;
      class procedure StatProc(s: String); static;
  end;

TMyClass.X := 17;
TMyClass.StatProc('Hello');

You need to use "static" keyword after function / procedure declaration. Classes can have static class methods -- i.e. methods that can be called from a class type. Class static methods can be accessed without an object reference. Unlike ordinary class methods, class static methods have no Self parameter at all. They also cannot access any instance members. They still have access to class fields, class properties, and class methods. Also unlike class methods, class static methods cannot be declared virtual.

Tuesday, September 26, 2017

VCL Hierarchy in Delphi: Types of Controls in Delphi: TWinControls and TGraphicControls

In VCL (Visual Component Library) hierarchy, you will find TObject at the top, then TPersistent, then TComponent and then TControl.

TObject >> TPersistent >> TComponent >>TControl

VCL Hierarchy in Delphi
TObject is the base class from which all classes descend.

TPersistent class descends directly from TObject. The special characteristic of TPersistent is that it is an abstract class that defines the methods that allow it to be streamed.

TComponent is the base class from which all components descend. Non-visual components are descendants of TComponent. For example: TTimer

TControl is the base class from which all the visual components descend. For example: TEdit, TListBox, TComboBox etc.

Again, there are two types of controls (TControl): Window Controls (TWinControl) and Graphic Controls (TGraphicControl)

Following is the difference between TWinControl and TGraphicControl in Delphi:

TWinControls

TWinControls can receive input focus and they can be parents to other controls.

Example: TEdit, TListBox and TComboBox

TGraphicControl

TGraphicControls differ from TWinControls in that they cannot receive input focus. Also, they cannot be parents to other controls.

Example: TLabel

TGraphicControls are used when you want to display something on the form.

Two key advantages of TGraphicControls over TWinControls:

1. TGraphicControls don't use up system resources since they don't require a window handle.

2. They are a bit faster at drawing than their TWinControl counterparts since their painting is not subject to the windows message dispatching system. Instead, they piggyback on their parent's paint process.

For more details, please refer to documentation.

What is the difference between Singleton Class and Static Class? Which one to use and when?

Almost both singleton class and static class serve the same purpose which creates confusion among developers and architects about which one to use. When to use singleton class and when to use static class. In this article I have tried to differentiate between the two. Following is the list of differences between a singleton class and a static class:

1. Singleton classes are more inclined to the object-oriented concepts as compared to the static classes. With Singleton classes, you can use inheritance and polymorphism to extend a base class, implement an interface and capable of providing different implementations. While a static class cannot inherit their instance members. So, Singleton classes are more flexible than static classes.

2. Singleton classes can be passed as an object to other methods while a static class allows only static methods and you cannot pass static class as parameter.

3. If your requirement is to maintain the state, then singleton class is the better choice than static class, because maintaining the state with static classes is very cumbersome and leads to subtle bugs.

4. Singleton classes can be lazy loaded if it’s a heavy object, but static class doesn't have such advantages and always eagerly loaded. Static class is generally initialized when it is first loaded and it will lead to potential class loader issues.

5. Static classes are hard to mock and consequently hard to test than singletons, which are easy to mock and thus easy to test. It’s easier to write unit tests for singleton than static classes, because you can pass mock object whenever singleton is expected, e.g. into constructor or as method arguments.

6. Singleton classes provide more flexibility. Consider the possibility that your application has a global shared object and tomorrow you decide that you must make more than one instance of the class. If you use a singleton class then all you should do is make the constructor public. This is not so simple with static classes.

7. Many Dependency Injection framework manages singleton quite well e.g. Spring, which makes using them very easy.

8. Singleton objects are stored in Heap, but static objects are stored in Stack.

9. Static class provides better performance than singleton class, because static methods are bonded on compile time.

10. Singleton classes maintain single object across the application life cycle, so the scope is at application level. The static does not have any object pointer, so the scope is at App Domain level. Moreover, both should be implemented to be thread-safe.

Thursday, September 14, 2017

Security and Privacy issues with IoT (Internet of Things) - Top barriers to IoT success

IoT is one of the biggest breakthroughs in the history of the tech industry. It will soon be an inherent part of every aspect of our lives. Security and Privacy are the most significant challenge for the IoT. Security and Privacy issues are acting as top barriers to IoT success. So, addressing underlying security and privacy concerns is very crucial. 

As more and mored devices are being added to the IoT everyday, privacy and security concerns are becoming vital. 

Security issue with IoT devices

Security is one of the main concerns of the IoT ("Internet of Things"). With billions of devices connected to the internet, it is needless to say that to protect your connected devices from malware penetrations is a real challenge. Security risks of IoT devices cannot be ignored whether it is from hackers or corporations. Increasing the number of connected devices increases the opportunity to exploit security vulnerabilities.

When IoT devices are connected to the cloud or data centers via internet, they get under the risk of being accessible to the outside world. Poorly designed devices can easily expose user data to theft. 

There are many reasons behind the state of insecurity in IoT. 

Companies do not update their devices enough or at all. This means that an IoT device which was safe when you first bought it can become unsafe as hackers discover new vulnerabilities.

The fundamental security weakness of the Internet of Things is that it increases the number of devices on the internet. Earlier, most of us had to only worry about protecting our computers, then, we had to worry about protecting our smartphones as well, and now after IoT, we have to worry about protecting our car, our home appliances, our wearables, and many other IoT devices.

Because there are so many devices that can be hacked, that means that hackers can accomplish more. You may have heard about how hackers could potentially remotely control cars and remotely accelerate or decelerate the car. But hackers could use even seemingly unimportant devices like baby monitors or your thermostat to uncover private information or just ruin your day. The point is that we have to think about what a hacker could do with a device if he can break through its security. 

In this way, our normal day to day life and health can become the target of IoT hack attacks. Urban infrastructure can also become a target for hackers.

Privacy issue with IoT devices

Corporations which create and distribute interconnected devices could also use these devices to obtain personal data, particularly dangerous when used for money transfers. Manufacturers can steal the information about your habits and misuse it. 

For example, consider how some companies are distributing Fitbits to their employees so that they can track their health and thus get lower health insurance premiums. Even if we ignore the worrying idea of workers’ health being monitored by corporations around the clock, there is the question of what corporations can do with the data they have gathered. Companies can attempt to send or even sell gathered data to other companies, which raises issues regarding our individual privacy rights.

Vision features and voice recognition are now being integrated into smart TVs. These features can listen continuously to conversations or look for activity and transmit data selectively to cloud services for processing. These cloud services may sometimes even include third parties. The collection of all this information faces a number of regulatory and legal challenges.

The collection of private information exposes legal and regulatory challenges facing data protection and privacy law. There are a wide range of regulatory and legal questions surrounding the IoT, which need thoughtful consideration. Legal issues with IoT devices include crossborder data flow, conflict between law enforcement surveillance and civil rights, data retention and destruction policies, and legal liability for unintended uses, security breaches or privacy lapses. 

In short, Security and Privacy issues are the biggest issues which consumers, businesses and governments need to consider before using IoT connected devices.