Pipelining vs Batching in Stackexchange.Redis

前端 未结 1 405
旧时难觅i
旧时难觅i 2020-12-02 13:56

I am trying to insert a large(-ish) number of elements in the shortest time possible and I tried these two alternatives:

1) Pipelining:

List

        
相关标签:
1条回答
  • 2020-12-02 14:27

    Behind the scenes, SE.Redis does quite a bit of work to try to avoid packet fragmentation, so it isn't surprising that it is quite similar in your case. The main difference between batching and flat pipelining are:

    • a batch will never be interleaved with competing operations on the same multiplexer (although it may be interleaved at the server; to avoid that you need to use a multi/exec transaction or a Lua script)
    • a batch will be always avoid the chance of undersized packets, because it knows about all the data ahead of time
    • but at the same time, the entire batch must be completed before anything can be sent, so this requires more in-memory buffering and may artificially introduce latency

    In most cases, you will do better by avoiding batching, since SE.Redis achieves most of what it does automatically when simply adding work.

    As a final note; if you want to avoid local overhead, one final approach might be:

    redisDB.SetAdd(string.Format(keyFormat, row.Field<int>("Id")),
        row.Field<int>("Value"), flags: CommandFlags.FireAndForget);
    

    This sends everything down the wire, neither waiting for responses nor allocating incomplete Tasks to represent future values. You might want to do something like a Ping at the end without fire-and-forget, to check the server is still talking to you. Note that using fire-and-forget does mean that you won't notice any server errors that get reported.

    0 讨论(0)
提交回复
热议问题