Archive for the ‘Databases’ Category

Services Oriented Architecture with PHP and MySQL

Joe Stump, Lead Architect, Digg. Slides should make its way at Joe’s website soon enough.

Mainly works on the backend, makes sure its scalable, can all the Digg buttons be served, et al.

Application layer is loosely coupled from your data. Whole point of SOA? You can put a service in front of the DB, and move between DB’s if required.

They do use MySQL, but its pretty vanilla.

Old habits die hard
– Data requests are sequential (I need foo, bar, bleh, ecky)
– Data requests are blocking (When you need foo, nothing else is happening)
– Tightly coupled (mysql_query, and if you’re using DB abstraction layer even, you’re still using SQL… you then can’t use CouchDB for instance)
– Scaling is not abstracted (a lot of caching are in the front end code. Its a problem when you start scaling your teams out). They use memcached from what I gather.

SOA
– Data is requested from a service (via HTTP, custom, etc.)
– Data requests are run in parallel (over non-blocking sockets. 10 data requests in 1 webpage, and each request takes 10ms. It might now only take 70ms now, maybe, over 100ms. Generally 1.5-2.5x faster now, for blocking parallel requests)
– Data requests are asynchronous (non-blocking parallel requests)
– Data layer is loosely coupled
– Scalability is abstracted (can find engineers anywhere, that can parse JSON or XML :P)

Options?
– Run requests over HTTP (Google (Java), Amazon (Java), etc.)
– New York Times’ DBSlayer (small little HTTP server that runs and provides parallel and async requests to mysql)
– Danga’s Gearman (binary protocol, has worked, its kind of a queuing system)
– Remember the wall clock goes down, but the CPU time is still happening, its still the same

HTTP w/PHP
1. Group requests for data at the top
2. Open a socket for each request
– Sockets must be non-blocking
– Make sure to use TCP_NODELAY
3. Use __get() to block for results
4. See Services_Digg_Request

Use a pear package, called Services_Digg for the above example. Note Digg’s API documentation as well.

HTTP is widely supported in all languages. Its very easy to get up and running, with lots of options for servers/tuning. Overhead in the protocol is great, and Apache itself has a lot of overhead.

DBSlayer
– small HTTP daemon written in C. You post JSON to it for communications
– connection pooling (benchmark mysql connection, and there’s a whole bunch of overhead in the mysql authentication; mysql proxy does this too)
– load balancing and failover (like mysql proxy)
– tightly coupled to MySQL (no migration)
– tightly coupled to SQL (no CouchDB)
– no intelligence

Gearman
– highly scalable queuing system (worker bees, like PHP scripts. Sockets open, client comes to gearman server to do foo, and it says it has n number of workers, and gearman gets ’em to work. So it works linearly. Jobs can return results back, run in parallel on many gearman servers and many CPUs)
– simple and efficient binary protocol
– sets of jobs are run in parallel
– queue can scale linearly
– php, perl, python, ruby, c clients
– poorly documented (“I think poorly documented is giving them too much credit.. All danga stuff has next to no documentation”)
– livejournal uses this, instead of using HTTP running
– its not very “robust” (it scales, they at digg don’t see massive number of failing jobs. Queue isn’t persistent though. When pushing stuff, and gearman gets restarted, the queue goes away – there is a workaround, for this, so ask Joe – its an undocumented feature available though)
– digg uses it in the submission process for crawling
– Chris at Yahoo! uses Gearman requests to run multiple memcached GETs (if you’re not using multi-get, check them).
– Check out Net_Gearman, which is a PEAR package

DIY option?
– not recommended, unless you have a highly customised solution, i.e. what Flickr does
– they ran into a problem where uploading an image, and then getting the image resized, for large images, was a problem. So they use a custom binary protocol that is much more efficient for the datasets (think, an SLR has files that are 7MB in size or something)
– this requires more resources (humans, engineers!)

What goes in the Services layer?
– smart caching strategies
– data mapping and distribution
– intelligent grouping of data results
– partitioning logic

Remember to intelligently group data into endpoints, and version them! This will help you improve your software.

Consider bundling and grouping requests (bulk loading).

EPIC FAIL!
– sending SQL over for translation? Pfft. DBSlayer does this, but it tightly couples you
– hundreds of teeny tiny endpoints (cohesive endpoints that return a decent amount of data)
– running SOA requests sequentially! You then get no benefits from an SOA architecture, at all. Parallel requests are good.

