Building a cloud-friendly application

Larry Garfield

@Crell

Larry implements Huggable
Software is eating the world, and cloud is eating software.

(Source: @bassamtabbara)

What is cloud
computing?

What is
The Cloud?

These are separate questions...

The Cloud™: noun

Someone else's hard drive

Cloud computing: noun

Abstracting away physical infrastructure

Disposable application design

What makes an application
cloud-friendly?

They're not rules

They're more like guidelines...

They're more like guidelines

Split code from content

Code

  • Provided by developer
  • Carefully tested
  • Lives in version control
  • Read-only runtime

Content

  • Provided by users
  • Frequently ad hoc
  • Lives in DB or filesystem
  • Writeable runtime
Don't mix the filesystems

Your application is disposable.
Your data is not.

Data flow

Code flows up to production, data flows down.

You don't get an in-between option

Take-away

Cleanly separate "Dev provided" and "user provided" files

What is configuration?

Does config come from the developer or the user?

Git or Database?

Drupal Config Management

Drupal 8
  1. Configure in UI / DB
  2. Export to YAML
  3. Commit to Git
  4. Push / Pull
  5. Import back to DB

Take-away

Decide

What happens at runtime
stays at runtime

Application slots into cluster

Dependency inject your environment

  • DB credentials (Solr, Redis, etc.)
  • API keys
  • All paths on disk
  • Domain names

Environment variables


getenv('foo');

$_ENV['foo'];

$_ENV['foo']['bar'];
        

Only ever use getenv()

Need glue code

Platform.sh / Symfony 3


// platform_parameters.php
if (getenv('PLATFORM_RELATIONSHIPS')) {
    $relationships = json_decode(base64_decode(getenv('PLATFORM_RELATIONSHIPS')), true);

    foreach ($relationships['database'] as $endpoint) {
        if (!empty($endpoint['query']['is_master'])) {
            $container->setParameter('database_driver', 'pdo_'.$endpoint['scheme']);
            $container->setParameter('database_host', $endpoint['host']);
            $container->setParameter('database_port', $endpoint['port']);
            $container->setParameter('database_name', $endpoint['path']);
            $container->setParameter('database_user', $endpoint['username']);
            $container->setParameter('database_password', $endpoint['password']);
            break;
        }
    }
}
$container->setParameter('kernel.secret', getenv('PLATFORM_PROJECT_ENTROPY'));
        

Platform.sh / Symfony 5


          composer require platformsh/symfonyflex-bridge
        

            function mapPlatformShEnvironment() : void
            {
                $config = new Config();

                $secret = getenv('APP_SECRET') ?: $config->projectEntropy;
                setEnvVar('APP_SECRET', $secret);

                $appEnv = getenv('APP_ENV') ?: 'prod';
                setEnvVar('APP_ENV', $appEnv);

                mapPlatformShDatabase('database', $config);

                if (!getenv('MAILER_DSN')) {
                    mapPlatformShMailer($config);
                }
            }
            // ...
              

Platform.sh / Laravel


composer require platformsh/laravel-bridge
        

function mapPlatformShEnvironment() : void
{
    $config = new Config();

    $secret = "base64:" . base64_encode(substr(
          base64_decode($config->projectEntropy), 0, 32));
    setEnvVar('APP_KEY', $secret);

    $secure_cookie = getenv('SESSION_SECURE_COOKIE') ?: 1;
    setEnvVar('SESSION_SECURE_COOKIE', $secure_cookie);

    mapAppUrl($config);
    mapPlatformShDatabase('database', $config);
    mapPlatformShRedisCache('rediscache', $config);
    mapPlatformShRedisSession('redissession', $config);
}
// ...
        

Platform.sh / Express.js


const platformsh = require('platformsh-config');
let config = platformsh.config();

const credentials = config.credentials('database');

const connection = await mysql.createConnection({
    host: credentials.host,
    port: credentials.port,
    user: credentials.username,
    password: credentials.password,
    database: credentials.path,
});

var app = express();
// ...

app.listen(config.port);
        

Glue of last resort

composer.json


{
    "autoload": {
        "files": ["extra-bridge.php"]
    }
}
        

Only works if your app reads env vars...

Use DotEnv (.env files)

(Many to pick from in every language)

Trusted domains

(App-specific configuration)

Symfony\HttpFoundation

Request::setTrustedHosts()

Constants? Static config files?

Take-away

Dependency inject your environment

User-configured connections

Installers

  1. Ask for DB credentials
  2. Ask user for basic site info
  3. Write credentials to config file
  4. Populate DB
  5. Write basic site info to config file/DB
  6. Profit!!!

Cloud

Better installers

  • Pre-include connection glue
  • Installer skips pre-populated values
  • Do not download from installer

Avoid lock-in

Always be able to
take your business elsewhere.

Google has killed...

  • Reader
  • iGoogle
  • Google Talk
  • Google Health
  • Knol
  • Google Insights
  • Picnik
  • Buzz
  • Aardvark
  • Sidewiki
  • Notebook
  • Dictionary
  • Labs
  • Wave
  • SearchWiki
  • Dodgeball
  • Jaiku
  • Lively
  • Page Creator
  • Plus
  • Zeitgeist
  • Answers
  • Google X
  • Catalog
  • Web Accelerator
  • Video Player
  • Sets
  • SearchMash
  • Writely
  • Plus+

Source: WordStream (2015)

Use Free Software

Use replaceable services

Safe

  • MySQL/MariaDB
  • PostgreSQL
  • RabbitMQ
  • Solr/Elasticsearch
  • InfluxDB

Unsafe

  • Amazon RDS
  • Amazon DynamoDB
  • Azure Cosmos DB
  • Anything you can't replace in a day

Microservices: Threat or menace

What is a microservice?

There is no industry consensus yet regarding the properties of microservices, and an official definition is missing as well.

Wikipedia

Properties of microservice design

(Sources: Wikipedia, Martin Fowler)

  • Single-purpose components
  • Dumb pipes (HTTP, IPC, etc.)
  • Separate teams
  • Independent releases

Every microservice treats every other microservice as a separate 3rd party
that may as well be a different company.

Benefits

  • Different tools/languages
  • Small, focused, interdisiplinary teams
  • Strong separation of concerns
  • Scale/evolve/replace separately
Any organization that designs a system (defined broadly) will produce a design whose structure is a copy of the organization's communication structure.

Conway's Law

Drawbacks

  • Network latency
  • Network points of failure (plural)
  • Transitioning code is 10x harder
  • API versioning
  • More staff needed
  • Per-request overhead
"If one of your microservices going down means the others don't work, you don't have a microservice; you have a distributed monolith."

—A few dozen people

So if microservices aren't for me, what is?

Multi-Modal Monolith

Multi-Modal Monolith

  • Single team
  • Discrete components
  • Deploys as once
  • Usually one language (not required)
  • May share datastores

You've probably already done this

Multi-Modal Monolith

 

Cron jobs

Queue workers

"Admin" application

API app

All asynchronous

To summarize...

Remember what they say when you assume

Larry Garfield

@Crell

Director of Developer Experience Platform.sh

The end-to-end web platform for agile teams

Stalk us at @PlatformSH

Buy my book!

https://bit.ly/fn-php

Cover of Thinking Functionally in PHP