Filedb:受 Bitcask 启发的基于磁盘的键值存储
Filedb: Disk-based key-value store inspired by Bitcask

原始链接: https://github.com/rajivharlalka/filedb

FileDB是一个基于Zig语言的键值存储,受Bitcask启发,具有高吞吐量和O(1)的读取性能。它使用日志结构哈希表存储元数据,并采用追加写入的方式操作磁盘,从而提高写入性能。新数据总是写入活动数据文件。重启或达到最大文件大小时,数据文件会进行轮换,旧文件变为只读。 一个定期运行的压缩进程会合并这些文件,并更新元数据。数据文件根据配置同步到磁盘,或者可以选择在每次写入后同步。它提供了一个兼容Redis的客户端。基准测试显示,单线程下每秒可以处理13736个SET请求和44286个GET请求。多线程可以进一步提高吞吐量,使用10个线程时,每秒可以处理104876个GET请求。

这个Hacker News帖子讨论了Filedb,一个受Bitcask启发的基于磁盘的键值存储。用户分享了他们使用Bitcask的经验,强调了它的简单性、快速的写入速度以及其适用于特定用例,例如存储最终一致性可接受的数据。一位用户指出缺乏二级索引是一个限制。Filedb的实现细节受到质疑,特别是关于数据完整性和`sync`操作。经验丰富的用户将Filedb视为一个学习项目,将其与SQLite和其他数据库选项进行比较。一些人认为,虽然Filedb本身并非生产就绪型,但Bitcask背后的原理(快速写入、日志结构存储、最终一致性)对于理解数据存储设计非常有价值,并且适用于某些不需要绝对数据一致性的应用程序,例如遥测或GPS跟踪。
相关文章

原文

A key-value store inspired by Bitcask.


FileDB is a Zig-implementation of Bitcask by Riak1 paper.

  • FileDB stores record metadata in a log-structured hashtable and parallely keeps 1 disk file open for inserting records in append-only mode. On restarts or MAX_FILE_REACHED, the disk file is rotated and all the oldfiles are kept open for reading only.
  • A compaction process running every config.compactionInterval seconds, reads all the disk files and combines them into one file while updating the metadata hashtable.
  • A sync process syncs the open disk files once every config.syncInterval. Sync also can be done on every request if config.alwaysFsync is True.

Read about internals in-depth at FileDb.

  1. Since the metadata keeps an exact location of file and position in file for a record, fetching records become O(1) operation.
  2. All metadata records are constant size, so irrespective of the size of the value of a record the in-memory store keeps a constant sized metadata.
  3. Provides high throughput by using the open file in append only mode.
  1. init(allocator: std.mem.Allocator, options: ?config.Options) : Intialized FileDB
  2. deinit(): Deinitalizes FileDB
  3. put(key:[]const u8, value: []const u8): Inserts a key-value pair in the database to be tracked.
  4. get(key:[]const u8): Retrieved a key-value pair from the database.
  5. delete(key: []const u8): Delete a key-value pair from the database
  6. list(allocator: std.mem.Allocator): Returns a list of keys stored in the database.
  7. sync(): Syncs the current open datafile on the disk
  8. storeHashMap(): Creates the HINTS file
  9. loadKeyDir(): Loads the hashmap from the HINTS file

Along with the library, a Redis-compatible client is available.

127.0.0.1:6379> RING
(error) ERR unknown command
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> get abcd
(nil)
127.0.0.1:6379> set abcd def
OK
127.0.0.1:6379> get abcd
"def"
redis-benchmark -p 6379 -t set -n 10000 -r 100000000
Summary:
  throughput summary: 13736.26 requests per second
  latency summary (msec):
          avg       min       p50       p95       p99       max
        3.615     0.088     3.455     6.831     8.831    14.919
      
redis-benchmark -p 6379 -t set -n 200000 -r 100000000
Summary:
  throughput summary: 14375.04 requests per second
  latency summary (msec):
          avg       min       p50       p95       p99       max
        3.452     0.072     3.087     6.767    10.647   114.303

redis-benchmark -p 6379 -t get -n 100000 -r 100000000
Summary:
  throughput summary: 44286.98 requests per second
  latency summary (msec):
          avg       min       p50       p95       p99       max
        0.573     0.088     0.519     0.967     1.447     7.495

redis-benchmark -p 6379 -t get -n 1000000 -r 1000000000 --threads 10
Summary:
  throughput summary: 104876.77 requests per second
  latency summary (msec):
          avg       min       p50       p95       p99       max
        0.405     0.032     0.375     0.831     1.295    26.047
  1. Bitcask Paper by Riak
  2. Go Implementation of Bitcask
  1. https://www.openmymind.net/Basic-MetaProgramming-in-Zig/
  2. https://pedropark99.github.io/zig-book/
  3. https://zig.guide/standard-library/
  4. https://zighelp.org
  1. https://riak.com/assets/bitcask-intro.pdf

联系我们 contact @ memedata.com