{"__v":22,"_id":"572a291dc2ce542b00e6a8fa","category":{"project":"5723ead0fda3c70e005b88e5","version":"5723eaebeae5090e00ee61f1","_id":"572a28f6d10a200e00b1cb14","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-05-04T16:53:10.034Z","from_sync":false,"order":1,"slug":"concepts","title":"Concepts"},"parentDoc":null,"project":"5723ead0fda3c70e005b88e5","user":"5723ea8efda3c70e005b88e3","version":{"__v":3,"_id":"5723eaebeae5090e00ee61f1","project":"5723ead0fda3c70e005b88e5","createdAt":"2016-04-29T23:14:51.190Z","releaseDate":"2016-04-29T23:14:51.190Z","categories":["5723eaebeae5090e00ee61f2","5723f854110e570e00486c7a","572a28f6d10a200e00b1cb14"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"0.1.0","version":"0.1"},"updates":["57899a3fc1c3de0e00169dc0"],"next":{"pages":[],"description":""},"createdAt":"2016-05-04T16:53:49.092Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"[block:callout]\n{\n  \"type\": \"success\",\n  \"title\": \"Key takeaways\",\n  \"body\": \"* Services are the basic building blocks of Lever apps.\\n* To define a service you provide the **code** and an **entry point**.\\n* Methods are the functions that make up the entry point of the service.\\n* You can invoke methods from outside Lever or from another Lever service.\\n* Lever runs multiple instances of the service, automatically adjusting their number depending on scale.\"\n}\n[/block]\nLever **services** are the basic building blocks of Lever cloud apps. They contain the business logic of your application and they scale automatically with demand. Lever's mantra is that when you write cloud applications you shouldn't be thinking about servers. You should be thinking about services. This fundamentally changes the way we write cloud application, because the idea assumes a need for scalability, even for the simplest apps.\n\n# Methods\n\nServices are made out of their underlying code (the business logic) and a specified entry point, which represents the API of the service. For example, in JavaScript, the code might be a set of JavaScript files and the entry point might be one of these files, which exports the functions that will be exposed as the service's API. The Lever system automatically maps each of the exported functions to a unique [Lever URL](doc:lever-urls), such that there is a universal way to address them.\n\nThe individual exported functions are called **methods**. Lever methods can be invoked in several ways, either from outside Lever or from another Lever service:\n\n* Through the [Lever CLI](doc:cli).\n* Through the [HTTP API](doc:http-api) - only from outside Lever.\n* Through the [Lever RPC system](doc:rpc) - use the Lever client libraries for this (currently [Node](doc:node-client-api) and [Go](doc:go-api) are supported).\n\nThere are two types of Lever methods:\n\n* Regular methods - a regular request-reply.\n* Streaming methods - they allow the creation of a persistent channel through which a full-duplex conversation could take place between the client and the server. Streaming method names must end with `Chan` or `_chan`.\n\nFor more details see the [Lever RPCs page](doc:rpc).\n\n# Instances\n\nUnder the hood, each Lever service run zero or more Lever **instances** to serve traffic to that service. The number of instances is adjusted automatically by Lever, in real-time, depending on demand.\n\nEach invokation, regardless of its origin, is thus load-balanced between the instances of a Lever service. You should never assume that consecutive requests to a service will hit the same instance. If you need to rely on such an assumption, either use a [streaming method](doc:rpc#section-streaming-rpcs) or the concept of [resources](doc:resources).\n\nEach instance is allocated a certain amount of memory, computing power and network bandwidth. To adjust these see the [ram property in lever.json](doc:lever-json#section--hash-ram-integer-optional-).\n\n### Instance lifecycle\n\nInstances are brought up and stopped as necessary, depending on the amount of traffic the service gets. Because sometimes instances are brought up at request time, it is very important that the implementation does not include any heavy processing as part of the startup procedure. Otherwise this would seriously affect latency on handling some of the requests.\n\nWhen an instance is no longer needed (because the traffic has gone low), it is scheduled for stopping. There is an additional delay before the instance is actually stopped, in case traffic spikes back up.\n\nUpon closing, the instance is sent a `TERM` signal and is allowed to drain for 10 seconds. If the instance does not terminate during this time, it is killed.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Where to next?\"\n}\n[/block]\nTo learn more about Lever OS, continue reading about [Environments](doc:environments), [Lever RPCs](doc:rpc), [Lever URLs](doc:lever-urls) or about [Lever Resources](doc:resources).\n\nLater, you might also want to check out the [Quick Start guide](doc:quick-start), if you haven't already, or the API reference pages. See links on left-hand side.","excerpt":"This page will introduce you to Lever's main building blocks","slug":"services","type":"basic","title":"Services"}

Services

This page will introduce you to Lever's main building blocks

[block:callout] { "type": "success", "title": "Key takeaways", "body": "* Services are the basic building blocks of Lever apps.\n* To define a service you provide the **code** and an **entry point**.\n* Methods are the functions that make up the entry point of the service.\n* You can invoke methods from outside Lever or from another Lever service.\n* Lever runs multiple instances of the service, automatically adjusting their number depending on scale." } [/block] Lever **services** are the basic building blocks of Lever cloud apps. They contain the business logic of your application and they scale automatically with demand. Lever's mantra is that when you write cloud applications you shouldn't be thinking about servers. You should be thinking about services. This fundamentally changes the way we write cloud application, because the idea assumes a need for scalability, even for the simplest apps. # Methods Services are made out of their underlying code (the business logic) and a specified entry point, which represents the API of the service. For example, in JavaScript, the code might be a set of JavaScript files and the entry point might be one of these files, which exports the functions that will be exposed as the service's API. The Lever system automatically maps each of the exported functions to a unique [Lever URL](doc:lever-urls), such that there is a universal way to address them. The individual exported functions are called **methods**. Lever methods can be invoked in several ways, either from outside Lever or from another Lever service: * Through the [Lever CLI](doc:cli). * Through the [HTTP API](doc:http-api) - only from outside Lever. * Through the [Lever RPC system](doc:rpc) - use the Lever client libraries for this (currently [Node](doc:node-client-api) and [Go](doc:go-api) are supported). There are two types of Lever methods: * Regular methods - a regular request-reply. * Streaming methods - they allow the creation of a persistent channel through which a full-duplex conversation could take place between the client and the server. Streaming method names must end with `Chan` or `_chan`. For more details see the [Lever RPCs page](doc:rpc). # Instances Under the hood, each Lever service run zero or more Lever **instances** to serve traffic to that service. The number of instances is adjusted automatically by Lever, in real-time, depending on demand. Each invokation, regardless of its origin, is thus load-balanced between the instances of a Lever service. You should never assume that consecutive requests to a service will hit the same instance. If you need to rely on such an assumption, either use a [streaming method](doc:rpc#section-streaming-rpcs) or the concept of [resources](doc:resources). Each instance is allocated a certain amount of memory, computing power and network bandwidth. To adjust these see the [ram property in lever.json](doc:lever-json#section--hash-ram-integer-optional-). ### Instance lifecycle Instances are brought up and stopped as necessary, depending on the amount of traffic the service gets. Because sometimes instances are brought up at request time, it is very important that the implementation does not include any heavy processing as part of the startup procedure. Otherwise this would seriously affect latency on handling some of the requests. When an instance is no longer needed (because the traffic has gone low), it is scheduled for stopping. There is an additional delay before the instance is actually stopped, in case traffic spikes back up. Upon closing, the instance is sent a `TERM` signal and is allowed to drain for 10 seconds. If the instance does not terminate during this time, it is killed. [block:api-header] { "type": "basic", "title": "Where to next?" } [/block] To learn more about Lever OS, continue reading about [Environments](doc:environments), [Lever RPCs](doc:rpc), [Lever URLs](doc:lever-urls) or about [Lever Resources](doc:resources). Later, you might also want to check out the [Quick Start guide](doc:quick-start), if you haven't already, or the API reference pages. See links on left-hand side.