In every tech talk, or in every blog post I've read about IEnumerable and IObservable I read that, IEnumerable is pull-based structure and IObservable is push-based structure.
I've read that with IObservable we have async calls, where nothing is blocked and everything is push based.
But,but,but...
What does it really means? Push based and pull based
Because in my opinion in IEnumerable we can also push data into the structure and also pull data from it, I've really lost in that technical terms and ideas.
Please in a normal and human way explain to me the difference between that two structure and difference between push-based and pull-based structures.
Thanks.
Pull-based describes a client-run scheme: Clients make requests, and the data is served up immediately. Push-based describes a server-run scheme: Clients can connect to a push-stream (IObservable), but they can't demand data, they get it when the server feels like giving it.
Push model: when a message sent by the producer arrives at the server, the server will immediately deliver it to the consumer. Pull model: the server does not process the received messages; instead, it only waits for the consumer to actively read them from it, that is, the consumer needs to "pull" messages.
Pull coding or client pull is a style of network communication where the initial request for data originates from the client, and then is responded to by the server. The reverse is known as push technology, where the server pushes data to clients.
Since Kafka is pull-based, it implements aggressive batching of data. Kafka like many pull based systems implements a long poll (SQS, Kafka both do). A long poll keeps a connection open after a request for a period and waits for a response.
Please in a normal and human way explain to me the difference
OK, let's make some toast. That's the most normal human thing I do.
Pull based:
// I want some toast. I will pull it out of the toaster when it is done. // I will do nothing until the toast is available. Toast t = toaster.MakeToast(); t.AddJam(); // Yum.
Push based:
// I want some toast. But it might take a while and I can do more work // while I am waiting: Task<Toast> task = toaster.MakeToastAsync(); Toast t = await task; // await returns to the caller if the toast is not ready, and assigns // a callback. When the toast is ready, the callback causes this method // to start again from this point: t.AddJam(); // Yum.
You want to pull a result, you call a function and you do nothing else until the function returns.
You want a result pushed to you, you call an async function and await the result. When the result is available it is pushed at the callback, which resumes the method where it needs to be.
IEnumerable<T>
is just a sequence of pulls; you call MoveNext
every time you want to pull a result and you get a T
. IObservable<T>
is just a sequence of pushes; you register a callback, and it is called every time a new T
is available.
Put another way: IEnumerable<T>
is logically a sequence of Func<T>
invocations. IObservable<T>
is logically a sequence of Task<T>
continuations. Don't let the fact that they are sequences confuse you; that's incidental. The fundamental idea is that functions are synchronous; you call them and get a result synchronously; if it takes a while, you wait. Tasks are asynchronous; you start them and get the result asynchronously when it is available.
This idea existed in C# before IObservable
and await
. Another way to look at is is: pull-based is like function calls, push-based is like event handlers. A normal function call, you call it when you want something. An event handler, it calls you when something happens. Events are how the C# language itself represents the observer pattern. But events always logically form a sequence, so it makes sense to be able to manipulate a sequence of pushed items the same way we'd manipulate a sequence of pulled items. And hence, IObservable
was invented.
A man walks into a grocery store and asks the shopkeeper if he has any eggs. "Yes," says the shopkeeper. "May I have some?" asks the man. And the shopkeeper gives the man some eggs. "Do you have any more?" asks the man. "Yes," says the shopkeeper. "May I have some?" asks the man. And the shopkeeper gives the man some eggs. "Do you have any more?" asks the man. "No," says the shopkeeper. The man leaves.
That's pull-based. The man keep "pulling" eggs from the shopkeeper until there were none left.
A man walks into a grocery store and asks the shopkeeper if he can delivery any eggs and, if so, can he deliver then whenever he can get them. "Yes," says the shopkeeper. The man leaves. In a few days some eggs arrive. A few more days later some eggs arrive. Then the man calls the shopkeeper and asks the for the deliveries to stop. No more eggs arrive.
That's push-based. The man doesn't wait for the shopkeeper to give him eggs. The man goes about doing something else and the shopkeeper "pushes" the eggs to the man.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With