{"id":187,"date":"2014-04-22T13:49:21","date_gmt":"2014-04-22T05:49:21","guid":{"rendered":"http:\/\/sinofool.net\/blog\/?page_id=187"},"modified":"2016-10-03T02:19:39","modified_gmt":"2016-10-02T18:19:39","slug":"practices-of-using-mysql-and-dbpool","status":"publish","type":"post","link":"https:\/\/sinofool.net\/blog\/archives\/187","title":{"rendered":"Practices of using MySQL and DBPool"},"content":{"rendered":"<address>\u7ed9\u516c\u53f8\u5199\u7684\u6587\u6863\u8349\u7a3f\uff0c\u7a0d\u540e\u4f1a\u5199\u6210\u4e2d\u6587\u7684\u3002<\/address>\n<h1>Summary<\/h1>\n<p>In large cluster environment, it is always challenge of manage hundreds MySQL databases.<\/p>\n<ul>\n<li>Planed down time of database server maintenance<\/li>\n<li>Scalability<\/li>\n<li>Change of data structures<\/li>\n<\/ul>\n<p>Ideally, these all handled by proper development process and you have enough software engineers to support.<br \/>\nBut in real world, it is more urgent of solving problems.<\/p>\n<p>Here is the 9 best practices to operate thousands of app servers and databases and we don\u2019t have any &#8220;user impact\u201d because of database maintenance.<\/p>\n<h2>Database design:<\/h2>\n<h3>1 Global design<\/h3>\n<p>We design our database schema like a one-node-cluster, we can run all the service in one box or 1000 boxes.<br \/>\nIt is transparent to software engineers.<\/p>\n<h3>2 Simple only<\/h3>\n<p>Use only basic MySQL features: table, primary key, index, replication.<\/p>\n<h2>Application design:<\/h2>\n<h3>3 Use app server\u2019s CPU.<\/h3>\n<p>App servers are scalable, but MySQL is a bottle neck, Use as much as application CPU, which means:<br \/>\nDo not query without index<br \/>\nDo not sort using database<br \/>\nDo not use any query causes temporary tables<\/p>\n<h3>4 Use a abstraction layer of tables<\/h3>\n<p>We use DBPool for a long time<\/p>\n<h2>Ops workflow:<\/h2>\n<h3>5 Vertical partitioning<\/h3>\n<p>When you want to move a few tables to a different master.<br \/>\na. Setup new master (B) as the slave of the old master (A)<br \/>\nb. Change the DBPool configuration, point master to B.<br \/>\nc. In the middle of the time, (B) will also receive some update requests from old client.<br \/>\nd. Make sure it is no any queries to tables on B<br \/>\ne. Stop the replication and optionally drop old tables on A.<\/p>\n<h3>6 Horizontal shading<\/h3>\n<p>When you want to distribute data of one table into more physic servers.<br \/>\na. Estimate how many is needed, find a proper shading key. You should visit only one instance after shading.<br \/>\nb. A good shading number may be 10 or 100. It is human friendly when debug.<br \/>\nc. You don\u2019t need to have 100 physic servers to deploy all tables, DBPool have the ability of route.<br \/>\nd. Use the same method to move tables to new master as showed in (5)<\/p>\n<h3>7 Change of data structures<\/h3>\n<p>a. We do add column only, no drop column.<br \/>\nb. Application level compatible is required. Make sure new code is working with both old and new data. (if impossible, see (8))<br \/>\nc. Make the changes<br \/>\nd. Update the application to use new column for new feature.<\/p>\n<h3>8 Data migration<\/h3>\n<p>This situation always involves a big change to the logic, you need to redesign the structure<br \/>\na. Create a new master (B) of tables using (5)<br \/>\nb. Create new data structures on (B).<br \/>\nc. Create a trigger to update new structure when old data changed on (B).<br \/>\nd. Migrate your old data into the new structure, pay attention to (c) have already moved some recent data.<br \/>\ne. Create a new abstract instance in DBPool, for new structures.<br \/>\nf. Update the application use the new structure for reading.<br \/>\ng. In the same time, old client and new client have the same data for we have (c).<br \/>\nh. Update the application use the new structure for writing.<br \/>\ni. Stop the replication (a), trigger (c) and drop the old tables<\/p>\n<h3>9 Planned maintenance<\/h3>\n<p>a. DBPool is enough to move MySQL slave servers.<br \/>\nb. Use (5) to make a new master or promote one slave to master.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u516c\u53f8\u5199\u7684\u6587\u6863\u8349\u7a3f\uff0c\u7a0d\u540e\u4f1a\u5199\u6210\u4e2d\u6587\u7684\u3002 Summary In large cluster environment, it is always challenge of manage hundreds MySQL databases. Planed down time of database server maintenance Scalability Change of data structures Ideally, these all handled by proper development process and you have enough software engineers to support. But in real world, it is more urgent of solving problems. Here [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[5],"tags":[],"class_list":["post-187","post","type-post","status-publish","format-standard","hentry","category-tech"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/sinofool.net\/blog\/wp-json\/wp\/v2\/posts\/187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sinofool.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sinofool.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sinofool.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sinofool.net\/blog\/wp-json\/wp\/v2\/comments?post=187"}],"version-history":[{"count":1,"href":"https:\/\/sinofool.net\/blog\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"predecessor-version":[{"id":188,"href":"https:\/\/sinofool.net\/blog\/wp-json\/wp\/v2\/posts\/187\/revisions\/188"}],"wp:attachment":[{"href":"https:\/\/sinofool.net\/blog\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sinofool.net\/blog\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sinofool.net\/blog\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}