News for hackers. Stuff that matters.
Re:Nginx with dynamic upstreams by jpmattia on Sunday July 26, 2015 @01:30PM (#9951638)
Am I wrong for thinking that failure to honor the TTL in the first place is a bug?
Re:Nginx with dynamic upstreams by lisper on Sunday July 26, 2015 @02:10PM (#9951747)
It's worse than a bug, it's a Really Bad Design (tm). Bugs are usually unintentional, but this is a deliberate reinvention of a wheel (IP address caching) that doesn't need to be reinvented.

But this is not just nginx's fault. It's also Bad Design on the part of AWS because switching IP addresses this way means you can't keep a TCP socket open to an ELB machine for more than 60 seconds at a time because you never know when the routing rug is going to be yanked out from under you. This makes ELB useless for anything involving a persistent connection. No websockets for you!

Re:Nginx with dynamic upstreams by twic on Sunday July 26, 2015 @02:14PM (#9951753)
It's not quite as bad as that. AWS's short TTL means that they can change the IP whenever they like, not that they will. I would imagine that most of the time, long-lived connections to an ELB will be fine.

If the ELB does get a new address, then yes, the connection will fail, the client will have to reconnect, and when it does, it will need to do a fresh address lookup. But since the connection is over a network, failure is a possibility regardless of what AWS does, and so clients need to be able to detect failure and reconnect anyway.

Re:Nginx with dynamic upstreams by lisper on Sunday July 26, 2015 @02:34PM (#9951811)
The problem is not the frequency with which it happens. The problem is that the way you get notified that it has happened is that IP packets suddenly start to get delivered to the wrong machine. If you're lucky, the net result will simply be a dropped TCP connection. If you're not lucky, pretty much arbitrarily bad things can happen.

Now, it is true that IP is not reliable, and so arbitrarily bad things can happen at any time and you do have to be prepared for those. The problem here is that there is no notification. Potentially bad things are happening here not because something has gone wrong, but by design. That, IMHO, is the very definition of Bad Design.

Normally, when you make a DNS change, you control the DNS and the affected end points. That way you know when the change is happening, and you can give yourself as much time as you need to make the transition in an orderly way. With ELB, the only guarantee you have is the TTL. With a 60 second TTL, that means you have at most 60 seconds to do the transition, and even that is only if you notice it when it happens (and the only way to guarantee that is to poll the name server constantly).

Re:Nginx with dynamic upstreams by devonkim on Sunday July 26, 2015 @03:28PM (#9951948)
I like to think of the problem as an inherent issue with cache invalidation in distributed systems that has no straightforward solution in an Internet architecture. DNS problems greatly resemble what it's like to have dangling pointers, for another analogy.

I think the expected solution approach is a little like validating 2FA logins. To account for system variability, you accept that transactions can take up to a certain time and you allow multiple values (the similarities probably end there, admittedly). With a more advanced solution you would even advise clients to invalidate their own cache too (similar to load shedding by advising / redirecting clients - you can see this in 2FA logins sometimes where you're asked for the sequence after the once-valid key just entered). So I think you'd need to accept multiple prior CNAME resolutions to account for longer lived transactions and make sure each entry change will be valid only for so long. Being able to be notified of these changes programmatically would be really nice though specifically for AWS. Perhaps AWS Lambda or SNS could be leveraged for pushing AWS DNS state change notifications to your system?

One approach I've seen some folks do is to simply reload their nginx configurations across their backend nodes to refresh the cached entry. It's probably intractable without sacrificing a lot of theoretical availability with in a degenerate state of reloading upon each request when your CNAME changes several times a second. Some of my colleagues have experienced problems even with the nginx proxy_pass directive that most people say is the most recommended free solution and also recommended in the article.

Re:Nginx with dynamic upstreams by Tenzer on Sunday July 26, 2015 @02:19PM (#9951761)
Regarding how the ELB works, then I think the AWS engineers have a different idea on how to implement stuff. AWS is very much a dynamic platform and the engineers seem to have embraced this when they came up with this solution.

It doesn't necessarily mean you can't use websockets through an ELB, it just means that you would need to be able to handle reconnects, but that shouldn't be a new challenge for any system relying on connections being open for long. Also, the load balancer servers doesn't switch every 60 seconds, you can have connections running for a lot longer than that. I would also assume the load balancers keep handling connections for a while after they were taken out of the DNS rotation, in order to make sure DNS caches are updated before the IP addresses stops working.

Re:Nginx with dynamic upstreams by JimmyL on Sunday July 26, 2015 @02:38PM (#9951823)
AWS support has said that ELBs will continue to accept connections (and use the correct backend) for at least an hour after the CNAME stops resolving to a particular IP.
Re:Nginx with dynamic upstreams by lisper on Sunday July 26, 2015 @02:45PM (#9951845)
Ah, I didn't know that. That makes a big difference.

But there's still a significant hole here: is there any way to get notified that this has happened other than polling the DNS?

Re:Nginx with dynamic upstreams by lisper on Sunday July 26, 2015 @02:44PM (#9951841)
> you would need to be able to handle reconnects, but that shouldn't be a new challenge for any system relying on connections being open for long

That's true, but it kind of misses the point. Normally, if a connection is dropped it means something has gone wrong. In this case, connections are dropped by design, and there is no way (AFAICT) to work around this. Designing so that behavior that is otherwise the result of things going wrong is now the normal designed-for behavior is, IMHO, the very definition of Bad Design.

Re:Nginx with dynamic upstreams by ishigoemon on Sunday July 26, 2015 @05:15PM (#9952220)
I'd argue that this design promotes building reliable applications. A system that cannot reconnect is fragile, and the best way to know if the system can handle that failure is to occasionally induce the event. Assuming that you are running across a lossless, ideal network is, IMHO, the very definition of Bad Design.

"If it hurts, do it more often." -- Martin Fowler

Re:Nginx with dynamic upstreams by lisper on Sunday July 26, 2015 @07:02PM (#9952513)
Certainly systems should be designed to be robust against failures. But encouraging this by deliberately producing failures in production seems like a bad idea to me. It's kind of like saying, "Let's see if the new hull design works by deliberately steering the boat into an iceberg!"
Re:Nginx with dynamic upstreams by ishigoemon on Monday July 27, 2015 @12:19PM (#9956102)
A TCP socket teardown followed by a reconnect is hardly the equivalent of ramming a floating chunk of ice. There are a bunch of reasons you will see that teardown in practice, like NAT timeouts in a home router, or carrier-grade 6to4 NAT, or mobile devices rehoming to a new tower, or anywhere else that state is tied to the path.

Sure this is a deliberately produced failure, but only in the sense that this is a "normal" failure. This is a condition that is to be expected on the internet, and this is simply an additional place it occurs.

Re:Nginx with dynamic upstreams by kelnos on Monday July 27, 2015 @12:11AM (#9953290)
Bad analogy. It's like saying "let's see if the new hull design works by deliberately running it into things in a test laboratory setting". Because, y'know, if you deploy an application to production using a particular network configuration (that is, using an ELB) without testing it in a development/staging environment first, you're doing a poor job.

This disconnect behavior is just a property of the system. Either you design your application to handle it, or you use a different system. (Not that you can get away with not handling disconnects even without ELBs.)

Re:Nginx with dynamic upstreams by bmurphy1976 on Sunday July 26, 2015 @04:29PM (#9952116)
My analysis shows an AWS ELB changes ip addresses on us roughly every 2 weeks. Often enough to cause problems if you aren't prepared but infrequent enough to give you a false confidence that things are working as designed.
Re:Nginx with dynamic upstreams by jpgvm on Sunday July 26, 2015 @02:56PM (#9951870)
You can hold a TCP connection open through an ELB basically as long as you want. The default idle timeout is 60s but can be increased to 1hr, this is a non-factor if you are sending any sort of data though.

When the "routing rug" is pulled out from under you all you need to do is re-resolve and re-establish the TCP connection which will likely live on for days (in most cases weeks) without disconnecting again.

This is fine for most use cases I am aware of.

As for Websockets, you will need to run the ELB in TCP mode to do that and probably run a real HTTP proxy behind it that supports Websockets/UPGRADE and uses constant source-ip hashing and supports the TCP PROXY protocol. i.e HAProxy. You can run HAProxy or other any other proxy that matches the above in an ELB to get good highly available Websockets proxy layer.

Re:Nginx with dynamic upstreams by Tenzer on Sunday July 26, 2015 @03:01PM (#9951882)
You can also use Nginx for the PROXY protocol, that was added in version 1.5.12. You should just add "proxy_protocol" to your "listen" directive: http://nginx.org/en/docs/http/ngx_http_core_module.html#list....
Re:Nginx with dynamic upstreams by jpgvm on Sunday July 26, 2015 @03:07PM (#9951901)
Cool, does it allow you to control the balancing to enforce source-ip -> backend mapping?

This is required due to the nature of Websockets UPGRADE and most semi-stateful Websockets servers.

Re:Nginx with dynamic upstreams by Tenzer on Sunday July 26, 2015 @03:10PM (#9951907)
Sure, that's not really related to the protocol used. It's instead handled by the upstream module and the "ip_hash" flag: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#....
Re:Nginx with dynamic upstreams by jon-wood on Sunday July 26, 2015 @01:53PM (#9951695)
I find it particularly grating that the official way to deal with Nginx ignoring the advertised TTL is to pay $1500 per year, per server, for an Nginx Plus license.
Re:Nginx with dynamic upstreams by MichaelGG on Sunday July 26, 2015 @05:13PM (#9952216)
Yes. Some of the nginx plus features are very small, and there's even 3rd party modules that add such features. This particular case seems like an outright bug, with a hack workaround. Would they take a patch to fix it?

It makes me think that they must be intentionally omitting features in order to make plus valuable. That seems rather cheesy. Obviously it's their code and right, but I think it shows how hard it is to profit off an open source program.

Re:Nginx with dynamic upstreams by Tenzer on Sunday July 26, 2015 @02:14PM (#9951752)
I think the reasoning for it is for performance. If you can make all the DNS queries you need before you start serving any requests, then you don't have to wait for DNS servers while clients hammer your server.

Ideally I would have like it to be an option though, instead of it basically having become a feature in Nginx Plus - if it wasn't for the way I described in the post.

Re:Nginx with dynamic upstreams by click170 on Sunday July 26, 2015 @02:26PM (#9951786)
I think there's room for interpretation but that in cases like this, it is indeed a bug.

Many applications are programmed this way but I firmly believe that an application needs to honor the TTL of a DNS request for any subsequent connections, for exactly this reason. DNS records change. Sometimes frequently. IMO you shouldn't need yo kick your apps to get them to use the new hostname for subsequent connections.

Re:Nginx with dynamic upstreams by justincormack on Sunday July 26, 2015 @05:27PM (#9952249)
It should just schedule a DNS update before the previous one has expired, and if the addresses have changed shift over, so it should not cause any delays.
Re:Nginx with dynamic upstreams by Tenzer on Monday July 27, 2015 @12:13AM (#9953298)
I would expect that to be the way the Nginx Plus implementation works, but I have never had a chance to try it out.
Re:Nginx with dynamic upstreams by kelnos on Monday July 27, 2015 @12:12AM (#9953294)
That was my first thought as well. It's a bit preposterous that a) there's an "unbreak this software" option at all, and b) you have to pay for the privilege to enable it.