learn-tech/专栏/左耳听风/080程序员练级攻略(2018):数据库.md
2024-10-16 06:37:41 +08:00

261 lines
14 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

因收到Google相关通知网站将会择期关闭。相关通知内容
080 程序员练级攻略2018数据库
对于数据库方向,重点就是两种数据库,一种是以 SQL 为代表的关系型数据库,另一种是以非 SQL 为代表的 NoSQL 数据库。关系型数据库主要有三个Oracle、MySQL 和 Postgres。
在这里,我们只讨论越来越主流的 MySQL 数据库。首先,我们要了解数据库的一些实现原理和内存的一些细节,然后我们要知道数据的高可用和数据复制这些比较重要的话题,了解一下关系型数据库的一些实践和难点。然后,我们会进入到 NoSQL 数据库的学习。
NoSQL 数据库千奇百怪,其主要是解决了关系型数据库中的各种问题。第一个大问题就是数据的 Schema 非常多,用关系型数据库来表示不同的 Data Schema 是非常笨拙的,所以要有不同的数据库(如时序型、键值对型、搜索型、文档型、图结构型等)。另一个大问题是,关系型数据库的 ACID 是一件很讨厌的事,这极大地影响了数据库的性能和扩展性,所以 NoSQL 在这上面做了相应的妥协以解决大规模伸缩的问题。
对于一个程序员,你可能觉得数据库的事都是 DBA 的事,然而我想告诉你你错了,这些事才真正是程序员的事。因为程序是需要和数据打交道的,所以程序员或架构师不仅需要设计数据模型,还要保证整体系统的稳定性和可用性,数据是整个系统中关键中的关键。所以,作为一个架构师或程序员,你必须了解最重要的数据存储——数据库。
关系型数据库
今天,关系型数据库最主要的两个代表是闭源的 Oracle 和开源的 MySQL。当然还有很多了比如微软的 SQL ServerIBM 的 DB2 等,还有开源的 PostgreSQL。关系型数据库的世界中有好多好多产品。当然还是 Oracle 和 MySQL 是比较主流的。所以,这里主要介绍更为开放和主流的 MySQL。
如果你要玩 Oracle我这里只推荐一本书《Oracle Database 9i/10g/11g 编程艺术》,无论是开发人员还是 DBA它都是必读的书。这本书的作者是 Oracle 公司的技术副总裁托马斯·凯特Thomas Kyte他也是世界顶级的 Oracle 专家。
这本书中深入分析了 Oracle 数据库体系结构,包括文件、内存结构以及构成 Oracle 数据库和实例的底层进程,利用具体示例讨论了一些重要的数据库主题,如锁定、并发控制、事务等。同时分析了数据库中的物理结构,如表、索引和数据类型,并介绍采用哪些技术能最优地使用这些物理结构。
学习 MySQL首先一定是要看MySQL 官方手册。
然后,官方还有几个 PPT 也要学习一下。
How to Analyze and Tune MySQL Queries for Better Performance
MySQL Performance Tuning 101
MySQL Performance Schema & Sys Schema
MySQL Performance: Demystified Tuning & Best Practices
MySQL Security Best Practices
MySQL Cluster Deployment Best Practices
MySQL High Availability with InnoDB Cluster
然后推荐《高性能 MySQL》这本书是 MySQL 领域的经典之作拥有广泛的影响力。不但适合数据库管理员DBA阅读也适合开发人员参考学习。不管是数据库新手还是专家都能从本书中有所收获。
如果你对 MySQL 的内部原理有兴趣的话可以看一下这本书《MySQL 技术内幕InnoDB 存储引擎》。当然还有官网的MySQL Internals Manual 。
数据库的索引设计和优化也是非常关键的,这里还有一本书《数据库的索引设计与优化》也是很不错的。虽然不是讲 MySQL 的,但是原理都是相通的。这也是上面推荐过的《高性能 MySQL》在其索引部分推荐的一本好书。
你千万不要觉得只有做数据库你才需要学习这种索引技术。不是的!在系统架构上,在分布式架构中,索引技术也是非常重要的。这本书对于索引性能进行了非常清楚的估算,不像其它书中只是模糊的描述,你一定会收获很多。
下面还有一些不错的和 MySQL 相关的文章。
MySQL 索引背后的数据结构及算法原理
Some study on database storage internals
Sharding Pinterest: How we scaled our MySQL fleet
Guide to MySQL High Availability
Choosing MySQL High Availability Solutions
High availability with MariaDB TX: The definitive guide
最后,还有一个 MySQL 的资源列表 Awesome MySQL这个列表中有很多的工具和开发资源可以帮助你做很多事。
MySQL 有两个比较有名的分支,一个是 Percona另一个是 MariaDB其官网上的 Resources 页面中有很多不错的资源和文档,可以经常看看。 Percona Resources、MariaDB Resources ,以及它们的开发博客中也有很多不错的文章,分别为 Percona Blog 和 MariaDB Blog。
然后是关于 MySQL 的一些相关经验型的文章。
Booking.com: Evolution of MySQL System Design Booking.com 的 MySQL 数据库使用的演化,其中有很多不错的经验分享,我相信也是很多公司会遇到的的问题。
Tracking the Money-Scaling Financial Reporting at Airbnb Airbnb 的数据库扩展的经验分享。
Why Uber Engineering Switched from Postgres to MySQL ,无意比较两个数据库谁好谁不好,推荐这篇 Uber 的长文,主要是想让你从中学习到一些经验和技术细节,这是一篇很不错的文章。
关于 MySQL 的集群复制,下面有这些文章供你学习一下,都是很不错的实践性比较强的文章。
Monitoring Delayed Replication, With A Focus On MySQL
Mitigating replication lag and reducing read load with freno
另外Booking.com 给了一系列的文章,你可以看看:
Better Parallel Replication for MySQL
Evaluating MySQL Parallel Replication Part 2: Slave Group Commit
Evaluating MySQL Parallel Replication Part 3: Benchmarks in Production
Evaluating MySQL Parallel Replication Part 4: More Benchmarks in Production
Evaluating MySQL Parallel Replication Part 4, Annex: Under the Hood
对于 MySQL 的数据分区来说,还有下面几篇文章你可以看看。
StackOverflow: MySQL sharding approaches?
Why you dont want to shard
How to Scale Big Data Applications
MySQL Sharding with ProxySQL
然后,再看看各个公司做 MySQL Sharding 的一些经验分享。
MailChimp: Using Shards to Accommodate Millions of Users
Uber: Code Migration in Production: Rewriting the Sharding Layer of Ubers Schemaless Datastore
Sharding & IDs at Instagram
Airbnb: How We Partitioned Airbnbs Main Database in Two Weeks
NoSQL 数据库
关于 NoSQL 数据库,其最初目的就是解决大数据的问题。然而,也有人把其直接用来替换掉关系型数据库。所以在学习这个技术之前,我们需要对这个技术的一些概念和初衷有一定的了解。下面是一些推荐资料。
Martin Fowler 在 YouTube 上分享的 NoSQL 介绍 Introduction To NoSQL 以及他参与编写的 NoSQL Distilled - NoSQL 精粹,这本书才 100 多页,是本难得的关于 NoSQL 的书,很不错,非常易读。
NoSQL Databases: a Survey and Decision Guidance这篇文章可以带你自上而下地从 CAP 原理到开始了解 NoSQL 的种种技术,是一篇非常不错的文章。
Distribution, Data, Deployment: Software Architecture Convergence in Big Data Systems这是卡内基·梅隆大学的一篇讲分布式大数据系统的论文。其中主要讨论了在大数据时代下的软件工程中的一些关键点也说到了 NoSQL 数据库。
No Relation: The Mixed Blessings of Non-Relational Databases这篇论文虽然有点年代久远。但这篇论文是 HBase 的基础,你花上一点时间来读读,就可以了解到,对各种非关系型数据存储优缺点的一个很好的比较。
NoSQL Data Modeling Techniques NoSQL 建模技术。这篇文章我曾经翻译在了 CoolShell 上,标题为 NoSQL 数据建模技术,供你参考。
MongoDB - Data Modeling Introduction ,虽然这是 MongoDB 的数据建模介绍,但是其很多观点可以用于其它的 NoSQL 数据库。
Firebase - Structure Your Database Google 的 Firebase 数据库使用 JSON 建模的一些最佳实践。
因为 CAP 原理,所以当你需要选择一个 NoSQL 数据库的时候,你应该看看这篇文档 Visual Guide to NoSQL Systems。
选 SQL 还是 NoSQL这里有两篇文章值得你看看。
SQL vs. NoSQL Databases: Whats the Difference?
Salesforce: SQL or NoSQL
各种 NoSQL 数据库
学习使用 NoSQL 数据库其实并不是一件很难的事,只要你把官方的文档仔细地读一下,是很容易上手的,而且大多数 NoSQL 数据库都是开源的,所以,也可以通过代码自己解决问题。下面我主要给出一些典型的 NoSQL 数据库的一些经验型的文章,供你参考。
列数据库 Column Database
Cassandra 相关
沃尔玛实验室有两篇文章值得一读。
Avoid Pitfalls in Scaling Cassandra Cluster at Walmart
Storing Images in Cassandra at Walmart
Yelp: How We Scaled Our Ad Analytics with Apache Cassandra Yelp 的这篇博客也有一些相关的经验和教训。
Discord: How Discord Stores Billions of Messages Discord 公司分享的一个如何存储十亿级消息的技术文章。
Cassandra at Instagram Instagram 的一个 PPT其中介绍了 Instagram 中是怎么使用 Cassandra 的。
Netflix: Benchmarking Cassandra Scalability on AWS-Over a million writes per second Netflix 公司在 AWS 上给 Cassandra 做的一个 Benchmark。
HBase 相关
Imgur Notification: From MySQL to HBASE
Pinterest: Improving HBase Backup Efficiency
IBM : Tuning HBase performance
HBase File Locality in HDFS
Apache Hadoop Goes Realtime at Facebook
Storage Infrastructure Behind Facebook Messages: Using HBase at Scale
GitHub: Awesome HBase
针对于 HBase 有两本书你可以考虑一下。
首先先推荐两本书一本是偏实践的《HBase 实战》另一本是偏大而全的手册型的《HBase 权威指南》。
当然,你也可以看看官方的 The Apache HBase™ Reference Guide
另外两个列数据库:
ClickHouse - Open Source Distributed Column Database at Yandex
Scaling Redshift without Scaling Costs at GIPHY
文档数据库 Document Database - MongoDB, SimpleDB, CouchDB
Data Points - What the Heck Are Document Databases?
eBay: Building Mission-Critical Multi-Data Center Applications with MongoDB
The AWS and MongoDB Infrastructure of Parse: Lessons Learned
Migrating Mountains of Mongo Data
Couchbase Ecosystem at LinkedIn
SimpleDB at Zendesk
Github: Awesome MongoDB
数据结构数据库 Data structure Database - Redis
Learn Redis the hard way (in production) at Trivago
Twitter: How Twitter Uses Redis To Scale - 105TB RAM, 39MM QPS, 10,000+ Instances
Slack: Scaling Slacks Job Queue - Robustly Handling Billions of Tasks in Milliseconds Using Kafka and Redis
GitHub: Moving persistent data out of Redis at GitHub
Instagram: Storing Hundreds of Millions of Simple Key-Value Pairs in Redis
Redis in Chat Architecture of Twitch (from 27:22)
Deliveroo: Optimizing Session Key Storage in Redis
Deliveroo: Optimizing Redis Storage
GitHub: Awesome Redis
时序数据库 Time-Series Database
What is Time-Series Data & Why We Need a Time-Series Database
Time Series Data: Why and How to Use a Relational Database instead of NoSQL
Beringei: High-performance Time Series Storage Engine @Facebook
Introducing Atlas: Netflixs Primary Telemetry Platform @Netflix
Building a Scalable Time Series Database on PostgreSQL
Scaling Time Series Data Storage - Part I @Netflix
Design of a Cost Efficient Time Series Store for Big Data
GitHub: Awesome Time-Series Database
图数据库 - Graph Platform
首先是 IBM Devloperworks 上的两个简介性的 PPT。
Intro to graph databases, Part 1, Graph databases and the CRUD operations
Intro to graph databases, Part 2, Building a recommendation engine with a graph database
然后是一本免费的电子书《Graph Database》。
接下来是一些图数据库的介绍文章。
Handling Billions of Edges in a Graph Database
Neo4j case studies with Walmart, eBay, AirBnB, NASA, etc
FlockDB: Distributed Graph Database for Storing Adjacency Lists at Twitter
JanusGraph: Scalable Graph Database backed by Google, IBM and Hortonworks
Amazon Neptune
搜索数据库 - ElasticSearch
Elasticsearch: The Definitive Guide 这是官网方的 ElasticSearch 的学习资料,基本上来说,看这个就够了。
接下来是 4 篇和性能调优相关的工程实践。
Elasticsearch Performance Tuning Practice at eBay
Elasticsearch at Kickstarter
9 tips on ElasticSearch configuration for high performance
Elasticsearch In Production-Deployment Best Practices
最后是 GitHub 上的资源列表 GitHub: Awesome ElasticSearch 。
小结
好了,总结一下今天分享的内容。虽然有人会认为数据库与程序员无关,是 DBA 的事儿。但我坚信,数据库才真正是程序员的事儿。因为程序是需要和数据打交道的,所以程序员或架构师不仅需要设计数据模型,还要保证整体系统的稳定性和可用性,数据是整个系统中关键中的关键。
对于数据库方向,重点就是两种数据库,一种是以 SQL 为代表的关系型数据库,另一种是以非 SQL 为代表的 NoSQL 数据库。因而,在这篇文章中,我给出了 MySQL 和各种开源 NoSQL 的一些相关的有价值的文章和导读,主要是让你对这些数据库的内在有一定的了解,但又不会太深。同时给出了一些知名企业使用数据库的工程实践,这对于了解各种数据库的优劣非常有帮助,值得认真读读。
从下篇文章开始,我们将进入分布式系统架构方面的内容,里面不仅涵盖了大量的理论知识,更有丰富的入门指导和大量的工程实践。敬请期待。