| Now that we have theC10K concurrent connection problem licked, how do we level up and support 10 million concurrent connections? Impossible you say. Nope, systems right now are delivering 10 million concurrent connections using techniques that are as radical as they may be unfamiliar. To learn how it’s done we turn toRobert Graham, CEO of Errata Security, and his absolutely fantastic talk atShmoocon 2013 calledC10M Defending The Internet At Scale. Robert has a brilliant way of framing the problem that I’ve never heard of before. He starts with a little bit of history, relating how Unix wasn’t originally designed to be a general server OS, it was designed to be a control system for a telephone network. It was the telephone network that actually transported the data so there was a clean separation between the control plane and the data plane. Theproblem is we now use Unix servers as part of the data plane, which we shouldn’t do at all. If we were designing a kernel for handling one application per server we would design it very differently than for a multi-user kernel. |
我们现在已经搞定了C10K并发连接问题,升级一下,如何支持千万级的并发连接?你可能说,这不可能。你说错了,现在的系统可以支持千万级的并发连接,只不过所使用的那些激进的技术,并不为人所熟悉。 要了解这是如何做到的,我们得求助于Errata Security的CEORobert Graham,看一下他在Shmoocon 2013的绝对奇思妙想的演讲,题目是C10M Defending The Internet At Scale。 Robert以一种我以前从来没有听说过的才华横溢的方式来搭建处理这个问题的架构。他的开场是一些历史,关于Unix最初为什么不是设计成一个通用的服务器的OS,而是为电话网络的控制系统设计的。真正传输数据的是电话网络,因而控制层和数据层有非常清晰的区分。问题是,我们现在用的Unix服务器还是数据层的一部分,虽然并不应当是这样的。如果一台服务器只有一个应用程序,为这样的系统设计内核,与设计一个多用户系统的内核的区别是非常大的。 |
| Which is why he says the key is to understand: · The kernel isn’t the solution.The kernel is the problem. Which means: · Don’t let the kernel do all the heavy lifting. Take packet handling, memory management, and processor scheduling out of the kernel and put it into the application, where it can be done efficiently. Let Linux handle the control plane and let the the application handle the data plane. The result will be a system that can handle 10 million concurrent connections with 200 clock cycles for packet handling and 1400 hundred clock cycles for application logic. As a main memory access costs 300 clock cycles it’s key to design in way that minimizes code and cache misses. With adata plane oriented system you can process 10 million packets per second. With a control plane oriented system you only get 1 million packets per second. If this seems extreme keep in mind the old saying:scalability is specialization. To do something great you can’t outsource performance to the OS. You have to do it yourself. Now, let’s learn how Robert creates a system capable of handling 10 million concurrent connections... | 这也是为什么他说重要的是要理解:
这意味着:
结果就是成为一个用200个时钟周期处理数据包,14万个时钟周期来处理应用程序逻辑,可以处理1000万并发连接的系统。而作为重要的内存访问花费300个时钟周期,这是尽可能减少编码和缓存的设计方法的关键。 用一个面向数据层的系统你可以每秒处理1000万个数据包。用一个面向控制层的系统每秒你只能获得1百万个数据包。 如果这貌似有点极端,记住一句老话:可扩展性是专业化。要做些牛X的事儿,你不能局限于操作系的性能。你必须自己去做。 现在,让我们学习Robert是怎样创作一个能处理1000万并发连接的系统…… |
| C10K Problem - So Last Decade A decade ago engineers tackled the C10K scalability problems that prevented servers from handling more than 10,000 concurrent connections. This problem was solved by fixing OS kernels and moving away from threaded servers like Apache to event-driven servers like Nginx and Node. This process has taken a decade as people have been moving away from Apache to scalable servers. In the last few years we’ve seen faster adoption of scalable servers. The Apache Problem · The Apache problem is the more connections the worse the performance. · Key insight:performance and scalability or orthogonal concepts. They don’t mean the same thing. When people talk about scale they often are talking about performance, but there’s a difference between scale and performance. As we’ll see with Apache. · With short term connections that last a few seconds, say a quick transaction, if you are executing a 1000 TPS then you’ll only have about a 1000 concurrent connections to the server. · Change the length of the transactions to 10 seconds, now at 1000 TPS you’ll have 10K connections open. Apache’s performance drops off a cliff though which opens you to DoS attacks. Just do a lot of downloads and Apache falls over. · If you are handling 5,000 connections per second and you want to handle 10K, what do you do? Let’s say you upgrade hardware and double it the processor speed. What happens? You get double the performance but you don’t get double the scale. The scale may only go to 6K connections per second. Same thing happens if you keep on doubling. 16x the performance is great but you still haven’t got to 10K connections. Performance is not the same as scalability. · The problem was Apache would fork a CGI process and then kill it. This didn’t scale. · Why? Servers could not handle 10K concurrent connections because of O(n^2) algorithms used in the kernel. o Two basic problems in the kernel: § Connection = thread/process. As a packet came in it would walk down all 10K processes in the kernel to figure out which thread should handle the packet § Connections = select/poll (single thread). Same scalability problem. Each packet had to walk a list of sockets. o Solution: fix the kernel to make lookups in constant time § Threads now constant time context switch regardless of number of threads. § Came with a new scalable epoll()/IOCompletionPort constant time socket lookup. · Thread scheduling still didn’t scale so servers scaled using epoll with sockets which led to the asynchronous programming model embodied in Node and Nginx. This shifted software to a different performance graph. Even with a slower server when you add more connections the performance doesn’t drop off a cliff. At 10K connections a laptop is even faster than a 16 core server. | C10K的问题——过去十年 十年前,工程师在处理C10K可扩展性问题时,都尽可能的避免服务器处理超过10,000个的并发连接。通过修正操作系统内核以及用事件驱动型服务器(如Nginx和Node)替代线程式的服务器(如Apache)这个问题已经解决。从Apache转移到可扩展的服务器上,人们用了十年的时间。在过去的几年中,(我们看到)可扩展服务器的采用率在大幅增长。 Apache的问题
|
| The C10M Problem - The Next Decade In the very near future servers will need to handle millions of concurrent connections. With IPV6 the number of potential connections from each server is in the millions so we need to go to the next level of scalability. · Examples of applications that will need this sort of scalability: IDS/IPS because they connection to a server backbone. Other examples: DNS root server, TOR node, Nmap of Internet, video streaming, banking, Carrier NAT, Voip PBX, load balancer, web cache, firewall, email receive, spam filtering. · Often people who see Internet scale problems are appliances rather than servers because they are selling hardware + software. You buy the device and insert it into your datacenter. These devices may contain an Intel motherboard or Network processors and specialized chips for encryption, packet inspection, etc. · X86 prices on Newegg as of Feb 2013 - $5K for 40gpbs, 32-cores, 256gigs RAM. The servers can do more than 10K connections. If they can’t it’s because you’ve made bad choices with software. It’s not the underlying hardware that’s the issues. This hardware can easily scale to 10 million concurrent connections. | C10M问题 ——下一个十年 在不久的将来,服务器将需要处理数百万的并发连接。由于IPV6普及,连接到每一个服务器的潜在可能连接数目将达到数百万,所以我们需要进入下一个可扩张性阶段。 示例应用程序将会用到这类可扩张性方案:IDS/IPS,因为他们是连接到一台服务器的主干。另一个例子:DNS根服务器、TOR节点、Nmap互联网络、视频流、银行业务、NAT载体、网络语音电话业务PBX、负载均衡器、web缓存、防火墙、邮件接收、垃圾邮件过滤。 通常人们认为互联网规模问题是个人计算机而不是服务器,因为他们销售的是硬件+软件。你买的设备连接到你的数据中心。这些设备可能包含英特尔主板或网络处理器和用于加密的芯片、数据包检测,等等。 2013年2月40gpbs、32核、256gigs RAM X86在新蛋的售价为$5000。这种配置的服务器能够处理10K以上的连接。如果不能,这不是底层的硬件问题,那是因为你选错了软件。这样的硬件能够轻而易举的支持千万的并发连接。 |
| What the 10M Concurrent Connection Challenge means: 1.10 million concurrent connections 2.1 million connections/second - sustained rate at about 10 seconds a connections 3.10 gigabits/second connection - fast connections to the Internet. 4.10 million packets/second - expect current servers to handle 50K packets per second, this is going to a higher level. Servers used to be able to handle 100K interrupts per second and every packet caused interrupts. 5.10 microsecond latency - scalable servers might handle the scale but latency would spike. 6.10 microsecond jitter - limit the maximum latency 7.10 coherent CPU cores - software should scale to larger numbers of cores. Typically software only scales easily to four cores. Servers can scale to many more cores so software needs to be rewritten to support larger core machines. |
10,000,000个并发连接挑战意味着什么 1. 10,000,000个并发连接 2.每秒1,000,000个连接——每个连接大约持续10秒 3. 10千兆比特/每秒——快速连接到互联网。 4. 10,000,000包/每秒——预期当前服务器处理50,000包/每秒,这将导致更高的级别。服务器能够用来处理每秒100,000个中断和每个包引发的中断。 5. 10微秒延迟——可扩张的服务器也许能够处理这样的增长,但是延迟将会很突出。 6. 10微秒上下跳动——限制最大延迟 7. 10个一致的CPU内核——软件应该扩张到更多内核。典型的软件只是简单的扩张到四个内核。服务器能够扩张到更多的内核,所以软件需要被重写以支持在拥有更多内核的机器上运行。 |
| We’ve Learned Unix Not Network Programming · A generation of programmers has learned network programming by reading Unix Networking Programming by W. Richard Stevens. The problem is the book is about Unix, not just network programming. It tells you to let Unix do all the heavy lifting and you just write a small little server on top of Unix. But the kernel doesn’t scale. The solution is to move outside the kernel and do all the heavy lifting yourself. · An example of the impact of this is to consider Apache’s thread per connection model. What this means is the thread scheduler determines which read() to call next depending on which data arrives.You are using the thread scheduling system as the packet scheduling system. (I really like this, never thought of it that way before). · What Nginx says it don’t use thread scheduling as the packet scheduler. Do the packet scheduling yourself. Use select to find the socket, we know it has data so we can read immediately and it won’t block, and then process the data. · Lesson: Let Unix handle the network stack, but you handle everything from that point on. |
我们学的是Unix而不是网络编程(Network Programming)
|
| How do you write software that scales? How do change your software to make it scale? A lot of or rules of thumb are false about how much hardware can handle. We need to know what the performance capabilities actually are. To go to the next level the problems we need to solve are: 1.packet scalability 2.multi-core scalability 3.memory scalability |
你怎么编写软件使其可伸缩? 你怎么改变你的软件使其可伸缩?有大量的经验规则都是假设硬件能处理多少。我们需要真实的执行性能。 要进入下一个等级,我们需要解决的问题是:
|
| Packet Scaling - Write Your Own Custom Driver to Bypass the Stack · The problem with packets is they go through the Unix kernel. The network stack is complicated and slow. The path of packets to your application needs to be more direct. Don’t let the OS handle the packets. · The way to do this is to write your own driver. All the driver does is send the packet to your application instead of through the stack. You can find drivers: PF_RING, Netmap, Intel DPDK (data plane development kit). The Intel is closed source, but there’s a lot of support around it. · How fast? Intel has a benchmark where the process 80 million packets per second (200 clock cycles per packet) on a fairly lightweight server. This is through user mode too. The packet makes its way up through to user mode and then down again to go out. Linux doesn’t do more than a million packets per second when getting UDP packets up to user mode and out again. Performance is 80-1 of a customer driver to a Linux. · For the 10 million packets per second goal if 200 clock cycles are used in getting the packet that leaves 1400 clocks cycles to implement functionally like a DNS/IDS. · With PF_RING you get raw packets so you have to do your TCP stack. People are doing user mode stacks. For Intel there is an available TCP stack that offers really scalable performance. |
精简包-编写自己的定制驱动来绕过堆栈
|
| Multi-Core Scalability Multi-core scalability is not the same thing as multi-threading scalability. We’re all familiar with the idea processors are not getting faster, but we are getting more of them. Most code doesn’t scale past 4 cores. As we add more cores it’s not just that performance levels off, we can get slower and slower as we add more cores. That’s because software is written badly. We want software as we add more cores to scale nearly linearly. Want to get faster as we add more cores. Multi-threading coding is not multi-core coding · Multi-threading: o More than one thread per CPU core o Locks to coordinate threads (done via system calls) o Each thread a different task · Multi-core: o One thread per CPU core o When two threads/cores access the same data they can’t stop and wait for each other o All threads part of the same task · Our problem is how to spread an application across many cores. · Locks in Unix are implemented in the kernel. What happens at 4 cores using locks is that most software starts waiting for other threads to give up a lock. So the kernel starts eating up more performance than you gain from having more CPUs. · What we need is an architecture that is more like a freeway than an intersection controlled by a stop light. We want no waiting where everyone continues at their own pace with as little overhead as possible. · Solutions:
|
多核的可扩展性 多核的可扩展性和多线程可扩展性是不一样的。 我们熟知的idea处理器不在渐渐变快,但是我们却拥有越来越多的idea处理器。 大多数代码并不能扩展到4核。当我们添加更多的核心时并不是性能不变,而是我们添加更多的核心时越来越慢。因为我们编写的代码不好。我们期望软件和核心成线性的关系。我们想要的是添加更多的核心就更快。 多线程编程不是多核编程 · 多线程: o 锁来协调线程(通过系统调用) o 每个线程有不同的任务 · 多核:
|
| Memory Scalability · The problem is if you have 20gigs of RAM and let’s say you use 2k per connection, then if you only have 20meg L3 cache, none of that data will be in cache. It costs 300 clock cycles to go out to main memory, at which time the CPU isn’t doing anything. · Think about this with our 1400 clock cycle budge per packet. Remember 200 clocks/pkt overhead. We only have 4 cache misses per packet and that's a problem. · Co-locate Data o Don’t scribble data all over memory via pointers. Each time you follow a pointer it will be a cache miss: [hash pointer] -> [Task Control Block] -> [Socket] -> [App]. That’s four cache misses. o Keep all the data together in one chunk of memory: [TCB | Socket | App]. Prereserve memory by preallocating all the blocks. This reduces cache misses from 4 to 1. · Paging o The paging table for 32gigs require 64MB of paging tables which doesn’t fit in cache. So you have two caches misses, one for the paging table and one for what it points to. This is detail we can’t ignore for scalable software. o Solutions: compress data; use cache efficient structures instead of binary search tree that has a lot of memory accesses o NUMA architectures double the main memory access time. Memory may not be on a local socket but is on another socket. · Memory pools o Preallocate all memory all at once on startup. o Allocate on a per object, per thread, and per socket basis. · Hyper-threading o Network processors can run up to 4 threads per processor, Intel only has 2. o This masks the latency, for example, from memory accesses because when one thread waits the other goes at full speed. · Hugepages o Reduces page table size. Reserve memory from the start and then your application manages the memory. |
内存的可扩展性
|
| Summary
The control plane is left to Linux, for the data plane, nothing. The data plane runs in application code. It never interacts with the kernel. There’s no thread scheduling, no system calls, no interrupts, nothing. Yet, what you have is code running on Linux that you can debug normally, it’s not some sort of weird hardware system that you need custom engineer for. You get the performance of custom hardware that you would expect for your data plane, but with your familiar programming and development environment. | 总结
(仅)把控制层留给Linux,与数据层毫无瓜葛。由应用程序管理数据层。应用程序与内核间没有交互。没有线程调度,没有系统调用,没有中断,什么都没有。 然而,你拥有的是在Linux上运行的代码,并且可以正常调试,它并不是某些需要特殊定制的怪异的硬件系统。你得到了定制硬件的性能,就像你期待的那样,只是需要用你熟悉的编程(语言)和开发环境。 |

本文探讨了如何通过重新思考网络编程和系统架构实现千万级的并发连接处理能力。文章提出传统Unix内核不再是最佳选择,而应转向自定义驱动、多核优化及内存管理等策略。
8539

被折叠的 条评论
为什么被折叠?



