Home > Articles > Web Development

  • Print
  • + Share This
This chapter is from the book

Application Layer

The application layer is where you'll probably spend most of your time because it is the heart and soul of any SaaS system. This is where your code translates data and requests into actions, changing, manipulating, and returning data based on inputs from users, or other systems. This is the only layer that you have to actually maintain and scale, and even then, some cloud providers offer you unique solutions to handle that automatically for you. In Google AppEngine, this is handled automatically for you. In Amazon Web Services, this can be handled semi-automatically for you by using Auto-Scaling Groups, for which you can set rules on when to start and stop instances based on load averages or other metrics.

Your application layer is built on top of a base image that you created and may also contain scripts that tell it to update or add more code to that running instance. It should be designed to be as modular as possible and enable you to launch new modules without impacting the old ones. This layer should be behind a proxy system that hides how many actual modules are in existence. Amazon enables you to do this by providing a simple service known as Elastic Load Balancing, or ELB.

Using Elastic Load Balancing

Amazon's Elastic Load Balancing, or ELB, can be used simply and cheaply to proxy all requests to your modules based on their Instance ID. ELB is even smart enough to proxy only to systems that are actually live and processing, so you don't have to worry about server failures causing long-term service disruptions. ELB can be set up to proxy HTTP or standard TCP ports. This is simple to accomplish using code and can even be done on the actual instance as it starts, so it can register itself when it's ready to accept connections. This, combined with Auto-Scaling Groups, can quickly and easily scale your applications seamlessly in a matter of minutes without any human interaction. If, however, you want more control over your applications, you can just use ELB without Auto-Scaling Groups and launch new modules manually.

Creating and managing ELBs is quite easy to accomplish using boto and the elbadmin command-line tool that I created, which comes with boto. Detailed usage of this tool can be found by running it on the command line with no arguments:

% elbadmin
Usage: elbadmin [options] [command]
Commands:
    list|ls                           List all Elastic Load Balancers
    delete    <name>                  Delete ELB <name>
    get       <name>                  Get all instances associated
                                      with <name>
    create    <name>                  Create an ELB
    add       <name> <instance>       Add <instance> in ELB <name>
    remove|rm <name> <instance>       Remove <instance> from ELB
                                      <name>
    enable|en <name> <zone>           Enable Zone <zone> for ELB
                                      <name>
    disable   <name> <zone>           Disable Zone <zone> for ELB
                                      <name>


Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -z ZONES, --zone=ZONES
                        Operate on zone
  -l LISTENERS, --listener=LISTENERS
                        Specify Listener in,out,proto

The first thing to do when starting out is to create a new ELB. This can be done simply as shown here:

% elbadmin -l 80,80,http -z us-east-1a create test
Name: test
DNS Name: test-68924542.us-east-1.elb.amazonaws.com

Listeners
---------
IN       OUT      PROTO
80       80       HTTP

  Zones
---------
us-east-1a

Instances
---------

You must pass at least one listener and one zone as arguments to create the instance. Each zone takes the same distribution of requests, so if you don't have the same amount of servers in each zone, the requests will be distributed unevenly. For anything other than just standard HTTP, use the tcp protocol instead of http. Note the DNS Name returned by this command, which can also be retrieved by using the elbadmin get command. This command can also be used at a later time to retrieve all the zones and instances being proxied to by this specific ELB. The DNS Name can be pointed to by a CNAME in your own domain name. This must be a CNAME and not a standard A record because the domain name may point to multiple IP addresses, and those IP addresses may change over time.

Recently, Amazon also released support for adding SSL termination to an ELB by means of the HTTPS protocol. You can find instructions for how to do this on Amazon's web page. At the time of this writing, boto does not support this, so you need to use the command-line tools provided by Amazon to set this up. The most typical use for this will be to proxy port 80 to port 443 using HTTPS. Check with the boto home page for updates on how to do this using the elbadmin command-line script.

Adding Servers to the Load Balancer

After you have your ELB created, it's easy to add a new instance to route your incoming requests to. This can be done using the elbadmin add command:

% elbadmin add test i-2308974

