diff --git a/README-zh-Hant.md b/README-zh-Hant.md index c5dd56d3..b9ea2b3b 100755 --- a/README-zh-Hant.md +++ b/README-zh-Hant.md @@ -339,267 +339,269 @@ ![Imgur](http://i.imgur.com/jj3A5N8.png) -## Object-oriented design interview questions with solutions +## 面向对象设计面试问题及解答 -> Common object-oriented design interview questions with sample discussions, code, and diagrams. +> 常见面向对象设计面试问题及实例讨论,代码和图表演示。 > -> Solutions linked to content in the `solutions/` folder. +> 与内容相关的解决方案在 `solutions/` 文件夹中。 ->**Note: This section is under development** +>**注:此节还在完善中** -| Question | | -| -------------------------------------- | ---------------------------------------- | -| Design a hash map | [Solution](solutions/object_oriented_design/hash_table/hash_map.ipynb) | -| Design a least recently used cache | [Solution](solutions/object_oriented_design/lru_cache/lru_cache.ipynb) | -| Design a call center | [Solution](solutions/object_oriented_design/call_center/call_center.ipynb) | -| Design a deck of cards | [Solution](solutions/object_oriented_design/deck_of_cards/deck_of_cards.ipynb) | -| Design a parking lot | [Solution](solutions/object_oriented_design/parking_lot/parking_lot.ipynb) | -| Design a chat server | [Solution](solutions/object_oriented_design/online_chat/online_chat.ipynb) | -| Design a circular array | [Contribute](#contributing) | -| Add an object-oriented design question | [Contribute](#contributing) | +| 问题 | | +|---|---| +| 设计 hash map | [解决方案](solutions/object_oriented_design/hash_table/hash_map.ipynb) | +| 设计 LRU 缓存 | [解决方案](solutions/object_oriented_design/lru_cache/lru_cache.ipynb) | +| 设计一个呼叫中心 | [解决方案](solutions/object_oriented_design/call_center/call_center.ipynb) | +| 设计一副牌 | [解决方案](solutions/object_oriented_design/deck_of_cards/deck_of_cards.ipynb) | +| 设计一个停车场 | [解决方案](solutions/object_oriented_design/parking_lot/parking_lot.ipynb) | +| 设计一个聊天服务 | [解决方案](solutions/object_oriented_design/online_chat/online_chat.ipynb) | +| 设计一个环形数组 | [待解决](#contributing) | +| 添加一个面向对象设计问题 | [待解决](#contributing) | -## System design topics: start here +## 系统设计主题:从这里开始 -New to system design? +不熟悉系统设计? -First, you'll need a basic understanding of common principles, learning about what they are, how they are used, and their pros and cons. +首先,你需要对一般性原则有一个基本的认识,知道它们是什么,怎样使用以及利弊。 -### Step 1: Review the scalability video lecture +### 第一步:回顾可扩展性(scalability)的视频讲座 -[Scalability Lecture at Harvard](https://www.youtube.com/watch?v=-W9F__D3oY4) +[哈佛大学可扩展性讲座](https://www.youtube.com/watch?v=-W9F__D3oY4) -* Topics covered: - * Vertical scaling - * Horizontal scaling - * Caching - * Load balancing - * Database replication - * Database partitioning +* 主题涵盖 + * 垂直扩展(Vertical scaling) + * 水平扩展(Horizontal scaling) + * 缓存 + * 负载均衡 + * 数据库复制 + * 数据库分区 -### Step 2: Review the scalability article +### 第二步:回顾可扩展性文章 -[Scalability](http://www.lecloud.net/tagged/scalability) +[可扩展性](http://www.lecloud.net/tagged/scalability) -* Topics covered: +* 主题涵盖: * [Clones](http://www.lecloud.net/post/7295452622/scalability-for-dummies-part-1-clones) - * [Databases](http://www.lecloud.net/post/7994751381/scalability-for-dummies-part-2-database) - * [Caches](http://www.lecloud.net/post/9246290032/scalability-for-dummies-part-3-cache) - * [Asynchronism](http://www.lecloud.net/post/9699762917/scalability-for-dummies-part-4-asynchronism) + * [数据库](http://www.lecloud.net/post/7994751381/scalability-for-dummies-part-2-database) + * [缓存](http://www.lecloud.net/post/9246290032/scalability-for-dummies-part-3-cache) + * [异步](http://www.lecloud.net/post/9699762917/scalability-for-dummies-part-4-asynchronism) -### Next steps +### 接下来的步骤 -Next, we'll look at high-level trade-offs: +接下来,我们将看看高阶的权衡和取舍: -* **Performance** vs **scalability** -* **Latency** vs **throughput** -* **Availability** vs **consistency** +* **性能**与**可扩展性** +* **延迟**与**吞吐量** +* **可用性**与**一致性** -Keep in mind that **everything is a trade-off**. +记住**每个方面都面临取舍和权衡**。 -Then we'll dive into more specific topics such as DNS, CDNs, and load balancers. +然后,我们将深入更具体的主题,如 DNS,CDN 和负载均衡器。 -## Performance vs scalability +## 性能与可扩展性 -A service is **scalable** if it results in increased **performance** in a manner proportional to resources added. Generally, increasing performance means serving more units of work, but it can also be to handle larger units of work, such as when datasets grow.1 +如果服务**性能**的增长与资源的增加是成比例的,服务就是可扩展的。通常,提高性能意味着服务于更多的工作单元,另一方面,当数据集增长时,同样也可以处理更大的工作单位。1 -Another way to look at performance vs scalability: +另一个角度来看待性能与可扩展性: -* If you have a **performance** problem, your system is slow for a single user. -* If you have a **scalability** problem, your system is fast for a single user but slow under heavy load. +* 如果你的系统有**性能**问题,对于单个用户来说是缓慢的。 +* 如果你的系统有**可扩展性**问题,单个用户较快但在高负载下会变慢。 -### Source(s) and further reading +### 来源及延伸阅读 -* [A word on scalability](http://www.allthingsdistributed.com/2006/03/a_word_on_scalability.html) -* [Scalability, availability, stability, patterns](http://www.slideshare.net/jboner/scalability-availability-stability-patterns/) +* [简单谈谈可扩展性](http://www.allthingsdistributed.com/2006/03/a_word_on_scalability.html) +* [可扩展性,可用性,稳定性和模式](http://www.slideshare.net/jboner/scalability-availability-stability-patterns/) -## Latency vs throughput +## 延迟与吞吐量 -**Latency** is the time to perform some action or to produce some result. +**延迟**是执行操作或运算结果所花费的时间。 -**Throughput** is the number of such actions or results per unit of time. +**吞吐量**是单位时间内(执行)此类操作或运算的数量。 -Generally, you should aim for **maximal throughput** with **acceptable latency**. +通常,你应该以**可接受级延迟**下**最大化吞吐量**为目标。 -### Source(s) and further reading +### 来源及延伸阅读 -* [Understanding latency vs throughput](https://community.cadence.com/cadence_blogs_8/b/sd/archive/2010/09/13/understanding-latency-vs-throughput) +* [理解延迟与吞吐量](https://community.cadence.com/cadence_blogs_8/b/sd/archive/2010/09/13/understanding-latency-vs-throughput) -## Availability vs consistency +## 可用性与一致性 -### CAP theorem +### CAP 理论
- Source: CAP theorem revisited
+ 来源:再看 CAP 理论
- Source: DNS security presentation
+ 来源:DNS 安全介绍
- Source: Why use a CDN
+ 来源:为什么使用 CDN
@@ -607,67 +609,66 @@ Sites with heavy traffic work well with pull CDNs, as traffic is spread out more Source: Scalable system design patterns
-Load balancers distribute incoming client requests to computing resources such as application servers and databases. In each case, the load balancer returns the response from the computing resource to the appropriate client. Load balancers are effective at: +负载均衡器将传入的请求分发到应用服务器和数据库等计算资源。无论哪种情况,负载均衡器将从计算资源来的响应返回给恰当的客户端。负载均衡器的效用在于: -* Preventing requests from going to unhealthy servers -* Preventing overloading resources -* Helping eliminate single points of failure +* 防止请求进入不好的服务器 +* 防止资源过载 +* 帮助消除单一的故障点 -Load balancers can be implemented with hardware (expensive) or with software such as HAProxy. +负载均衡器可以通过硬件(昂贵)或 HAProxy 等软件来实现。 +增加的好处包括: -Additional benefits include: +* **SSL 终结** ─ 解密传入的请求并加密服务器响应,这样的话后端服务器就不必再执行这些潜在高消耗运算了。 + * 不需要再每台服务器上安装 [X.509 证书](https://en.wikipedia.org/wiki/X.509)。 +* **Session 留存** ─ 如果 Web 应用程序不追踪会话,发出 cookie 并将特定客户端的请求路由到同一实例。 -* **SSL termination** - Decrypt incoming requests and encrypt server responses so backend servers do not have to perform these potentially expensive operations - * Removes the need to install [X.509 certificates](https://en.wikipedia.org/wiki/X.509) on each server -* **Session persistence** - Issue cookies and route a specific client's requests to same instance if the web apps do not keep track of sessions +通常会设置采用[工作─备用](#active-passive) 或 [双工作](#active-active) 模式的多个负载均衡器,以免发生故障。 -To protect against failures, it's common to set up multiple load balancers, either in [active-passive](#active-passive) or [active-active](#active-active) mode. +负载均衡器能基于多种方式来路由流量: -Load balancers can route traffic based on various metrics, including: +* 随机 +* 最少负载 +* Session/cookie +* [轮询调度或加权轮询调度算法](http://g33kinfo.com/info/archives/2657) +* [四层负载均衡](#layer-4-load-balancing) +* [七层负载均衡](#layer-7-load-balancing) -* Random -* Least loaded -* Session/cookies -* [Round robin or weighted round robin](http://g33kinfo.com/info/archives/2657) -* [Layer 4](#layer-4-load-balancing) -* [Layer 7](#layer-7-load-balancing) +### 四层负载均衡 -### Layer 4 load balancing +四层负载均衡根据监看[传输层](#communication)的信息来决定如何分发请求。通常,这会涉及来源,目标 IP 地址和请求头中的端口,但不包括数据包(报文)内容。四层负载均衡执行[网络地址转换(NAT)](https://www.nginx.com/resources/glossary/layer-4-load-balancing/)来向上游服务器转发网络数据包。 -Layer 4 load balancers look at info at the [transport layer](#communication) to decide how to distribute requests. Generally, this involves the source, destination IP addresses, and ports in the header, but not the contents of the packet. Layer 4 load balancers forward network packets to and from the upstream server, performing [Network Address Translation (NAT)](https://www.nginx.com/resources/glossary/layer-4-load-balancing/). +### 七层负载均衡器 -### layer 7 load balancing +七层负载均衡器根据监控[应用层](#communication)来决定怎样分发请求。这会涉及请求头的内容,消息和 cookie。七层负载均衡器终结网络流量,读取消息,做出负载均衡判定,然后传送给特定服务器。比如,一个七层负载均衡器能直接将视频流量连接到托管视频的服务器,同时将更敏感的用户账单流量引导到安全性更强的服务器。 -Layer 7 load balancers look at the [application layer](#communication) to decide how to distribute requests. This can involve contents of the header, message, and cookies. Layer 7 load balancers terminates network traffic, reads the message, makes a load-balancing decision, then opens a connection to the selected server. For example, a layer 7 load balancer can direct video traffic to servers that host videos while directing more sensitive user billing traffic to security-hardened servers. +以损失灵活性为代价,四层负载均衡比七层负载均衡花费更少时间和计算资源,虽然这对现代商用硬件的性能影响甚微。 -At the cost of flexibility, layer 4 load balancing requires less time and computing resources than Layer 7, although the performance impact can be minimal on modern commodity hardware. +### 水平扩展 -### Horizontal scaling +负载均衡器还能帮助水平扩展,提高性能和可用性。使用商业硬件的性价比更高,并且比在单台硬件上**垂直扩展**更贵的硬件具有更高的可用性。相比招聘特定企业系统人才,招聘商业硬件方面的人才更加容易。 -Load balancers can also help with horizontal scaling, improving performance and availability. Scaling out using commodity machines is more cost efficient and results in higher availability than scaling up a single server on more expensive hardware, called **Vertical Scaling**. It is also easier to hire for talent working on commodity hardware than it is for specialized enterprise systems. +#### 缺陷:水平扩展 -#### Disadvantage(s): horizontal scaling +* 水平扩展引入了复杂度并涉及服务器复制 + * 服务器应该是无状态的:它们也不该包含像 session 或资料图片等与用户关联的数据。 + * session 可以集中存储在数据库或持久化[缓存](#cache)(Redis, Memcached)的数据存储区中。 +* 缓存和数据库等下游服务器需要随着上游服务器进行扩展,以处理更多的并发连接。 -* Scaling horizontally introduces complexity and involves cloning servers - * Servers should be stateless: they should not contain any user-related data like sessions or profile pictures - * Sessions can be stored in a centralized data store such as a [database](#database) (SQL, NoSQL) or a persistent [cache](#cache) (Redis, Memcached) -* Downstream servers such as caches and databases need to handle more simultaneous connections as upstream servers scale out +### 缺陷:负载均衡器 -### Disadvantage(s): load balancer +* 如果没有足够的资源配置或配置错误,负载均衡器会变成一个性能瓶颈。 +* 引入负载均衡器以帮助消除单点故障但导致了额外的复杂性。 +* 单个负载均衡器会导致单点故障,但配置多个负载均衡器会进一步增加复杂性。 -* The load balancer can become a performance bottleneck if it does not have enough resources or if it is not configured properly. -* Introducing a load balancer to help eliminate single points of failure results in increased complexity. -* A single load balancer is a single point of failure, configuring multiple load balancers further increases complexity. +### 来源及延伸阅读 -### Source(s) and further reading - -* [NGINX architecture](https://www.nginx.com/blog/inside-nginx-how-we-designed-for-performance-scale/) -* [HAProxy architecture guide](http://www.haproxy.org/download/1.2/doc/architecture.txt) -* [Scalability](http://www.lecloud.net/post/7295452622/scalability-for-dummies-part-1-clones) +* [NGINX 架构](https://www.nginx.com/blog/inside-nginx-how-we-designed-for-performance-scale/) +* [HAProxy 架构指南](http://www.haproxy.org/download/1.2/doc/architecture.txt) +* [可扩展性](http://www.lecloud.net/post/7295452622/scalability-for-dummies-part-1-clones) * [Wikipedia](https://en.wikipedia.org/wiki/Load_balancing_(computing)) -* [Layer 4 load balancing](https://www.nginx.com/resources/glossary/layer-4-load-balancing/) -* [Layer 7 load balancing](https://www.nginx.com/resources/glossary/layer-7-load-balancing/) -* [ELB listener config](http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-listener-config.html) +* [四层负载平衡](https://www.nginx.com/resources/glossary/layer-4-load-balancing/) +* [七层负载平衡](https://www.nginx.com/resources/glossary/layer-7-load-balancing/) +* [ELB 监听器配置](http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-listener-config.html) ## Reverse proxy (web server) @@ -1260,7 +1261,7 @@ def set_user(user_id, values): - [从缓存到内存数据](http://www.slideshare.net/tmatyashovsky/from-cache-to-in-memory-data-grid-introduction-to-hazelcast) - [可扩展系统设计模式](http://horicky.blogspot.com/2010/10/scalable-system-design-patterns.html) - [大型系统架构介绍](http://lethain.com/introduction-to-architecting-systems-for-scale/) -- [可扩展性,可用性,稳定性,模式](http://www.slideshare.net/jboner/scalability-availability-stability-patterns/) +- [可扩展性,可用性,稳定性和模式](http://www.slideshare.net/jboner/scalability-availability-stability-patterns/) - [可扩展性](http://www.lecloud.net/post/9246290032/scalability-for-dummies-part-3-cache) - [AWS ElastiCache 策略](http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/Strategies.html) - [维基百科](https://en.wikipedia.org/wiki/Cache_(computing))