Middleware API adopted from PHP community https://stackphp.com and this will help you to filter every request from users before hitting to the application, suppose you want to block some request based on the request object parameter or you can add the proper headers to all responses leaving your application.
Middleware API is equivalent to hook_boot() in Drupal 7. That means this will trigger every request so adding Middlewares are very expensive. Effectively, Don't manage heavy or complex code inside the Middleware it will leads to performance issues on Drupal.
Code snippet that can be used to implement custom middleware to ban an IP address: