4. Examples of few simple apps
1. Get an image as input, resize and store it somewhere. That’s state.
2. Register a user, and create a list of Todo items
That’s definitely state.
3. Given a city, retrieve the current weather.
Ah! No state?
5. Given a city, retrieve the current weather
There is no state, only if you
are NOT Yahoo Weather
What we have done is
delegated state to a
black-box
There are NO useful apps without state. All we can do is delegate, delegate and
delegate!
@Tirumarai
6. Patterns for delegating state
1) Send full context in event payload
2) Use BaaS aka managed services
3) Run lightweight long running proxies
4) Apply state externally
5) Use fast transactional databases (escape hatch)
@Tirumarai
7. Pattern #1: Send context with event
Send as much context as possible with an event
Event #1
"id": “42”
Event #2
"user_info": {
"user-id": "1"
},
"op": "UPDATE",
"data": {
"old":{
"id":"42",
"name": "jane"
},
"new": {
"id":"42",
"name": "john doe"
}
}
Obviate the need
for database read
❌ ✅
8. Example #1: Send context with event
Send email on user creation
"user_info": {
"user-id": "1",
"name": "Jane",
"address": "Ch. S",
},
"login": "Facebook",
"created": "14-02-19"
"utm": {
"source": "twit",
"campaign": "ad"
}
@Tirumarai
9. Pattern #2: Use BaaS/managed services
Use as much BaaS / managed services as you can afford.
Also known as serviceful serverless
Let someone else deal with state
Identity: Auth0
Media tools: Cloudinary
Search: Algolia
Notifications: Twilio
E.g.
@Tirumarai
10. Example #2: Use BaaS/managed services
Index items for search
11. Pattern #3: Run long running proxies
Sometimes, may need to run long running proxies for
managing state.
E.g. Delegate database connections to connection poolers
running on Fargate/managed containers
@Tirumarai
12. Example #3: Run long running proxies
Running a
GraphQL
service on
serverless
https://medium.com/open-graphql/
scaling-rdbms-for-graphql-backend
s-on-serverless-980ef24af171
13. Pattern #4 : Apply state externally
Make code stateless and return a final object to be
persisted.
Object is persisted reliably and idempotentically i.e.
definitely persisted and can handle duplicates.
@Tirumarai
15. Pattern #5 : Use fast transactional databases
Sometimes, databases and transactions are just
unavoidable.
Use fast databases with lightweight process management
for more scale.
@Tirumarai
16. Example #5 : Use fast transactional
databases
Postgres + pgBouncer
Most NoSQL databases
@Tirumarai
18. Stateless == Idempotency?
What people usually mean by stateless is idempotency
If we can ensure idempotency, our stateful function seems
like a stateless function!
Managed services should be idempotent for use with
serverless