WordPressのメモリ消費を節約する

WordPressを動かしてるこのサーバが重くてどうしようもなくなってきたので、メモリを節約するようチューニングしてみました。試してみたことをメモ。

サーバ環境:

  • メモリ256MBの軟弱VPSです。
  • Ubuntu 9.04

かなり適当にやっつけているので、参考にならん!って方は僕が参考にしたこちらなんかをどうぞ:
Optimize your Apache VPS for WordPress | Jestro

Apacheのモジュールを減らす

…と思ったけど無駄なものは最初から何もなかった。

php.ini

影響しそうなのはmemory_limit。 PHPがApacheと組んで消費するメモリはmemory_limit*httpd.confのMaxClientsとのことなので、これを適当に減らしてみる。あと、memory_limitを変更したらpost_max_sizeがそれを超えないように設定すべきとのことだけど、十分下回ってるようなので今は据え置き。

Before:

memory_limit = 32M
post_max_size = 8M

After:

memory_limit = 16M
post_max_size = 8M

httpd.conf

さっき触れたMaxClients。Ubuntu上のApache2+PHPはデフォルトでmpm_preforkを使ってるようなので、その関連設定を変更。値は適当です。

Before:


     StartServers          5
     MinSpareServers       5
     MaxSpareServers      10
     MaxClients          150
     MaxRequestsPerChild   0

After:


     StartServers          2
     MinSpareServers       2
     MaxSpareServers       4
     MaxClients           50
     MaxRequestsPerChild   0

本当はサーバのメモリが256MBとケチくさいのでMaxClients 50は高すぎるんだろうけども。

次はタイムアウト。短く切ってやる。ついでにKeepAliveのタイムアウトも短く。ここも適当…。

Before:

Timeout 300
KeepAliveTimeout 15

After:

Timeout 30
KeepAliveTimeout 10

結果

だいぶ適当に数字を減らしただけなんだけども、十分使い物になるレベルにまで軽くなった。面倒だから特に検証もしてないけど、memory_limitとStartServersとMaxSpareServersが一番効いてるような気がする。ブログのアクセス数は大したことないから、たぶんこれでしばらく持つんじゃないだろうか。

Categories: HowTo's, Tips and Tricks |Tagged , | 3 Comments

Initial Thoughts on Spider for MySQL

Recently I had the chance to play with a storage engine for MySQL called Spider, so I’m jotting down some notes on what it is and what my initial thoughts were.

Spider is not your typical storage engine: it allows you to redirect your data and queries to other MySQL instances depending on field values. That means you can use it to shard databases in a relatively easy way.

Spider for MySQL

Spider for MySQL

Currently it’s available for MySQL versions higher than 5.1. Get it here:
Spider for MySQL in Launchpad

Here’s the creator’s blog where release info and such are posted (in Japanese):
Wild Growth 日本語

What to configure

  1. Set up four MySQL servers (A, B, C, and D)
  2. Create your normal schema in B, C, and D
  3. Create tables with the same schema as above, but with ENGINE=Spider. In this CREATE TABLE statement, specify B, C, D as databases where the data is actually stored.

You can get the details for configuration in the official docs. You’ll probably want to check the links below as well.

How it’s used

Just throw queries to server A like you normally would, and Spider will do the rest for you. No data is stored in A at all: everything is distributed over to the other three, which effectively achieves database sharding. What’s great about using Spider to shard your data is that you don’t have to make any changes in your client app code whatsoever.

What’s actually happening

Say you throw this query to server A: code>INSERT INTO users VALUES (‘someone@domain.com’, ‘Bobby’);. The INSERT statement is passed over to one of the three other servers depending on the conditions you set when you created the Spider table (for instance, by checking id mod 3). So what happens is that the row is stored in either B, C, or D, and A never holds any data at all.

From the client app point of view, all that happens is that you throw a query to A and A returns a result set, just like any old MySQL server. Other queries like SELECTs gets handled in the same transparent manner.

Of course, this won’t distribute the request load, but they say that IO loads make up for nearly all of a database server’s loads, don’t they?

This is all built upon a feature in MySQL called partitioning (which I didn’t know existed till recently, having been a very light user of MySQL). It’s a feature that lets you save data rows to different places in your local filesystem depending on the value of a particular column. What Spider does is it makes the three data tables (B, C, and D in this case) act as MySQL partitions.

Initial thoughts

It actually does what it says it does. It’s cool how in most cases you probably won’t have to change your app code at all thanks to the transparent query processing. So if your app code is not scale-ready, this might be a good solution to make some leeway.

But here’s the catch. The consequences of being built upon MySQL partitioning might be a pain in the ass. I think I’ll discuss that in another post.

Oh, and not being able to find a lot of tips, howtos and usage reports on the web doesn’t make me particularly happy either. There probably isn’t a whole lot of users yet. I pretty much like the concept and it does work, but I get this sort of fear of finding another pitfall along the way.

I’m looking forward to its gaining a bigger user base and getting better and more stable as it gets more bug reports and feature requests.

References

The Data Charmer: Test driving the Spider storage engine – sharding for the masses

