---
title: "leveldb资料整理"
author: "hideto"
date: "2011-12-28T19:50:57Z"
tags: []
categories: ["开源软件"]
slug: "leveldb"
draft: false
lastmod: "2012-01-05T15:35:32Z"
id: 1328921
url: "https://hideto.iteye.com/blog/1328921"
---

**leveldb介绍**
<http://code.google.com/p/leveldb/>
<http://en.wikipedia.org/wiki/LevelDB>
<http://highscalability.com/blog/2011/8/10/leveldb-fast-and-lightweight-keyvalue-database-from-the-auth.html>
<http://news.ycombinator.com/item?id=2526032>
<http://basho.com/blog/technical/2011/07/01/Leveling-the-Field/>
<http://blog.yufeng.info/archives/1327>
<http://www.slideshare.net/sunzhidong/google-leveldb-study-discuss>

**leveldb官方文档**
<http://leveldb.googlecode.com/svn/trunk/doc/index.html>
<http://leveldb.googlecode.com/svn/trunk/doc/benchmark.html>
<http://leveldb.googlecode.com/svn/trunk/doc/impl.html>
<http://leveldb.googlecode.com/svn/trunk/doc/table_format.txt>
<http://leveldb.googlecode.com/svn/trunk/doc/log_format.txt>

**leveldb内部实现和源码解析**
<http://blog.xiaoheshang.info/?cat=26>
<http://rdc.taobao.com/blog/cs/?p=1378>
<http://www.cnblogs.com/haippy/archive/2011/12/04/2276064.html>

**bigtable/mapreduce/gfs/lsm-tree/skiplist论文**
<http://blademaster.ixiezi.com/2010/03/27/bigtable：一个分布式的结构化数据存储系统中文版/>
<http://blademaster.ixiezi.com/2010/03/27/google-mapreduce中文版/>
<http://blademaster.ixiezi.com/2010/03/27/the-google-file-system中文版/>
<http://staff.ustc.edu.cn/~jpq/paper/flash/1996-The%20Log-Structured%20Merge-Tree%20%28LSM-Tree%29.pdf>
<http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.15.9072&rep=rep1&type=pdf>

**Tair ldb**
<http://rdc.taobao.com/blog/cs/?p=1394>
<http://code.taobao.org/p/tair/wiki/index/>
<http://code.taobao.org/p/tair/src/branches/ldb/src/storage/ldb/>

**相关资料**
<http://www.quora.com/What-is-an-SSTable-in-Googles-internal-infrastructure>
<http://www.ningoo.net/html/tag/dynamo>
<http://wiki.apache.org/cassandra/MemtableSSTable>
<http://wiki.apache.org/cassandra/ArchitectureSSTable>
<http://en.wikipedia.org/wiki/Queuing_theory>
<http://rdc.taobao.com/team/jm/archives/1344>

**Notes**
**leveldb的Write/Delete: **
DB::Put/Delete(DB::Open时*dbptr = impl) => DBImpl::Write => (1) 写log: log_->AddRecord (2) 写memtable: WriteBatchInternal::InsertInto(updates, mem_)

**leveldb的Get: **
DBImpl::Get => (1) 查memtable: mem->Get (2) 查immutable memtable: imm->Get (3) 查文件 versions_->current() => current->Get => Version::Get

**leveldb的Compaction: **
leveldb在Open/Get/Write时都有可能做Compaction: DB::Open/DBImpl::Get/DBImpl::Write(DBImpl::MakeRoomForWrite) =>DBImpl::MaybeScheduleCompaction => env_->Schedule(&DBImpl::BGWork, this) => (1) 启后台线程 PosixEnv::Schedule (2) DBImpl::BGWork => DBImpl::BackgroundCall => DBImpl::BackgroundCompaction

**leveldb的多线程写: **
DBImpl::Write的瓶颈在AcquireLoggingResponsibility，多线程写同一个db时互相竞争logger_，性能反而没有单写线程快. 所以为了scale，对leveldb做sharding，将key做hash后分到多个db，这样多线程读写不会相互竞争，经测试 num_threads : num_dbs为1:1时性能最好，充分利用多核

**leveldb的性能调优: **
通过sharding/batch writes/increase block_size(size per data block, default 4KB)/increase block_cache(LRUCache, default 8MB)/increase write_buffer_size(memtable size, default 4MB)来提高性能，经过测试，单机24-core采用16 threads/16 shards/1000 batch_sizes/block_size 8K/write_buffer_size 32MB能达到70w+ ops/sec的写性能