This instance must be in an enabled zone for requests to be proxied. You can add instances that are not in an enabled zone, but requests are not proxied until you enable it. This can be used for debugging purposes because you can disable a whole zone of instances if you suspect a problem in that zone. Amazon does offer a service level agreement (SLA), ensuring that it will have 99% availability, but this is not limited to a single zone, thus at any given time, three of the four zones may be down. (Although this has never happened.)

It's generally considered a good idea to use at least two different zones in the event one of them fails. This enables you the greatest flexibility because you can balance out your requests and even take down a single instance at a time without effecting the service. From a developer's perspective, this is the most ideal situation you could ever have because you can literally do upgrades in a matter of minutes without having almost any impact to your customers by upgrading a single server at a time, taking it out of the load balancer while you perform the upgrade.

Although ELB can usually detect and stop proxying requests quickly when an instance fails, it's generally a good idea to remove an instance from the balancer before stopping it. If you're intentionally replacing an instance, you should first verify that the new instance is up and ready, add it to the load balancer, remove the old instance, and then kill it. This can be done with the following three commands provided in boto package:

% elbadmin add test i-2308974
% elbadmin rm test i-0983123
% kill_instance i-0983123

The last command actually terminates the instance, so be sure there's nothing on there you need to save, such as log files, before running this command. After each of these elbadmin commands, the full status of that load balancer is printed, so be sure before running the next command that the previous command succeeded. If a failure is reported, it's most likely because of an invalid instance ID, so be sure you're copying the instance IDs exactly. One useful tool for this process is the list_instances command, also provided in the boto package:

% list_instances
ID            Zone          Groups       Hostname
------------------------------------------------------------------
i-69c3e401    us-east-1a    Wordpress    ..compute-1.amazonaws.com
i-e4675a8c    us-east-1c    default      ..compute-1.amazonaws.com
i-e6675a8e    us-east-1d    default      ..compute-1.amazonaws.com
i-1a665b72    us-east-1a    default      ..compute-1.amazonaws.com

This command prints out the instance IDs, Zone, Security Groups, and public hostname of all instances currently running in your account, sorted ascending by start date. The last instances launched will be at the bottom of this list, so be sure to get the right instance when you're adding the newest one to your ELB. The combination of these powerful yet simple tools makes it easy to manage your instances and ELB by hand.

Although the load balancer is cheap (about 2.5 cents per hour plus bandwidth usage), it's not free. After you finish with your load balancer, remove it with the following command:

% elbadmin delete test

Automatically Registering an Instance with a Load Balancer

If you use a boto pyami instance, you can easily tell when an instance is finished loading by checking for the email sent to the address you specify in the Notification section of the configuration metadata passed in at startup. An example of a configuration section using gmail as the smtp server is shown here:

[Notification]
smtp_host = smtp.gmail.com
smtp_port = 587
smtp_tls = True
smtp_user = my-sending-user@gmail.com
smtp_pass = MY_PASSWORD
smtp_from = my-sending-user@gmail.com
smtp_to = my-recipient@gmail.com

Assuming there were no error messages, your instance should be up and fully functional. If you want the instance to automatically register itself when it's finished loading, add an installer to your queue at the end of your other installers. Ensure that this is done after all your other installers finish so that you add only the instance if it's safe. A simple installer can be created like the one here for ubuntu:

from boto.pyami.installers.ubuntu.installer import Installer
import boto
class ELBRegister(Installer):
    """Register this instance with a specific ELB"""
    def install(self):
        """Register with the ELB"""
        # code here to verify that you're
        # successfully installed and running
        elb_name = boto.config.get("ELB", "name")
        elb = boto.connect_elb()
        b = ebl.get_all_load_balancers(elb_name)
        if len(b) <1:
            raise Exception, "No Load balancer found"
        b = b[0]

        b.register_instances([boto.config.get_instance ("instance_id")])
        def main(self):
        self.install()

This requires you to set your configuration file on boot to contain a section called ELB with one value name that contains the name of the balancer to register to. You could also easily adapt this installer to use multiple balancers if that's what you need. Although this installer will be called only if all the other installers before it succeed, it's still a good idea to test anything important before actually registering yourself with your balancer.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020