特征阻抗輸入阻抗輸出阻抗
by Milan Mimica
米蘭·米米卡(Milan Mimica)
軟件阻抗說明 (Software impedance explained)
數據處理組件之間的阻抗不匹配 (The impedance mismatch between data processing components)
It all starts with the simplest signal-processing diagram ever:
一切始于有史以來最簡單的信號處理圖:
Component A is passing the signal to component B. Let’s switch to software engineering jargon immediately: A producer is invoking a method of a consumer. Invoking a method takes a finite time. We call this response time or latency. The producer can pass an arbitrary (but limited) amount of data to each method invocation. We call this batch size. Concurrency level is another variable we can play with. The producer can control the number of concurrent invocations by limiting the window size of pending responses. Invoking the method concurrently indeed multiplies the throughput. Throughput (T) is a function of window size (W), batch size (B) and latency (t).
組件A將信號傳遞給組件B。讓我們立即切換到軟件工程術語:生產者正在調用使用者的方法。 調用方法需要有限的時間。 我們稱此為響應時間或等待時間 。 生產者可以將任意數量(但數量有限)的數據傳遞給每個方法調用。 我們稱此為批量大小 。 并發級別是我們可以使用的另一個變量。 生產者可以通過限制未決響應的窗口大小來控制并發調用的數量。 并發調用該方法確實使吞吐量成倍增長。 吞吐量(T)是窗口大小(W),批處理大小(B)和等待時間(t)的函數。
We aim for maximum throughput, so increase the concurrency and use higher batch sizes. If only! Response time depends on batch size and window size. To put it more formally, response time if a function of both window size and batch size.
我們的目標是最大吞吐量,因此增加并發性并使用更大的批處理大小。 要是! 響應時間取決于批處理大小和窗口大小。 更正式地說,響應時間是窗口大小和批處理大小的函數。
To achieve maximum throughput we must find the highest W and B that produce the lowest t. Non-ideal W and B will induce a higher “resistance” in the component, or call it back-pressure if you will.
為了獲得最大的吞吐量,我們必須找到產生最低t的最高W和B。 非理想的W和B會在組件中引起更高的“阻力”,或者如果您愿意,可以將其稱為背壓 。
Whether it produces the data or just passing it through, a producer must adapt window size and batch size to best suit the consumer. Otherwise we have what I call software impedance mismatch.
無論是產生數據還是僅傳遞數據,生產者都必須調整窗口大小和批處理大小以最適合消費者。 否則,我們稱之為軟件阻抗不匹配。
There is no generic expression for f(W, B) as it depends on the component’s implementation. Theory is of no help here. You have to probe the consumer with different batch and window sizes to spot the ideal values that will maximize the throughput.
f(W,B)沒有通用表達式,因為它取決于組件的實現。 理論在這里沒有幫助。 您必須用不同的批次和窗口大小來探測使用者,以找出理想的值,這將使吞吐量最大化。
Once you find the ideal batch size you must build an “impedance adapter”. Here is a suggested java implementation that accumulates the items and batches them before sending them to the next component (lots of boilerplate code omitted for brevity).
一旦找到理想的批量大小,就必須構建一個“阻抗適配器”。 這是一個建議的Java實現,該實現在將項目發送給下一個組件之前將其累積并進行批處理(為簡便起見,省略了許多樣板代碼)。
Note that invoking the consumer with smaller than maximum (ideal) batch size is still allowed. This assures no additional latency is added. If the selected batch size is optimal, assuming steady item feed, invoking the consumer will take exactly the time it takes to fill maxBatchSize items in the queue.
請注意,仍然允許使用小于最大(理想)批次大小的使用者。 這樣可以確保不增加額外的延遲。 如果選定的批次大小是最佳的,則假定穩定的項目進給,調用消費者將花費完全的時間來填充隊列中的maxBatchSize項目。
Likewise, the number of concurrent method invocations towards some consumer instance can be controlled using a semaphore.
同樣,可以使用信號量來控制對某個消費者實例的并發方法調用的次數。
推與拉模式 (Push vs. Pull mode)
The above scenario depicts “push” mode, in which the producing component controls the invocation, its timing, and the mentioned key parameters. A more modern approach of dealing with back-pressure is to put the consuming component in charge of the invocation. This puts the system designer in a slightly better position, as engineers of the consuming component don’t need to communicate batch and window size to producers. Nevertheless, a similar impedance adapter is needed.
上面的場景描述了“推送”模式,在該模式下,生產組件控制調用,其時間安排以及提到的關鍵參數。 處理背壓的一種更現代的方法是由消耗組件負責調用。 這使系統設計人員處在更好的位置,因為消耗組件的工程師不需要將批量和窗口大小傳達給生產者。 然而,需要類似的阻抗適配器。
結論 (Conclusion)
Impedance adapters are stateful components, consisting of queues, threads, callback maps, etc., which adds complexity, but matching impedance is essential in inter-component communication.
阻抗適配器是有狀態的組件,由隊列,線程,回調映射等組成,這增加了復雜性,但是匹配阻抗對于組件間的通信至關重要。
I argue that each component should specify its impedance parameters: optimal batch size and concurrency level. That way producer components can adapt to minimize the back-pressure.
我認為每個組件都應指定其阻抗參數:最佳批大小和并發級別。 這樣生產者的組件可以適應以最小化背壓。
Unlike the electrical impedance, the impedance in software is not limited to two dimensions. Here I show only two parameters, but often the response time depends on other variables too.
與電阻抗不同,軟件中的阻抗不限于二維。 在這里,我僅顯示兩個參數,但是響應時間通常也取決于其他變量。
Impedance is a very dynamic property. It may depend on the data being sent, overall workload, sometimes even on variables not controlled by the caller. If needed, the consumer’s API should be designed in a way that allows the component to publish its latest impedance parameters via API. That way the producers could dynamically adapt.
阻抗是非常動態的屬性。 它可能取決于發送的數據,總體工作量,有時甚至取決于調用者無法控制的變量。 如果需要,消費者的API的設計方式應允許組件通過API發布其最新的阻抗參數。 這樣生產者就可以動態地適應。
翻譯自: https://www.freecodecamp.org/news/software-impedance-6796cc65758b/
特征阻抗輸入阻抗輸出阻抗