MATLAB, with its large collection of domain specific toolboxes, provides much more than a powerful environment for users to prototype algorithms. It offers the ability to build software products and services that scale gracefully from a single user/application to offering a service to an entire enterprise.
Prototypes are important, but nothing stays as a prototype forever!
If merely an experiment, algorithms are replaced with the next bigger, better implementation. If valuable, the MATLAB based algorithm is shared with a target audience for the insights it provides. This audience could be as limited as a few colleagues or as wide as a publicly available scientific paper. Depending on the needs of the target audience, MATLAB offers a variety of options to socialize and consume the insight from the analysis. These options range from accelerating exploratory programming to creating interactive narratives (such as with the live editor), to integration with other production quality, enterprise grade software stacks.
For business-critical solutions, developers make the analysis itself available across the entire enterprise (or similar user-base). To support this need, MATLAB offers a portfolio of products to support an appropriate deployment option from standalone applications to running on technology stacks as diverse as Microsoft Excel®, web applications, databases, application servers or even Hadoop systems.
To make it tangible, let's take a MATLAB algorithm from prototype all the way to a production-grade service.
To keep this example simple, let's use the Fibonacci generator that Andy blogged about recently to build a MATLAB powered service.
Building a MATLAB powered service
My example application will call a MATLAB algorithm as a service to compute a fibonacci sequence to the N'th term.
As an illustration, I will pick a painless approach to expose a service that computes a Fibonacci sequence.
The MATLAB Production Server™ is an enterprise-grade solution that provides the ability for developers to integrate their MATLAB algorithms into full service-oriented architectures (SOA). A service-oriented architecture (SOA) is an architectural pattern in computer software design in which application components provide services to other components via a communications protocol, typically over a network and usually independent of any vendor, product, or technology.
On Production Server, your MATLAB algorithm can now run as a service 24 x 7 x 365, and importantly, scale both wide and deep. The real beauty of this is that developers are now enabled in their environment of choice to request stateless MATLAB powered analysis. MATLAB programs become accessible to production IT systems irrespective of the technology stack, vendor or language.
At the heart of our service, for this example, is the MATLAB code that computes the N'th Fibonacci number:
function f = fibonacci(n) % A simple example for generating the N'th Fibonacci number % Uses the Matrix Exponential method F = [1 1;1 0]^n; f = F(1,2); end
Packaging this algorithm to run on MATLAB Production Server takes a few clicks using the Production Server Compiler which can be found in the Apps catalog or can be invoked as a single command productionServerCompiler. It is possible to package multiple algorithms at once. In this case, I also package a second function called that calls the code above to compute the Fibonacci series up to a sensible given value of N (validating the input when requested).
We can now configure the tool to create a deployable package example.ctf which when deployed to the Production Server allows us to exercise the Fibonacci number generator algorithm. Clicking the Package button generates a component technology file (CTF) that is ready for use.
This process can support multiple functions and even accommodate an entire suite of software with their additional supporting files as a part of the same CTF file.
Setting up the infrastructure
Please permit me to put on my IT hat for minute.
The entire idea of this exercise is that I can provision infrastructure as a one-time exercise that will allow my developers to deploy their MATLAB algorithms as services. I want to make this post an interactive experiment with you, the reader, exercising this code as a MATLAB powered service, right here in your browser by the end of this narrative.
To setup the infrastructure, I spun up a compute optimized server on AWS running Windows Server 2012. On this machine, I setup the rest of the infrastructure by installing the MATLAB Production Server and a MATLAB Runtime. Additionally, I configured a few security rules and firewall settings to allow connection on port 31415 where I intend to expose my service.
The software installation is mostly a one-click process and starting up a new server instance is made easy by the scripts provided by the MATLAB Production Server product.
Configuring the server instance is done through the configuration file which is located in \services\config\main_config. In this case, I make a few key changes to expose my service and match the IT configuration. In a nutshell:
# Bind service to port 31415 (i.e. the service port) --http 31415
# MATLAB Runtime Settings (i.e. where is the MATLAB Runtime) --mcr-root C:\Program Files\MATLAB\MATLAB Runtime\v901
# CORS Allowed Origins (i.e. where can this be accessed from) --cors-allowed-origins *
The product is designed to be highly customizable and provides a large number of settings in the config file that includes scalability settings such as the number of workers (usually set to 1 per core to scale deep), and security settings such as configuring HTTPS and associated certificates.
Starting the server:
C:\Work>mps-start -C services
Deploying the generated Fibonacci CTF file can be done on the fly. i.e. it is possible to deploy, update, and remove services on a running MATLAB Production Server instance. It is as simple as copying the file over to the ./services/auto_deploy folder.
That's it, MATLAB Production Server is now serving the algorithm as a HTTP service on port 31415 of my server. It will accept inputs through a number of mechanisms including a simple JSON encoded string.
MATLAB analysis from everywhere
Go ahead, give it a try: http://jsfiddle.net/hosagrahara/ky5c6aoz/show
Click the link, hit Run, enter a value and submit to watch MATLAB code execute as a service.
Please go ahead and play around, or feel free to fork away and please do post a comment if you build something interesting with a fibonacci computation. MATLAB running as a service can drive downstream functionality - for example, I can power a visualization of my choice like: http://jsfiddle.net/hosagrahara/vrhyd4uc/show
Note: I plan to shut off the MATLAB service when this post is 2 weeks old. If requested via the comments below, I can turn it back on briefly..
Walk a mile in my shoes
My sizing of infrastructure was to support a basic demo linked to a blog post. In a real application scenario, the possibilities are nearly limitless permitting you to exercise MATLAB analysis from every level of most applications - i.e. from the database, application code or even the browser as a part of your business-critical software.
Hopefully this post described a skeletal workflow from MATLAB code and analysis to a service available to the entire world. Before I end this post, I wanted to share with you a few things that you may not realize unless you have walked a mile in my shoes.
In a nutshell, MATLAB Production Server can be used to build services and microservices that are:
- Version agnostic: The product will run code built in any MATLAB version as long the infrastructure can find the corresponding runtime in its configuration. In some places, this fact alone has some IT organizations standing, clapping, and cheering.
- Built for performance: The product scales gracefully. Since the communication is over HTTP(s), it is possible to scale deep by using more powerful hardware or scale wide by using more than one instance to provide load-balanced services. This makes it amenable for resilient operation by adopting industry standard practices for scaling, fail-over, and disaster recovery.
- Enable developer-grade workflows: Since it is possible to split workers across multiple instances, ports and even computers, the product allows the creation of different environments for development, test, and production.
- Enable enterprise grade security: Since the service is HTTP(s) accessible, the access of the service can be protected with battle tested security products and practices that empower the bulk of the internet. The service can be as open as you can see in JSFiddle links above or secured as completely as your banking website.
In conclusion, build a MATLAB powered software product or a MATLAB powered service - you can decide based on the needs of your next development project. With the right approach, technology and tools, your favorite MATLAB analysis could be merely a click away.
Please do share your thoughts. Your comments make my day!
Get the MATLAB code
Published with MATLAB® R2016b