If you look at Application Architecture, middleware is in fact the middle of the architecture. It really is the meat of a web application. From following figure, you see the MVC design pattern is implemented as a part of middleware. Actually, it is implemented on top of the middleware.

what is middleware

Definition of Middleware

Middleware is the "software glue" betwen the operating system and applications on each side of a client-server architecture, some refer to it as the "dash" in client-server.

Middleware could be considered as the software that provides services to applications beyond those available from the underlying operating system - it connects applications running on the server side, and pass data between them.

Middleware allows multiple processes running on different machines to interact.

Rails Middleware

  • In Rails, a middleware called Rack, is automatically provided.
  • Rack provides a unified and simple interface that allows applications to "talk to" web server, including Mongrel, Thin, Phusion, Apache, etc
    For example: Rack is responsible for handling HTTP requests and responses.
  • Rack is used to group and organize modules, typically written in Ruby, and to specify the dependencies between them. Rack::Builder puts these together, creating a stack-like structure that can be used by a web application. To see the middleware installed in a Rails application, from the root of the application, type:
    $ rake middleware
  • Other Ruby frameworks, e.g., Sinatra, are also built on top of Rack, but it doesn't have the database backend.
  • For the default middleware configuration that Rails provides – it’s useful to know what’s “under the hood.”
  • When you execute:
    $ rails server
    a Rack::Server object is created and attached to the web server (WEBrick by default), and the middleware components are loaded up. The Rack::Server#start method starts the web server running, listening on the designated port for HTTP requests.

References & Resources

  • N/A