Sharding for the masses: Introducing the SPIDER storage engine (OpenSQLCamp @ FrOSCon) | Colin Charles Agenda

Categories: Ideas and Products |Tagged | Leave a comment

Spider for MySQLを使ってみた

最近、バイトでSpiderという面白いMySQL用ストレージエンジンをこねこねすることがあったので、特徴と感想を簡単に紹介します。

Spiderは少し特殊なストレージエンジンで、データを行単位で別のMySQLサーバへ飛ばして保存することができます。何が嬉しいかというと、比較的簡単にテーブルをSharding、つまり細切れにして負荷分散ができるということです。

Spider for MySQL

Spider for MySQL

現在、MySQL 5.1以上で利用可能です。配布元はこちら:
Spider for MySQL in Launchpad

リリース状況などを掲載されている作者ブログはこちら:
Wild Growth 日本語

設定の流れ

  1. MySQLサーバ4つ(A, B, C, D)を用意する
  2. うちB, C, Dの3つにデータ保存用テーブルを作成する(InnoDBなどで)
  3. Aに、B, C, Dと同じスキーマでエンジンがSpiderなテーブルを作成する。データ保存先としてさっき作った3つのテーブルを指定する。

具体的な設定方法については公式ドキュメントや最後尾の参考リンクでどうぞ。

利用の流れ

ふつうにAのMySQLサーバにアクセスしてクエリを投げるだけです。クライアント(アプリ)側には一切変更の必要がありません。ここがSpiderの魅力です。

中で何が起こっているか

たとえばINSERT INTO users VALUES ('someone@domain.com', 'Bobby');というクエリをAに投げると、テーブル作成時に指定した条件に従ってSpiderがB, C, Dから保存先ノードを選び、そこへINSERTを投げ直してくれます。つまりSpiderテーブル自体はデータを一切持ちません

同じくSELECTの場合もAへ投げたものがB, C, Dに飛び、Spiderが結果をまとめて返してくれます。

クライアント(アプリ)側からはあたかも普通にクエリを出して返って来ているように見えるんですが、実際には三つの保存先ノードへクエリが飛んでいるわけです。JOINなども同じように透過的に扱ってくれます。

リクエスト処理の負荷は分散出来ていませんが、データベースサーバの負荷のほとんどはIOだというウワサがありますので、そのIO負荷が分散できるというのはステキなことかも知れません。

実はぬるMySQLユーザな僕は全く知らなかったのですが、もともとパーティショニングという機能がMySQL 5.1以降で実装されています。これはテーブル内のデータを指定したカラムの値に基づいてローカルのファイルシステム内で分割して保存する機能です。Spiderはこのパーティショニングに乗っかって設計されていて、ここではB, C, DのテーブルをAのパーティションとして使っていることになります。

使用感

看板に偽りなしというか、できると言っただけのことはやってくれています。なにより本当にアプリ側を全く変えなくていいのが嬉しい。急にスケールしたくなったときなんかは採用しやすいソリューションかもという気がします。

ただ、パーティショニング機能自体の制限を受けるのがけっこう厄介です。これについてはまた別の記事に書きたいと思います。

あとは、オンラインに情報が少ないのが気になります。おそらくまだあまりユーザ数が多くないんだと思います。コンセプトは良いし実際に問題なく使えるんですが、現状では既知のバグや制限事項などが少ないので、どんな落とし穴が潜んでいるか分からない怖さがあります。

今後、いろんな人に試されて叩かれて強くなっていくことを密かに期待しておきます。

参考リンク

ウノウラボ Unoh Labs: 国産MySQLストレージエンジン「Spider」の作者、斯波健徳氏に聞く

http://dev.mysql.com/doc/refman/5.1/en/partitioning-overview.html
作者によるMySQL Conferenceでのプレゼン資料(zipped pdf)。

The Data Charmer: Test driving the Spider storage engine – sharding for the masses
使ってみた系の記事。設定方法を紹介、どんなクエリが飛んでるかとかも見てます。

Sharding for the masses: Introducing the SPIDER storage engine (OpenSQLCamp @ FrOSCon) | Colin Charles Agenda
使ってみた系の記事。独自の説明プレゼンつき。

Categories: Ideas and Products |Tagged , | Leave a comment

Google Readerの「v」で背面にタブを開くChrome拡張機能

Google Chromeの拡張機能です。Google Readerでvを押したときに元記事が背面のタブで開くようになります。これを入れておけば、タイトルを見ながらいくつも連続で開いておいて、あとでそれぞれを見ていくという読み方ができます。

ダウンロード:
Google Reader: Open in background tab « Google Chrome Extensions

Firefox用には同じことをするGreasemonkeyスクリプトがあったんですが、Chromeでは動作しなかったので作りました。

「ここはこうしとけよ!」みたいなのがあったらコメントでお願いします。

Categories: Things I Made |Tagged , , , | 11 Comments

script/runnerでdebuggerを使う方法

$ rdebug script/runner yourscript.rb

起動すると最初に必ずbreakするけど、contで続行すれば自分が設定したbreakpointまで走る。

Categories: HowTo's, Tips and Tricks |Tagged , , | Leave a comment