Technorati Tags: , , , , , , , , , , , , , , , , , ,

EXPLAIN Demystified

Baron Schwartz gave a most interesting talk about EXPLAIN. You will definitely want to read his slides (filled with detail), when they make their way online. These notes are very sparse, just bits that I didn’t see in the slides, that Baron mentioned verbally. Plenty of good questions, and plenty of interaction.

EXPLAIN only works for SELECT queries.

How does MySQL execute queries? Optimisation happens even as the query is being executed. As the query is being optimised, some execution happens as well. Execution Plan is a data structure, not bytecode.

When EXPLAIN’s output is generated, MySQL actually executes the query. It just set’s DESCRIBE on it, rather than executing it. Everything is a JOIN to MySQL (union, SELECT 1 [simplest base case join], etc…).

key_len – to know if your table is indexed well.

rows: estimated number of rows to read, but not the number of rows in the result set. In 5.1 and greater, it reflects LIMIT, but not before.

Maatkit includes mk-visual, so you can have a visual explain. This is also, very machine readable.

Update: Artem has good notes too.

Technorati Tags: , , , ,

MySQL Full Text Search by Alex Rubin

Download the PDF: http://www.mysqlfulltextsearch.com/full_text.pdf

Default search by relevance, default sort is by relevance

Boolean search is also popular. cats AND dogs. No default sorting, so you need to order the results yourself

Phrase search

MySQL Full Text Index, only available with MyISAM, and it supports natural language and Boolean search. ft_min_word_len – 4 characters per word by default is indexed. Frequency based ranking, doesn’t count distance between words

SELECT * FROM articles WHERE MATCH (title,body) AGAINST (‘database’ IN NATURAL LANGUAGE MODE);

For Boolean, you use AGAINST (‘cat AND dog’ IN BOOLEAN MODE).

n-gram fulltext plugin for CJK languages are available as plugins

DRBD and MySQL FullText search? DRBD requires InnoDB, when there is a failover, DRBD needs to perform a reovery. Fulltext only works for MyISAM. So ou create a “FullText” slave MyISAM table with FullText indexes. The slide (diagram) is most useful for this, naturally.

Speed up FT search? Fit the index into memory. key_buffer = total size of full text index (max=4GB). You can preload FT indexes into buffer.

You can manually partition. Partitioning decreases index and table size, so search is faster. Application needs changing of course. MySQL 5.1 partitioning features, do not support FTs.

Order by/Group by is a performance killer. Using order by date, is much slower than with no order by.

Real World Performance Killer
SELECT … FROM `ft` WHERE MATCH `album` AGAINST (‘the way i am’)
The above query, is very slow! It took like 13 seconds or so.

Note the stopword list and ft_min_word_len. I is not a stopword, but “the”, “way”, and “am” are stopwords.

ft_min_word_len = 1 will mean that all words except “i” will be filtered out with the standard stoplist. “i” is contained in lots of text!

Search with error correction? Use soundex() MySQL function (sounds similar). select soundex(“Dilane”) should equate to Dylan. You can sort it either by popularity or Levenstein distance (either by a stored procedure or a UDF).

Sphinx – nice, open source, can be faster than MySQL full text index on a large dataset, supports multi-node clustering out of the box. It is however an external solution that isn’t built-in, and needs to be integrated.

MySQL 5.0: need to patch source code. MySQL 5.1: copy Sphinx plugin to the plugin_dir.

You can set Sphinx to be MySQL’s storage engine if you like.

Resources

Technorati Tags: , , , , , , , ,

Keynote with Marten Mickos at MySQL Conference 2008

Live blogging from Marten Mickos’ (SVP Database Group) keynote at the MySQL Conference and Expo 2008.

Why did Sun acquire us?
The culture and the vision. Biggest match with Sun. “Network is the computer” and “Best Online Database”. Great affinity.

Its a 1 billion dollar vote for the LAMP stack.

Integrating better with OpenOffice.org, run better with Glassfish, and so on. Now, performance and scaling is number one, to make databases run faster. It will take time, but it will be very positive.

This conference itself is just as big as LinuxWorld was seven years ago.

An amazing photo of burning the IPO Prospectus – a bonfire in Santa Cruz. Getting rid of the past!

Scale exponentially, but keep the cost growth at a linear scale. World is flat – build your software in Russia, China, etc. and deploy elsewhere even. Fail fast? Remember to scale fast.

If the whole world is online, how do you think about business? If the world is online, what is the best way to develop software? Its open source, so do it online. Best way to deploy? Maybe not software as a service, but maybe a platform as a service?

Everytime MySQL makes a business model, someone tells us that its stupid and the blogosphere bursts with discussion. The subscription model is the winning model for us. We do see advertising models for example for the online world, but for us, its subscription.

If the whole world is online, how do you organise working? 70% work at home, in over 30 countries. Our organisation is completely spread out. But remember, you get to hire the best people wherever they are in the world.

Customers have choice. The ISV market was the software market, when Marten grew up. That’s not the case anymore. Enterprise 2.0 is building new application. You can but the application (OEM). Now there’s a huge market around buying the service (SaaS). And now, there’s also the market of using the service – you just use Google, or Amazon on the web for instance. Just a major shift in how the software world works.

Remember, It’s Your Data. This is probably the most thing… Data is the Intel inside of the next generation of computer applications (so says Tim O’Reilly). Data drives the web. Vendors might attempt to close the data and lock you in – don’t. Hold on to your data, it is yours. Keep using an open source database, keep using ODF. Its your data. Avoid vendor lock-in.

3 design priorities: reliability, performance and ease of use.

Reliability: Bugs fixed in 5.1: 997 in 2007 plus, 386 so far this year.
Performance: DBT2 performance tests on 5.1.24-rc vs 5.0 shows 10-15% throughput improvements at medium concurrency
Ease of Use: MySQL Workbench is GA as of this morning, and Mike Zinner is on stage.

If you still write SQL query scripts, just turn around, use MySQL Workbench and have a beer. Lots of clapping :) This is in reference to the t-shirt…

Storage Engines: Kickfire, Infobright (for datawarehousing, now resold by Sun), InnoDB (contract renewed!), PBXT (blob streaming coming out), Nitro Security (datewarehousing), SacleDB and Tokutek, Maria (Monty’s baby, MyISAM with transactions and crash recovery), Falcon (superior performance on 16-way Intel Caneland at all DBT2 workloads).

Technology direction?
Marten is leader of an open source group, and innovation happens elsewhere. Our direction?

  • Scale (read, write scalability, scale out, scale up, sharding, etc.). No matter what Ferrari models you have, your customer’s continue to want more horsepower.
  • Data by SQL or not – we work with memcached now, as well.
  • Database as software or as a service (look at db4free.net)
  • Opening up Architecture of Participation (inviting folk to meetings, worklog entries open, et al) – building up a strong ecosystem.

Technorati Tags: , , , , , , ,

Winners for 2008 at the MySQL Conference

2008 MySQL Application of the Year
Social Network – MySQL powered with over 70 million active users (Facebook)
Mobile operator – Highly available LAMP platform at the heart of SMS, mobile and CRM applications (Virgin Mobile France)
eCommerce site – Built caching tier using MySQL for 4 billion transactions per day (eBay)

2008 MySQL Partner of the Year
Open source backup solution (Zmanda)
Expanding support for open source (Microsoft)
Leading reseller (Computercenter)

2008 MySQL Community Member of the Year
Code Contributor (Baron Schwartz)
Quality Contributor (Diego Medina)
Community Advocate (Sheeri Kritzer Cabral (again!))

I have a photo of a representative of Virgin Mobile France, which I’ll upload soon :) Turns out I was sitting next to him…

Technorati Tags: , ,

Rich Green says “Don’t Panic”!

In-between Marten’s keynote, Rich Green, EVP Software, Sun Microsystems, comes up on stage, and here’s my live-blog.

He talked about the famous dinner last year, for buying MySQL. Autonomy reigns supreme in MySQL. MySQL continue at their same course and speed. The plan is the plan, until there is a new plan – and there is no new plan.

Sun has a heterogeneous platform. Many partners, including Dell, IBM, Intel, and so on. Lots of open source in terms of software. The cultures, discourse, intellectual arguments, its an excellent mash up, this Sun and MySQL. Don’t Panic! There is no change for what has been an incredibly successful thing.

Sun’s Continued MySQL Commitment:

  • profitable business on the principles of FOSS
  • GPL – consider GPLv3 as it involves, and we understand the interest and evolution of the community. Its GPLv2 still, naturally
  • Tuning for the most popular operating systems and hardware platforms
  • Integrating with the most popular development environments and other related technologies

Technorati Tags: , , , ,


i