Redis宣言[转载]

难易 发布于 2012/07/10 11:05
阅读 794
收藏 2

http://blog.dccmx.com/2012/02/excellent-redis-manifesto/

dccmx 于 2012年 二月 7日 发表 | 最后修改于 2012年 二月 7日

前两天瞄了一眼Redis的git log,突然发现一个叫MANIFESTO(宣言)的东西,打开一看,瞬间醍醐灌顶,再看log,去年就有了,这么好的东西躺硬盘上现在才发现,真是罪过,而且还自称看过Redis源码,羞愧啊。

忍不住翻译一下(为了防止读者被我拙劣的翻译误导,必须附上原文):

1 – A DSL for Abstract Data Types. Redis is a DSL (Domain Specific Language) that manipulates abstract data types and implemented as a TCP daemon. Commands manipulate a key space where keys are binary-safe strings and values are different kinds of abstract data types. Every data type represents an abstract version of a fundamental data structure. For instance Redis Lists are an abstract representation of linked lists. In Redis, the essence of a data type isn’t just the kind of operations that the data types support, but also the space and time complexity of the data type and the operations performed upon it.

抽象数据类型DSL。Redis是一个操作抽象数据类型的DSL(领域特定语言),实现方式为一个TCP服务。Redis命令处理一个key空间(这里的原文是“key space”,我觉得是来自“address space”故译作key空间),其中,key是二进制安全的字符串,值是各种各样的抽象数据类型。每种数据类型表示了一种抽象的基础数据结构。比如,Redis中的Lists就表示了链表。在Redis中,数据类型的本质不是仅仅指它支持的操作的集合,还包括其自身和所支持操作的时空复杂度。

2 – Memory storage is #1. The Redis data set, composed of defined key-value pairs, is primarily stored in the computer’s memory. The amount of memory in all kinds of computers, including entry-level servers, is increasing significantly each year. Memory is fast, and allows Redis to have very predictable performance.  Datasets composed of 10k or 40 millions keys will perform similarly.  Complex data types like Redis Sorted Sets are easy to implement and manipulate in memory with good performance, making Redis very simple.  Redis will continue to explore alternative options (where data can be optionally stored on disk, say) but the main goal of the project remains the development of an in-memory database.

内存存储为王。Redis数据集——一堆key-value对,主要存储在内存中。所有电脑包括入门级的服务器的内存都在逐年大幅增长。内存就是快,这使得redis能够获得非常可预期的性能。10k个key的数据和40m个key的数据性能上没什么差别。基于内存,复杂的数据类型,如有序集合,可以很容易并且高性能的实现。这使得Redis显得非常简洁。Redis会继续寻找其他可选方案(比如,提供选项可以让数据保存在磁盘上),但是项目的主要目标还是开发一个内存数据库。

3 – Fundamental data structures for a fundamental API. The Redis API is a direct consequence of fundamental data structures. APIs can often be arbitrary but not an API that resembles the nature of fundamental data structures. If we ever meet intelligent life forms from another part of the universe, they’ll likely know, understand and recognize the same basic data structures we have in our computer science books. Redis will avoid intermediate layers in API, so that the complexity is obvious and more complex operations can be performed as the sum of the basic operations.

基础数据结构对应基础API。Redis的API是其后面基础的数据结构操作的直接体现。API经常会被设计得很随意,但是这些随意的API中没哪个API能反映其背后基础数据结构的本性。即使外星人来了,他们也能够从我们的API中理解出与我们计算机书籍中一样的基本数据结构。Redis会避免API中间层,这样,其复杂性就会显而易见(不会因为中间层的包装而隐藏实际操作的复杂度),更复杂的操作可以用基本的操作组合而成。

4 – Code is like a poem; it’s not just something we write to reach some practical result.  Sometimes people that are far from the Redis philosophy suggest using other  code written by other authors (frequently in other languages) in order to implement something Redis currently lacks. But to us this is like if Shakespeare decided to end Enrico IV using the Paradiso from the Divina Commedia. Is using any external code a bad idea? Not at all. Like in “One Thousand and One Nights” smaller self contained stories are embedded in a bigger story, we’ll be happy to use beautiful self contained libraries when needed. At the same time, when writing the Redis story we’re trying to write smaller stories that will fit in to other code.

代码须如诗一般。这一条不仅仅是为了达到什么实际的效果。有时候人们会违背Redis哲学很远的建议用一些其他人的代码(常常是另外的语言)来实现Redis当前没有的功能。这对于我们来说就像是让莎士比亚决定用但丁《神曲:天堂》来做亨利四世的结尾一样。用外部代码是个馊主意?不尽然。就像《一千零一夜》中,一个个小故事组成了一个大故事,如果需要,我们将很乐意使用一些优美的自包含的库。同时,在写Redis这个“大故事”的时候,我们则是在写一个个更小的相互契合的故事。

5 – We’re against complexity. We believe designing systems is a fight against complexity. We’ll accept to fight the complexity when it’s worthwhile but we’ll try hard to recognize when a small feature is not worth 1000s of lines of code. Most of the time the best way to fight complexity is by not creating it at all.

我们反对复杂。我们相信:系统设计就是在和复杂性做斗争的过程。只要值得,我们就会和复杂性做斗争,但是,如果一个小功能需要几千行代码来实现的时候呢?我们会尽量发现这种情况并认真权衡是否值得。大多数时候,对抗复杂性最好的方法就是:不要制造复杂。

6 – Two levels of API. The Redis API has two levels: 1) a subset of the API fits naturally into a distributed version of Redis and 2) a more complex API that supports multi-key operations. Both are useful if used judiciously but there’s no way to make the more complex multi-keys API distributed in an opaque way without violating our other principles. We don’t want to provide the illusion of something that will work magically when actually it can’t in all cases. Instead we’ll provide commands to quickly migrate keys from one instance to another to perform multi-key operations and expose the tradeoffs to the user.

两层API。Redis的API分两层。1)用于分布式Redis的API子集。2)复杂的支持多键操作的API。如果使用得当,他们都非常有用。但是,没法让多键操作的API在不违反我们原则的前提下不透明的支持分布式。我们不希望造成一些功能看上去很神奇的能工作而实际上并不是所有情况下都能工作的假象。作为替代方案,我们提供在Redis实例间迅速迁移key的命令来完成多键操作,这样如何权衡就看用户了。

7 – We optimize for joy. We believe writing code is a lot of hard work, and the only way it can be worth is by enjoying it.  When there is no longer joy in writing code, the best thing to do is stop. To prevent this, we’ll avoid taking paths that will make Redis less of a joy to develop.

我们以优化为乐。我们相信,码代码非常辛苦,唯一能让我们觉得值得付出这些辛苦的方法就是享受它。如果我们码代码的时候不能感到快乐,最好就此停手。为了防止这种情况发生,我们会避免Redis走上会使开发变得无趣的路。

如读echo “import this” | python一样。神清气爽,非常神清气爽。

鉴于我发现我所尝试的任何注解或者补充说明都显得很虚弱。算了,通通删了。直接多读几遍。有益身心健康。

原文在redis源码包中或者http://antirez.com/post/redis-manifesto.html

加载中
0
i
ibmo
一直都觉得redis 的代码质量一般,建议看看nginx 的源码,它的代码写得相当的漂亮!
返回顶部
顶部