I have a collection of documents, which hold a subject id, a timestamp and a value. For example:
{ sid: 1, t: 3, v: "A" }
{ sid: 1, t: 5, v: "B" }
Which means subject#1 is measured to have value A at t=3. Later, at t=5, value changed to "B". Only when a value is changed, a new document is inserted.
My goal is to keep the history of changes, and allow queries
for multiple subject IDs (or other fields not mentioned here) to get full histories or
for a subject and a timestamp for "value at that time".
Periodically I receive another set of documents containing various subjects and values at different times (t). Then I need to merge this new information to the existing data. Let's say I receive:
{ sid: 1, t: 2, v: "A" }
{ sid: 1, t: 6, v: "B" }
{ sid: 2, t: 5, v: "C" }
Not every document in this new set gives me useful information. To explain:
First one is useful because it lets me know a change had happened earlier than I thought, so I should use it. I'm trying to detect changes as early as possible:
Second one is not useful because I already knew the value change from "A" to "B" happened at t=5, earlier than the new info.
Third one is useful because I didn't have any info about subject#2 before.
So after the merge result should be:
{ sid: 1, t: 2, v: "A" }
{ sid: 1, t: 5, v: "B" }
{ sid: 2, t: 5, v: "C" }
The new information arrives as a stream, about 2-3k docs/second. I can process it as a stream or in batches 15 minutes wide = 2.5M docs. Existing changes-data collection has hundred millions of docs.
New arriving data is not ordered in time, it can contain timestamps earlier or later than the existing data.
Easiest way that comes the mind is to query existing collection for every incoming document and see if a useful change had happened, but it would mean thousands of queries per second to the datastore (Solr).
Another way is to somehow detect which set of documents might potentially be affected by the new data and load it in one shot, revise it and write it back in one go. I couldn't figure out how can I determine those documents though.**
I tried to ask the question independent of any particular technology because I thought this design problem is independent from the tech stack too, but the changes data will be stored in Solr and I can run the aggregations in Spark. Another tech stack suggestions are welcome too if they can solve this problem.
I can also change the schema and the way data is represented in both existing and incoming data, if it can help solve this.
Do you have a design suggestion to this problem, or an answer to my starred ** question?