Database Sharding at Netlog
Database Sharding at Netlog
Database Sharding at Netlog
SHARDING
@NETLOG
Tags
performance,
partitioning, federation,
database, php, mySQL,
memcached, sphinx
jurriaanpersyn.com
lead web dev at Netlog
php + mysql + frontend
since 3 years
A Pan European Social Network
• php • squid
• apache
• debian
• memcached
• sphinx
• lighttpd
Topics
- Netlog history of
scaling
- Sharding architecture
- Implications
- Our approach
- Tackling the problems
Master
r/w
Master
w
Slave Slave
r r
Top
Master
w
Messages Friends
r/w r/w
Top Top
Slave Slave
r r
Top
Master
w
Messages Friends
w w
Top Top
Slave Slave
r r
Msgs Msgs Frnds Frnds
Slave Slave Slave Slave
r r r r
1040:
Too many Top
connections Master
w
Messages Friends
w w
Top Top
Slave Slave
r r
Msgs Msgs Frnds Frnds
Slave Slave Slave Slave
r r r r
1040:
Too many Top
connections Master 1040:
w Too many
1040: connections
Too many
Messages
connections Friends
w w
Top Top
Slave Slave
r r
Msgs Msgs1040: Frnds Frnds
Too many
Slave Slave
connections Slave Slave
r r r r
?
More vertical
partitioning?
Master-to-master
replication?
Caching?
Sharding!
pieces
fragments
horizontal part.
federation
Photos
%10 = 2
Photos Photos
%10 = 1 %10 = 3
Photos Photos
%10 = 0 %10 = 4
Aggregation
Photos Photos
%10 = 9 %10 = 5
Photos Photos
%10 = 8 %10 = 6
Photos
%10 = 7
More data?
More shards!
A simple example
• A blog
• Split users across 2 databases
• Users with even ID go to database 1
• Users with uneven ID go to database 2
• Query: Give me the blog messages from
author with id 26
What was ...
$db = DB::getInstance();
$db->assignInt('userID', $userID);
$db->execute();
Becomes ...
$db = DB::getInstance($userID);
$db->assignInt('userID', $userID);
$db->execute();
What to partition on?
• “sharding key”
• eg. for a blog
• partition on publication date?
• partition on author?
• author name?
• author id?
• partition on category?
How to partition that key?
• Partitioning schemes
• eg.
• Vertical
• Range based
• Key or hash based
• Directory based
The result
impossibly complicated
• Solutions:
• It’s possible to design (parts of) the
application so there’s no need for cross-
shard queries
Some implications ...
• Solutions:
• Parallel querying of shards
• Double systems
• guestbook messages:
• shard on id of guestbook owner
• shard on id of writer
• Denormalizing data
Some implications ...
• network implications
• keep connections open? close after every
query? pools?
Existing / related solutions?
• HyperTable (HQL)
• HBase / BigTable
Existing / related solutions?
• Hibernate Shards
(whole new DB layer for us)
• SQLAlchemy
(for Python)
• Oracle RAC
(well, not MySQL)
Goals
• in-house
• 100% php
• middleware between application logic and
class DB
sharddb001 sharddb002
• Sharding Management
• Lookup System (directory based)
• Balancer / Manager
• Sharded Tables API
• Database Access Layer
• Caching Layer
Sharding Management Directory
• $userID to $shardID
(via SQL/memcache - combination not
fixed!)
• Example API:
• An object per / -combination
$tableName $userID
• memcached
• parallel processing
• sphinx
Memcached
• Makes it faster
• much faster
• much much faster
• Makes some “cross shard” things possible
Using memcached
function isObamaPresident()
{
$memcache = new Memcache();
$result = $memcache->get('isobamapresident'); // fetch
if ($result === false)
{
// do some database heavy stuff
$db = DB::getInstance();
$votes = $db->prepare("SELECT COUNT(*) FROM VOTES WHERE
vote = 'OBAMA'")->execute();
$result = ($votes > (USA_CITIZEN_COUNT / 2)) ? 'Sure
is!' : 'Nope.'; // well, ideally
$memcache->set('isobamapresident', $result, 0);
}
return $result;
}
Typical usage for Netlog Sharding API
• READ_UPDATE_INSERT_MODE
• Needs memory
• Problem:
How do you give an overview of eg. latest
photos from different users? (on different
shards)
• Solution:
Check Jayme’s presentation “Sphinx search
optimization”, distributed full text search.
(Use it for more than searching!)
Tips & thoughts
Notes to the slides will become available soon at the Netlog Developer
Blog and my personal blog (www.jurriaanpersyn.com)