Search code examples
amazon-web-servicesamazon-ec2websocketautoscalinghorizontal-scaling

Trying to understand how does the AWS scaling work


There is one thing of scaling that I yet do not understand. Assume a simple scenario ELB -> EC2 front-end -> EC2 back-end

enter image description here

When there is high traffic new front-end instances are created, but, how is the connection to the back-end established?

How does the back-end application keep track of which EC2 it is receiving from, so that it can respond to the right end-user?

Moreover, what happen if a connection was established from one of the automatically created instances, and then the traffic is low again and the instance is removed.. the connection to the end-user is lost?

FWIW, the connection between the servers is through WebSocket.


Solution

  • Assuming that, for example, your ec2 'front-ends' are web-servers, and your back-end is a database server, when new front-end instances are spun up they must either be created from a 'gold' AMI that you previously setup with all the required software and configuration information, OR as part of the the machine starting up it must install all of your customizations (either approach is valid). with either approach they will know how to find the back-end server, either by ip address or perhaps a DNS record from the configuration information on the newly started machine.

    You don't need to worry about the backend keeping track of the clients - every client talking to the back-end will have an IP address and TCPIP will take care of that handshaking for you.

    As far as shutting down instances, you can enable connection draining to make sure existing conversations/connections are not lost:

    When Connection Draining is enabled and configured, the process of deregistering an instance from an Elastic Load Balancer gains an additional step. For the duration of the configured timeout, the load balancer will allow existing, in-flight requests made to an instance to complete, but it will not send any new requests to the instance. During this time, the API will report the status of the instance as InService, along with a message stating that “Instance deregistration currently in progress.” Once the timeout is reached, any remaining connections will be forcibly closed.

    https://aws.amazon.com/blogs/aws/elb-connection-draining-remove-instances-from-service-with-care/