最终使 mnesia 数据库与 erlang 保持一致。 最佳实践有人吗?
我正在用 erlang 编写一个 BitTorrent 跟踪器。 鉴于服务的性质, 我不需要绝对的一致性(即,客户可以对稍微过时的同行列表或 torrent 状态感到非常满意)。
到目前为止,我的策略是在启用了 disk_copies 的 RAM 中创建 mnesia 表,以便当日志大小超过一定大小时让 mnesia 自动将内存转储到磁盘。
如果服务器崩溃,一些信息将会丢失。 没什么大不了的。
另一种方法是实例化两个表(仅一个内存和一个磁盘),并每隔一分钟左右将进程从内存复制到磁盘。 这更天真,但允许仅转储内存中内容的子集,减少总体磁盘开销,并可能完全避免使用日志(我实际上不确定最后一条语句)。
我确信还有很多其他方法可以做到这一点。 你的是啥呢?
-teo
I'm writing a bittorrent tracker in erlang. Given the nature of the service,
I won't need absolute consistency (ie. a client can be perfectly happy with a slightly outdated list of peers or torrent status).
My strategy so far has been to create mnesia tables in RAM with disc_copies enabled, so to have mnesia automatically dump the memory to disk when the log size exceeds a certain size.
If the server crashes, some information will be lost. Not a big deal.
A different approach would be to instance two tables (one ram only and one disk only) and have a process copy from ram to disk every minute or so. This is more naive, but would allow to dump just a subset of what's in memory, reducing the overall disk overhead and possibly avoid the usage of a log altogether (I'm actually not sure about this last statement).
I'm sure there are many other ways to do this. What's yours?
-teo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个您可能感兴趣的项目 http://github.com/jlouis/etorrent/tree/大师。
Here is a project that might interest you http://github.com/jlouis/etorrent/tree/master.
您可能需要查看 redis 和 erldis。 Redis 采用第二种方法:所有内容都存储在内存中,然后定期转储到磁盘。
You may want to checkout redis & erldis. Redis takes the second approach: everything is stored in memory then periodically dumped to disk.
就您最初的问题而言,这当然是偏离主题的,但如果您真的正在编写一个纯粹的跟踪器,那么最好完全放弃持久性并将数据仅保留在内存中。
对于一个最小的跟踪器,一个公告只占几个字节:16 个字节用于 SHA1 哈希,6 个字节用于对等点的 IP 和端口,以及更多字节,因为您还需要保留时间戳。 但即使有一点开销,您也能够在内存中保存数百万条记录。
This is certainly off-topic in respect to your original question, but if you're really writing a pure tracker, then it might be best to forego persistence completely and keep the data solely in memory.
For a minimal tracker, an announce weighs in at only a few bytes: 16 bytes for the SHA1-hash, 6 bytes for the peer's IP and port, and some more bytes as you'll need to keep a timestamp as well. But even with a bit of overhead, you'll be able to literally keep millions of records in memory.