简体   繁体   English

将实时数据记录到 NoSQL DB 的最佳实践

[英]Best practices for logging real-time data into a NoSQL DB

I got a Java web application which receives some real-time events and pushes them to the user interface layer.我有一个 Java Web 应用程序,它接收一些实时事件并将它们推送到用户界面层。 I want to log all the perceived events and since the volume of information will be huge, I prefer using a NoSQL db.我想记录所有感知到的事件,由于信息量很大,我更喜欢使用 NoSQL 数据库。

I have setup a mongodb for this purpose which inserts a document per event.为此,我设置了一个 mongodb,它为每个事件插入一个文档。 The problem is that this approach (a disk access per event) slows down the whole process dramatically.问题是这种方法(每个事件一个磁盘访问)显着减慢了整个过程。

So, what approaches can I take in this situation?那么,在这种情况下我可以采取什么方法呢? what options are available in mongodb for this (eg bulk inserting, async inserting, caching, ...)? mongodb 中有哪些可用的选项(例如批量插入、异步插入、缓存等)? would switching to some other NoSQL db implementation make a difference?切换到其他一些 NoSQL 数据库实现会有所作为吗? what are the best practices here?这里的最佳做法是什么?

I have waited for some time to see other answers, but lose my patience.我已经等了一段时间才能看到其他答案,但失去了耐心。 I have used MongoDB as a log storage for 3 projects (two for Java and one for C#).我已经使用 MongoDB 作为 3 个项目的日志存储(两个用于 Java,一个用于 C#)。 Basing on this I can figure out following important rules to organize logging:基于此,我可以找出以下重要规则来组织日志记录:

  1. Don't use indexes.不要使用索引。 If you mostly write then indexes cause performance degradation.如果您主要编写,那么索引会导致性能下降。 If you need post-process log analyzes copy information to another database or collection.如果您需要后处理日志分析,请将信息复制到另一个数据库或集合。 Unfortunately you cannot get rid of primary key _id - just leave it as is (GUID) or replace with auto-increment NumberLong .不幸的是,您无法摆脱主键_id - 保持原样(GUID)或替换为自动递增NumberLong

  2. Lower write-concern.降低写关注度。 MongoDB has rich options to control awareness of write operations. MongoDB 有丰富的选项来控制写操作的感知。 You can set matching between LogLevel and writing rules.您可以设置 LogLevel 和写入规则之间的匹配。 For example DEBUG , INFO , WARN can go with WriteConcern.UNACKNOWLEDGED and ERROR , FATAL can be stored with WriteConcern.ACKNOWLEDGED .例如DEBUGINFOWARN可以与WriteConcern.UNACKNOWLEDGEDERROR一起使用, FATAL可以与WriteConcern.ACKNOWLEDGED一起存储。 Such way you improve application performance by avoiding pause during low-priority messages writing.通过这种方式,您可以通过避免在低优先级消息写入期间暂停来提高应用程序性能。 The same time you are sure that important messages (that are seldom) placed to storage.同时您确定重要的消息(很少)被放置到存储中。

  3. Cache you collection instance.缓存你的集合实例。 I mean avoid resolving Mongo's objects over getDB or getCollection each time when message arrives.我的意思是避免每次消息到达时通过getDBgetCollection解析 Mongo 的对象。

  4. Minify amount of data passed by network.减少网络传递的数据量。 Restrict your message by minimal set of fields.通过最少的字段集限制您的消息。 Truncate too long stack trace.截断太长的堆栈跟踪。 Look how Spring 3.x shortens full name of class swsmmaRequestMappingHandlerMapping instead of some.whatever.sub.main.minimal.agent.RequestMappingHandlerMapping看看 Spring 3.x 如何缩短类swsmmaRequestMappingHandlerMapping全名而不是some.whatever.sub.main.minimal.agent.RequestMappingHandlerMapping

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM