I have a multithreaded Linux C application that interacts with a Redis database by means of the Hiredis library, the synchronous API. It works as expected, but I have noticed that every once in a while, a response from the redisCommand() function seems to be overwritten by another thread. The multithreaded character of the application is no doubt behind it: when I use four or eight instances of the relevant thread, I have yet to notice the issue. With sixteen threads, I see one such overwriting event every five hours or so during the life of my application; with thirty two threads, several events are apt to show up every hour.
Before I start adding mutex locks - which, if carried to extremes, just end up serializing the application - I would like to ask members of this forum for advice on how to proceed. I was thinking that copying the contents of the redisCommand() reply locally, and then operating on the copied data, might help. However, I am not sure how efficient that will be in general - the redisCommand() reply, which essentially amounts to a linked list, might contain numerous entries. At any rate, if anybody with extensive experience with Hiredis (either the synchronous or the asynchronous API) could give some pointers on how to deal with this, I would be most interested to read what they have to say.