Vinay
Vinay

Reputation: 1

How to write logs from multiple processes to a single file sorted by timestamp?

Consider there are 3 processes that needs to log messages to a single file. The rate of messages from the 3 processes are different. Ex: P1 logs 10 requests per second; P2 logs 20 requests per second and P3 logs just 1 or 2 requests per second. How can we ensure that these logs are written in the order of the timestamp ? I mean it should be sorted by the timestamp before writing to the file.

How could we achieve this ?

Upvotes: 0

Views: 149

Answers (1)

Jim Mischel
Jim Mischel

Reputation: 134125

You could create a priority queue, ordered by time, that holds a few seconds' worth of messages. When a processes calls the log service, the service attempts to add the message to the queue. If the queue is full, then it pulls the first item (which is the oldest message) from the queue, outputs it, and then adds the new message to the queue.

This allows for messages that come in slightly out of order, or for servers whose times are slightly off (by up to a few seconds).

You said that your processes log a total of 30 or 35 messages per second. You probably want to allow maybe three times that to allow for traffic spikes, and then another two or three times that so you can hold two or three seconds of messages. Let's just say the queue holds 300 messages. Make it 1,000 messages if you want to allow more leeway.

And if you're worried about the queue not flushing fast enough during slow periods, you could either add a timer that flushes anything older than some set value, or make the writing code always remove anything that's older than some value.

This isn't a perfect solution. If you suffer network delays, or if one of your servers' clocks is off by a lot, or some thread gets hung up for several seconds before calling the logging service, the message could get written to the log out of order, because newer messages were already written. You could identify such messages and handle them exceptionally. Of course, the larger your queue, the less likely it is for something like this to happen.

You might ask yourself it's really necessary that the logs be written in strict chronological order.

Upvotes: 1

Related Questions